简体   繁体   English

无法从web.xml中读取上下文参数

[英]can't read context params from web.xml

I am writing a J2EE web application with NetBeans running in Tomcat 7.0.41. 我正在使用在Tomcat 7.0.41中运行的NetBeans编写J2EE Web应用程序。 I've created a deployment descriptor, web.xml, with four context-params in it. 我创建了一个部署描述符web.xml,其中包含四个上下文参数。 I've created a class that extends HttpServlet. 我创建了一个扩展HttpServlet的类。 In the init method for the class, when I call getInitParameterNames from the ServletConfig instance, I get an empty enumeration. 在该类的init方法中,当我从ServletConfig实例调用getInitParameterNames时,我得到一个空的枚举。 Ultimately, I suspect Tomcat isn't reading the web.xml file at all because I had to resort to using the @WebServlet annotation to even reach the servlet at all. 最终,我怀疑Tomcat根本没有读取web.xml文件,因为我不得不诉诸使用@WebServlet注释甚至到达Servlet。 Can anyone suggest why I can't access the context-params and/or why Tomcat isn't reading the web.xml file? 谁能建议我为什么无法访问上下文参数和/或Tomcat为什么不读取web.xml文件?

Here is the web.xml file: 这是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">
<context-param>
    <param-name>preptimeminutes</param-name>
    <param-value>60</param-value>
</context-param>
<context-param>
    <param-name>preptimehours</param-name>
    <param-value>0</param-value>
</context-param>
<context-param>
    <param-name>servings</param-name>
    <param-value>1</param-value>
</context-param>
<context-param>
    <param-name>calories</param-name>
    <param-value>100</param-value>
</context-param>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

Here is the code for the init method: 这是init方法的代码:

@Override
public void init(ServletConfig servletConfig) throws ServletException
{
    int preptemp;
    String tempString1, tempString2;
    Enumeration<String> e = servletConfig.getInitParameterNames();

this.servletConfig = servletConfig;
    servletContext = servletConfig.getServletContext();

    try 
    {
        while(e.hasMoreElements())
        {
           servletContext.log(e.nextElement());       
        }
    } ...
}

Thanks, 谢谢,

Jason Mazzotta 杰森·马佐塔(Jason Mazzotta)

It seems that you want to read parameters of ServletContex (the ones for entire application), but you ended up with reading ServletConfig (the ones for specific servlet). 似乎您想读取ServletContex参数(用于整个应用程序的参数),但最终却要读取ServletConfig (用于特定servlet的参数)。

Another potential problem is that you are overriding init(ServletConfig) instead of init() . 另一个潜在的问题是您要覆盖init(ServletConfig)而不是init() First method shouldn't be overridden because it handles many servlet life cycle tasks like adding servlet to available servlets pool. 第一个方法不应被覆盖,因为它可以处理许多Servlet生命周期任务,例如将Servlet添加到可用的Servlet池中。 If you want to add something to initialization process you should override init() method which is invoked later by init(ServletConfig) . 如果要在初始化过程中添加一些内容,则应重写init()方法,该方法稍后由init(ServletConfig)调用。

So try maybe something like 所以尝试像

@Override
public void init() throws ServletException
{
    ServletContext servletContext = getServletContext();
    Enumeration<String> e = servletContext.getInitParameterNames();
    try 
    {
        while(e.hasMoreElements())
        {
           servletContext.log(e.nextElement());       
        }
    } ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM