This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch05] 5.3, Implemented Hello, Calculator JSP file.
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<%-- | ||
Created by IntelliJ IDEA. | ||
User: Dongho | ||
Date: 2017. 3. 11. | ||
Time: PM 2:41 | ||
To change this template use File | Settings | File Templates. | ||
--%> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
<% | ||
String v1 = ""; | ||
String v2 = ""; | ||
String result = ""; | ||
String[] selected = {"", "", "", ""}; | ||
if (request.getParameter("v1") != null) { | ||
v1 = request.getParameter("v1"); | ||
v2 = request.getParameter("v2"); | ||
String op = request.getParameter("op"); | ||
result = calculate(Integer.parseInt(v1), | ||
Integer.parseInt(v2), | ||
op); | ||
if ("+".equals(op)) { | ||
selected[0] = "selected"; | ||
} | ||
else if ("-".equals(op)) { | ||
selected[1] = "selected"; | ||
} | ||
else if ("*".equals(op)) { | ||
selected[2] = "selected"; | ||
} | ||
else if ("/".equals(op)) { | ||
selected[3] = "selected"; | ||
} | ||
} | ||
%> | ||
<html> | ||
<head> | ||
<title>Calculator</title> | ||
</head> | ||
<body> | ||
<h2>JSP Calculator</h2> | ||
|
||
<form action="Calculator.jsp" method="get"> | ||
<input text="text" name="v1" size="4" value="<%=v1%>"> | ||
<select name="op"> | ||
<option value="+" <%=selected[0]%>> + </option> | ||
<option value="-" <%=selected[1]%>> - </option> | ||
<option value="*" <%=selected[2]%>> * </option> | ||
<option value="/" <%=selected[3]%>> / </option> | ||
</select> | ||
<input type="text" name="v2" size="4" value="<%=v2%>"> | ||
<input type="submit" value="="> | ||
<input type="text" size="8" value="<%=result%>"><br> | ||
</form> | ||
</body> | ||
</html> | ||
<%! | ||
private String calculate(int a, int b, String op) { | ||
int r = 0; | ||
if ("+".equals(op)) { | ||
r = a + b; | ||
} | ||
else if ("-".equals(op)) { | ||
r = a - b; | ||
} | ||
else if ("*".equals(op)) { | ||
r = a * b; | ||
} | ||
else if ("/".equals(op)) { | ||
r = a / b; | ||
} | ||
return Integer.toString(r); | ||
} | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<%-- | ||
Created by IntelliJ IDEA. | ||
User: Dongho | ||
Date: 2017. 3. 11. | ||
Time: PM 2:03 | ||
To change this template use File | Settings | File Templates. | ||
--%> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
<html> | ||
<head> | ||
<title>Hello</title> | ||
</head> | ||
<body> | ||
<p>Hello World!</p> | ||
</body> | ||
</html> |