首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求教!用Servlet编写计数器有关问题!啊个位大哥!

2011-11-26 
求教!用Servlet编写计数器问题!!!!急啊个位大哥!!!代码如下importjavax.servlet.*importjavax.servlet.ht

求教!用Servlet编写计数器问题!!!!急啊个位大哥!!!
代码如下
import   javax.servlet.*;  
import   javax.servlet.http.*;  
import   java.io.*;  
import   java.util.*;  

public   class   Servlet1   extends   HttpServlet   {  
        public   Servlet1()   {  
                try   {  
                        jbInit();  
                }   catch   (Exception   ex)   {  
                        ex.printStackTrace();  
                }  
        }  

        private   static   final   String   CONTENT_TYPE   =   "text/html;   charset=GBK ";  

        //Initialize   global   variables  
        public   void   init()   throws   ServletException   {  
        }  

        //Process   the   HTTP   Get   request  
        public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)   throws  
                        ServletException,   IOException   {  
                int   i=0;  
                Integer   num=new   Integer(0);  
                response.setContentType(CONTENT_TYPE);  
                PrintWriter   out   =   response.getWriter();  

                ServletContext   context=this.getServletContext();  
                num=(Integer)context.getAttribute( "count ");  
                if(num==null)  
                {  
                        i=1;  
                        num=new   Integer(1);  

                }  
                else  
                {  
                        i=num.intValue()+1;  
                        num=new   Integer(num.intValue()+1);  
                }  
                context.setAttribute( "count ",num);  
                out.println( " <html> ");  
              out.println( " <head> <title> Servlet1 </title> </head> ");  


              out.println( " <body   bgcolor=\ "#ffffff\ "> ");  
              out.println( " <p> 您已经访问了 "+i+ "次 </p> ");  
              out.println( " </body> ");  
              out.println( " </html> ");  


        }  

        //Process   the   HTTP   Post   request  
        public   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)   throws  
                        ServletException,   IOException   {  
                doGet(request,   response);  
        }  

        //Clean   up   resources  
        public   void   destroy()   {  
        }  

        private   void   jbInit()   throws   Exception   {  
        }  
}  
JB2005编译后在WEB   VIEW显示URL:   Unable   to   open   location:   Socket   is   not   connected:   connect  
急啊!!请个位大哥务必教我要哭出来了    


[解决办法]
lz的代码在我的机器上运行没有问题。(eclipse3.2+tomcat5.5.23+jdk1.5)

输出:您已经访问了1次

不过这个计数器不是防刷新的。

[解决办法]
计数器用Listener作吧
[解决办法]
看提示啊,端口没有连接。不是代码问题啊,我这里也可以正常运行的。
请教下,XML里加上count字段的格式具体是怎么样的?忘记了,汗。
[解决办法]
直接用九大对象里面的Application对象不也可以实现吗?

Application .....javax.servlet.servletContext 你去看看

计数器
<%
int a= 0;

if(application.getAttribute( "count ")==null){
application.setAttribute( "count ";new Integer(a));
}
else{
int aa = Integer.parseInt(application.getAttribute( "count ").toString())+1;
application.setAttribute( "count ",new Integer(aa));
}

out.println( "您是 "+application.getAttribute( "count ")+ "位访问者! ");

%>
[解决办法]
直接用九大对象里面的Application对象不也可以实现吗?

Application .....javax.servlet.servletContext 你去看看

计数器
<%
int a= 0;

if(application.getAttribute( "count ")==null){
application.setAttribute( "count ";new Integer(a));
}
else{
int aa = Integer.parseInt(application.getAttribute( "count ").toString())+1;
application.setAttribute( "count ",new Integer(aa));
}

out.println( "您是 "+application.getAttribute( "count ")+ "位访问者! ");

%>

完全同意.这个是最简单的.
但也有一个小问题.就是重新启支TOMCAT之后.就得重新再来一次了

热点排行