Servlet으로 계산기를 만들어보았다.
ssss.jsp에서 /calc로 mapping 지정후 post방식으로 server로 넘겨주어
server에서 dopost() 메서드를 호출하여 결과페이지를 띄워준다.


부호선택

계산 버튼을 누를시

결과 출력
[소스코드]
ServletTest.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.Calendar;
public class ServletTest extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("euc-kr");
int n1 = Integer.parseInt(request.getParameter("n1"));
int n2 = Integer.parseInt(request.getParameter("n2"));
String op = request.getParameter("op");
Calc result1 = new Calc(n1,n2,op);
PrintWriter out = response.getWriter();
out.append("<html><body><h2>결과</h2><hr>")
.append(result1.getX()+result1.getOp()+result1.getY()+"="+result1.getResult()+"</body></html>");
}
}
Calc.java
public class Calc {
int x;
int y;
long result;
String op;
Calc(int x,int y, String op){
this.x = x;
this.y = y;
this.op = op;
}
public void cal() {
switch(op) {
case "+": result = x+y; break;
case "-": result = x-y; break;
case "/": result = x/y; break;
case "*": result = x*y; break;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public long getResult() {
return result;
}
public void setResult(long result) {
this.result = result;
}
public String getOp() {
return op;
}
public void setOp(String op) {
this.op = op;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>ServletTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/calc</url-pattern>
</servlet-mapping>
</web-app>
ssss.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.Calendar"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
%>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>ServletTest</title>
</head>
<body>
<h1 align="center">계산기</h1><hr>
<form method="post" action="/calc">
<div align="center">
<input type="text" name="n1"size="10">
<select name="op" id="cal">
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
<option value="*">*</option>
</select>
<input type="text" name="n2"size="10">
<button type="submit" value="Run">계산</button>
<button type="reset" value="다시입력">다시입력</button>
</div>
</form>
</body>
</html>
'JSP' 카테고리의 다른 글
Servlet GET, POST 한글 처리 방법(Encoding) (0) | 2022.09.18 |
---|---|
JSP 4가지 Scope (0) | 2022.09.18 |
Servlet 매핑 (0) | 2022.09.16 |
HttpServlet 클래스 (0) | 2022.09.15 |
Tomcat Error Code (0) | 2022.09.15 |