add.html 을 작성하여 GET, POST 가능하도록 포맷을 구성
(값 두개를 받아 덧셈의 값을 출력하도록)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Registration</title>
</head>
<body>
<div>
<form action="add" method="post">
<div>
<label>x : </label><input name="x" type="text">
</div>
<div>
<label>y : </label><input name="y" type="text">
</div>
<div>
<input type="submit" value="계산"/>
</div>
</form>
</div>
</body>
</html>
package com.newlecture.homework;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/add")
public class Add extends HttpServlet{
@Override
protected void service(HttpServletRequest request
, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String x_ = request.getParameter("x");
String y_ = request.getParameter("y");
int x = 0;
int y = 0;
if(x_ != null && x_ != "") {
x = Integer.parseInt(x_);
}
if(y_ != null && y_ != "") {
y = Integer.parseInt(y_);
}
out.println("덧셈 결과 = " + (x + y));
}
}