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

菜鸟问题,请指点!!
package   mypack;
import   javax.servlet.*;
import   javax.servlet.http.*;
import   java.io.*;
import   java.util.*;
public   class   CounterServlet   extends   HttpServlet   {
    private   static   final   String   CONTENT_TYPE   =   "text/html ";
    public   void   init(ServletConfig   config)   throws   ServletException   {
    super.init(config);
}
    public   void   doGet(HttpServletRequest   request,
        HttpServletResponse   response)
        throws   ServletException,   IOException   {
        doPost(request,   response);
    }
    public   void   doPost(HttpServletRequest   request,
        HttpServletResponse   response)
        throws   ServletException,   IOException   {
        ServletContext   context   =   getServletContext();
        Integer   count   =   (Integer)context.getAttribute( "count ");
        if   (   count   ==   null   )   {
            count   =   new   Integer(0);
            context.setAttribute( "count ",   new   Integer(0));
        }
        response.setContentType(CONTENT_TYPE);
        PrintWriter   out   =   response.getWriter();
        out.println( " <html> ");
        out.println( " <head> <title> WebCounter </title> </head> ");
        out.println( " <body> ");
        out.println( " <p> The   current   COUNT   is   :   "   +   count   +   ". </p> ");
        out.println( " </body> </html> ");
        count   =   new   Integer(count.intValue()   +   1);     //这行我不理解
        context.setAttribute( "count ",   count);
    }
    public   void   destroy()   {
    }
}

这是从书上看到的ServletContext计数的一个例子,对于这个例子我没有问题,我的问题可能是基本语法的问题。
问题在第32行,也就是有注释的那行,我不理解。
为什么这行不能写成count=count+1这种形式,count不是被定义为Integer类型的了么?

多谢高手指点!!!

------解决方案--------------------
Integer是int类型的包装类,+运算符不能用于Integer类
------解决方案--------------------
sorry,我看错问题了.

程序中count是一个对象类型,java中除了String类型可以 "+ "之外,其他的对象都不可以的

------解决方案--------------------
Integer是包装类,只有int类型的变量才能用+运算
------解决方案--------------------
转化成INTEGET
------解决方案--------------------
从context中返回的attribute只能是Object类型,所以需要强制转换成你事先放入时的类型
------解决方案--------------------
setAttribute(Strin