日期:2014-05-18  浏览次数:20669 次

写了一个Servlet统计刷新次数,Sychronized关键字出错
代码如下:
public   class   ServerInformationServlet   extends   HttpServlet   {
private   static   int   cnt=0;
public   void   init()throws   ServletException{
cnt++;
}
public   void   doGet(HttpServletRequest   request,HttpServletResponse   response)throws   ServletException,IOException{
response.setContentType( "text/html;charset=gb2312 ");
PrintWriter   out=response.getWriter();

sychronized(cnt){
cnt++;
}
out.println( " <body> ");
out.println( " <font   face=宋体   color=red   size=15> 这是您第 "+cnt+ "次访问 </font> ");

out.println( " </body> </html> ");
}

}

提示sychronized(cnt)为自定义方法

------解决方案--------------------
synchronized