package cn.aofeng.thread;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
* 一个非线程安全的Servlet演示:<br/>
* 变量<code>name</code>和<code>desc</code>被定义成实例变量。
*
* @author aofeng <a href="mailto:aofengblog@163.com>aofengblog@163.com</a>
*/
public class NotSafeThreadServlet extends HttpServlet {
private static final long serialVersionUID = -2646458389304935967L;
private String name;
private String desc;
public NotSafeThreadServlet() {
super();
}
* The doGet method of the servlet. <br>
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
String paramName = request.getParameter("name");
String paramDesc = request.getParameter("desc");
name = paramName;
desc = paramDesc;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>NotSafeThreadServlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is [");
out.print(this.toString());
out.println("], using the GET/POST method");
out.print("<br/>paramName=" + paramName);
out.print("paramDesc=" + paramDesc);
out.print("<br/>name=" + name);
out.print("desc=" + desc);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}