UseServlet.java
@WebServlet("/user.do")
public class UseServlet extends HttpServlet{
private int sum=100;
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置字符集格式为utf-8
req.setCharacterEncoding("UTF-8");
String num2=req.getParameter("num2");
int n2=Integer.parseInt(num2);
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYPE>");
out.println("<html>");
out.println("<head><title>A servlet</title></head>");
out.println("<body>");
synchronized (this) {
out.println(sum+"+"+n2+"=");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sum+=n2;
out.println(sum);
}
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
index.jsp
<form action="user.do" method="post">
<table>
<tr>
<td>100+</td>
<td><input type="text" name="num2"/>=</td>
<td><input type="submit" value="计算"/></td>
</tr>
</table>
</form>
36.PNG
37.PNG
网友评论