Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NetBeans 7.2.1中没有用于映射上下文listner的web.xml文件_Java_Xml_Jakarta Ee_Servlets - Fatal编程技术网

Java NetBeans 7.2.1中没有用于映射上下文listner的web.xml文件

Java NetBeans 7.2.1中没有用于映射上下文listner的web.xml文件,java,xml,jakarta-ee,servlets,Java,Xml,Jakarta Ee,Servlets,我试图在netBeans 7.2.1中使用上下文侦听器。下面是我的实现代码: public class contextTry implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { ServletContext context=sce.getServletContext(); fetchVal o

我试图在netBeans 7.2.1中使用上下文侦听器。下面是我的实现代码:

public class contextTry implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context=sce.getServletContext();
        fetchVal obj=new fetchVal();
        Vector<RefValue> v_ref=obj.getReferenceParam();
        System.out.println("------INSIDE CONTEXT---------");
        context.setAttribute("v_Ref", v_ref);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Context destroyed");
    }
public类contextTry实现ServletContextListener{
@凌驾
public void contextInitialized(ServletContextEvent sce){
ServletContext=sce.getServletContext();
fetchVal obj=新的fetchVal();
向量v_ref=obj.getReferenceParam();
System.out.println(“----内部上下文-----------”);
setAttribute(“v_Ref”,v_Ref);
}
@凌驾
公共无效上下文已销毁(ServletContextEvent sce){
System.out.println(“上下文破坏”);
}
}

但无论何时调用参数,我都会得到一个空指针异常

public class logincontroller extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        Vector<RefValue> v=(Vector)getServletContext().getAttribute("v_ref");

        System.out.println("-----------------"+v.get(0).getStatus());
    }
公共类logincontroller扩展了HttpServlet{
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
Vector v=(Vector)getServletContext().getAttribute(“v_ref”);
System.out.println(“--------------”+v.get(0.getStatus());
}
我想我必须在web.xml文件中映射侦听器,但在netBeans 7.2.1中找不到它


请帮我找出一个解决方案!

监听器应该列在
web.xml
like中

<listener>
     <listener-class>com.yourPackage.contextTry</listener-class>
</listener>

com.yourPackage.contextTry
或者,如果您使用Servlet API 3.0或更高版本,您可以使用
@WebListener
对其进行注释,在这种情况下,您不再需要
web.xml

您还应该检查调用
obj.getReferenceParam()
是否真的返回了
Vector
的实例

一些与问题无关的注释

  • 您应该根据Java标准命名类
  • 您不应该再使用
    Vector
    对于大多数情况,
    List
    更快

我想您已经将项目设置为使用servlet spec 3.0或更高版本

默认情况下,某些IDE不会为这些版本生成
web.xml

请使用配置您的上下文侦听器