我正在尝试在 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 类条目之后会导致部署错误。
我想知道IDE是否没有显示错误。那我在哪里做错了?我是否将侦听器类错误地注册到 xml 文件?或者需要配置 glassfish..!!!
请指导我完成它...
提前谢谢...