<body>
<form action="calc" method="post">
<div>
<label>x :</label>
<input type="text" name="x" />
</div>
<div>
<label>y :</label>
<input type="text" name="y" />
</div>
<div>
<input type="submit" name="op" value="덧셈" />
<input type="submit" name="op" value="뺄셈" />
</div>
</form>
</body>
<input type="text" name="x" /> //x란 파라미터를 보냄
<input type="text" name="y" />
<input type="submit" name="op" value="덧셈" /> // op란 이름으로 덧셈이나 뺄셈이 저장되어 보내짐
<input type="submit" name="op" value="뺄셈" />
@WebServlet("/calc")
public class Calc extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
PrintWriter out = resp.getWriter();
String x_ = req.getParameter("x");
String y_ = req.getParameter("y");
String op_ = req.getParameter("op");
int x = 0;
int y = 0;
String op = "";
if(x_!=null && !x_.equals("")) x = Integer.parseInt(x_);
if(y_!=null && !y_.equals("")) y = Integer.parseInt(y_);
if(op_!=null && !op_.equals("")) op = op_;
if(op.equals("덧셈"))
out.print(x+y);
else
out.print(x-y);
}
}