<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>과제</title>
</head>
<body>
<form action="add" method="post">
계산할 값을 입력 하세요. <br>
<div>
<input type="text" name="x" />
</div>
<div>
<input type="text" name="y" /><br>
</div>
<div>
<input type="submit" value="계산">
</div>
</form>
</body>
</html>
package servlet.problem;
import java.io.IOException;
mport 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 {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
response.setCharacterEncoding("utf-8");
String x_ = request.getParameter("x");
String y_ = request.getParameter("y");
int x = 0;
int y = 0;
if(x_ != null || x_.length() == 0) {
x = Integer.parseInt(x_);
}
if(y_ != null || y_.length() == 0) {
y = Integer.parseInt(y_);
}
int result = x + y;
response.getWriter().printf("결과는 %d입니다. \n", result);
}
}