类型不匹配:不能从 Integer 转换为 int
<%
//隐式对象,系统对象 jsp简化程序员的工作,已经建好的对象
//servletContext对象 net application
//当前在线人数
int num = (Integer)application.getAttribute("usernum");
num++;
application.setAttribute("usernum",new Integer(num));
%>
int num = ((Integer)application.getAttribute("usernum")).intValue();
Integer 和 int 是不同的,前者的type 是 object,后者是primitive (基本类型)。
这两者是没有隐式类型转换的
Integer to int, 用xxx.intValue()
application.getAttribute("usernum")..intValue();
或者用 Integer 而不是 int 接收。
类型 ServletContext 中的方法 setAttribute(String, Object)对于参数(String, int)不
适用
package com.control;
import javax.servlet.ServletContextEvent;
public class ApplicationListener implements javax.servlet.ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
// TODO 自动生成方法存根
System.out.println("contextDestroyed");
}
// application start
public void contextInitialized(ServletContextEvent arg0) {
// TODO 自动生成方法存根
System.out.println("contextInitialized");
arg0.getServletContext().setAttribute("usernum",0 );
}
}