session监听器HttpSessionListener接口获得在线用户运行出错,帮忙看看
//hello类
package lognumber;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class hello implements HttpSessionListener {
String name;
public hello(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sessionCreated(HttpSessionEvent se) {
test.add(name);
}
public void sessionDestroyed(HttpSessionEvent se) {
test.delete(name);
}
}
//test类
package lognumber;
import java.util.ArrayList;
public class test {
static ArrayList list=new ArrayList();
public static void add(String name){// 添加用户
list.add(name);
}
public static void delete(String name){//删除用户
list.remove(name);
}
public static ArrayList getName(){
return list;
}
}
//Servlet1
package lognumber;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import lognumber.*;
public class Servlet1 extends HttpServlet {
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 {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
request.setCharacterEncoding( "gbk ");
String name = request.getParameter( "name ");
hello he=new hello(name);
ArrayList list=test.getName();
out.println( "当前在线用户 ");
for (int i = 0; i < list.size(); i++) {
out.println(list.get(i));
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
//XML配置
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app xmlns= "http://java.sun.com/xml/ns/j2ee " xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " version= "2.4 ">
<display-name> WebModule1 </display-name>
<listener>
<listener-class> lognumber.hello </listener-class>
</listener>
<servlet>
<servlet-name> servlet1 </servlet-name>
<servlet-class> lognumber.Servlet1 </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> servlet1 </servlet-name>
<url-pattern> /servlet1 </url-pattern>
</servlet-mapping>
</web-app>
//刚一运行就提示
严重: Error configuring application listener of class lognumber.hello
java.lang.InstantiationException: lognumber.hello
at java.lang.Class.newInstance0(Class.java:335)
at java.lang.Class.newInstance(Class.java:303)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3618)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4104)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)
at org.apache.catalina.startup.Catalina.start(Catalina.java:537)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)
2007-4-2 22:39:53 org.apache.catalina.core.StandardContext listenerStart
严重: Skipped installing application listeners due to previous error(s)
2007-4-2 22:39:53 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2007-4-2 22:39:53 org.apache.catalina.core.StandardContext start
严重: Context [/WebModule1] startup failed due to previous errors
[解决办法]
当应用程序试图使用 Class 类中的 newInstance 方法创建一个类的实例,而指定的类对象因为是一个接口或是一个抽象类而无法实例化时,抛出该异常。
[解决办法]
我觉得是hello这个类没有默认的构造方法
public hello(){
this.name= " ";
}
把这个构造方法加里,看看