ContextListener导致Glassfish错误

时间:2013-10-27 17:26:01

标签: java xml servlets netbeans glassfish

我正在尝试在Netbeans中运行一个非常基本的servlet ContextListener程序。我编写得非常好,但是在尝试执行它的最后一刻,程序的输出选项卡给出了错误,如下所示..

Starting GlassFish Server 3.1.1
GlassFish Server 3.1.1 is running.
In-place deployment at C:\Users\Maunil\Documents\NetBeansProjects\ServletContextListener\build\web
Initializing...
deploy?DEFAULT=C:\Users\Maunil\Documents\NetBeansProjects\ServletContextListener\build\web&name=ServletContextListener&contextroot=/ServletContextListener&force=true failed on GlassFish Server 3.1.1 
 Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.awt.HeadlessException. Please see server.log for more details.
C:\Users\Maunil\Documents\NetBeansProjects\ServletContextListener\nbproject\build-impl.xml:721: The module has not been deployed.
See the server log for details.

这是我的 ContextListener.java 文件

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.swing.JOptionPane;

public class ContextListener implements ServletContextListener{

private ServletContext cont = null;

@Override
public void contextInitialized(ServletContextEvent sce) {
    this.cont = sce.getServletContext();
    System.out.println("Servlet Initialized....");
    JOptionPane.showMessageDialog(null,"Servlet Initialized.......!!!!!!");
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    System.out.println("Servlet Destroyed....!!!");
    JOptionPane.showMessageDialog(null,"Servlet Destroyed.......!!!!!!");
    this.cont=null;
    throw new UnsupportedOperationException("Not supported yet.");
}

}

这是 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
    <listener-class>Listener.ContextListener</listener-class>
</listener>
<servlet>
    <servlet-name>DemoServlet</servlet-name>
    <servlet-class>servlet.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DemoServlet</servlet-name>
    <url-pattern>/servlet/DemoServlet</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

Netbeans没有显示语法错误。 servlet文件是Netbeans IDE生成的默认文件。 混淆点是......当我尝试执行它而没有监听器进入 web.xml 时,servlet执行成功。但是在.xml文件中的ContextListener类条目之后,它会导致deploymet错误。

我想知道如果IDE没有显示错误。那我在哪里做错了?我是否错误地将侦听器类注册到xml文件?或者需要配置glassfish .. !!!

请引导我完成它......

提前提前...

1 个答案:

答案 0 :(得分:0)

上下文侦听器抛出java.awt.HeadlessException。这很可能是因为您尝试在无头环境中使用类javax.swing.JOptionPane,这是不允许的。

在无头servlet容器中不可能使用大多数Swing / AWT类,因为它们需要运行图形环境。