간이계산기(JSP > Servlet )
2019. 8. 21. 17:41ㆍ프로그래밍/JSP
1.cal.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h3>calculator</h3>
<hr>
<form name="form1" action="./batman" method="POST">
<input name="num1" type="text" style="width:100">
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input name="num2" type="text" style="width:100">
<input type="submit" value="result">
<input type="reset" value="reset" onclick="checked">
</form>
</center>
</body>
</html>
2.CalcServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CalcServlet
*/
//@WebServlet("/CalcServlet")
public class CalcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public CalcServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
//Get,Post 방식으로 데이터 받음
int num1, num2;
int result;
String op;
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
// html form 을 통해 전달된 num1,num2 패러미터 갑을 변수에 할당
// 이때 getParameter() 메서드는 문자열을 리턴하므로 숫자형 데이터의 경우 Integer.poarseInt()를 통해 int로 변환
num1 = Integer.parseInt(request.getParameter("num1"));
num2 = Integer.parseInt(request.getParameter("num2"));
op = request.getParameter("operator");
// clac() 메서드 호출로 결과 받아옴
//result = calc(num1, num2, op);
Calc calc = new Calc(num1,num2,op);
result = calc.getResult(); // .함수 메소드
out.println("<html>");
out.println("<head><title>계산기</title></head>");
out.println("<body><center>");
out.println("<h2>계산 결과</h2>");
out.println("<HR>");
out.println(num1 + op + num2 + " = " + result);
out.println("</body></html>");
}
public int calc(int num1, int num2, String op) {
int result = 0;
if (op.equals("+")) {
result = num1 + num2;
} else if (op.equals("-")) {
result = num1 - num2;
} else if (op.equals("*")) {
result = num1 * num2;
} else if (op.equals("/")) {
result = num1 / num2;
}
return result;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
class Calc{
int num1, num2;
String op;
int result;
//생성자
public Calc(int num1, int num2, String op) {
this.num1 = num1;
this.num2 = num2;
this.op = op;
result=0;
}
public int getResult() {
if(op.equals("+")) {
result = num1 + num2;
}else if(op.equals("-")) {
result = num1 - num2;
}else if(op.equals("*")){
result = num1 * num2;
}else if(op.equals("/")) {
result = num1 / num2;
}
return result;
}
}
3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Servlet01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>ServletTest01</servlet-name>
<servlet-class>ServletTest01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest01</servlet-name>
<url-pattern>/superman</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CalcServlet</servlet-name>
<servlet-class>CalcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalcServlet</servlet-name>
<url-pattern>/batman</url-pattern>
</servlet-mapping>
</web-app>
'프로그래밍 > JSP' 카테고리의 다른 글
session을 이용해 장바구니 만들기 (0) | 2019.08.23 |
---|