Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch05] 5.3, Implemented Hello, Calculator JSP file.
Browse files Browse the repository at this point in the history
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
  • Loading branch information
dhsim86 committed Mar 11, 2017
1 parent 19bc501 commit e4aff02
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/main/webapp/Lesson05/Calculator.jsp
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);
}
%>
16 changes: 16 additions & 0 deletions src/main/webapp/Lesson05/Hello.jsp
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>

0 comments on commit e4aff02

Please sign in to comment.