Servlet的一些细节

//线程安全(网页)
public class ServletDemo4 extends HttpServlet implements SingleThreadModel{

	int i = 0;
	
	//子类覆盖父类的方法,不能抛出比父类更多的异常
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		i++;
		
		try{
			Thread.sleep(1000*4);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		response.getOutputStream().write((i+"").getBytes());

		
		
//		synchronized (this) {
//			i++;
//			
//			try{
//				Thread.sleep(1000*4);
//			}
//			catch(Exception e){
//				e.printStackTrace();
//			}
//			
//			response.getOutputStream().write((i+"").getBytes());
//		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}


posted @ 2013-04-21 20:52  魅惑之眼  阅读(130)  评论(0编辑  收藏  举报