서블릿(Servlet)은 자바를 사용하여 웹페이지를 동적으로 생성하는 자바 기반의 웹 애플리케이션 프로그래
GET 방식은 URL주소에 데이터를 붙여서 전송하는 방식
Post방식으로 데이터를 전송했을 때
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>calc.html</title>
</head>
<body>
<h1>간단 계산기</h1>
<form action="/web0814/calcServlet" >
숫자1 <input type="text" name="n1">
<select name = "opr">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
숫자2 <input type ="text" name="n2">
<input type="submit" value="=">
</form>
</body>
</html>
package web0814.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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 {
String n1 = request.getParameter("n1");
String n2 = request.getParameter("n2"); //Parameter는 무조건 int
String n3 = request.getParameter("opr");
int result=0;
if(n3.equals("+")) {
result = Integer.parseInt(n1)+Integer.parseInt(n2);
response.getWriter().append(""+result);
}
else if(n3.equals("*")) {
int result2 = Integer.parseInt(n1)*Integer.parseInt(n2);
response.getWriter().append(""+result2);
}
else if(n3.equals("-")) {
int result3 = Integer.parseInt(n1)-Integer.parseInt(n2);
response.getWriter().append(""+result3);
}
else if(n3.equals("/")) {
int result4 = Integer.parseInt(n1)/Integer.parseInt(n2);
response.getWriter().append(""+result4);
}
// ""문자열 + int getWriter = out.print
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}