Getting the init parameters in a servlet

陌路散爱 提交于 2019-11-27 04:02:49

问题


I am new to servlets. I got the init parameters in DD within the init() method using getInitParameter("name"). I tried a lot from within doGet() method to access the init parameters, but it always returns null.

I tried with

getServletContext().getInitParametr("name")

and with

getServletConfig().getInitParametr("name")

but they all return null. Can I get the init parameters in the doGet()?


回答1:


The answer is - Yes, you can.

OK, besides the JB Nizet's comment here are a few suggestions.

1) Have you added your init parameters while the Web Container / Application Server was running?

Quote from "Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam":

The servlet init parameters are read only ONCE - when the Container initializes the servlet. ...
When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig. The Container never reads the init parameters again! Once the parameters are in the ServletConfig, they won’t be read again until/unless you redeploy the servlet.


2) There are two types of init parameters available. Another quote from "Head First Servlets and JSP" (emphasis mine):

There are context init parameters (defined in <context-param> element) and servlet init parameters (defined in <init-param> element). They are both referred to as init parameters, although defined in different elements.

  • Context init parameters are available to any servlet or JSP that are part of the current web app.

  • Servlet init parameters are available to only the servlet for which the <init-param> was configured.

  • Context init parameters are defined within the <web-app> element.

  • Servlet init parameters are defined within the <servlet> element for each specific servlet.


Example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
    version="3.0">

    <display-name>Servlet testing app</display-name>

    <!-- This is a context init parameter -->
    <context-param>
        <param-name>email</param-name>
        <param-value>admin@example.com</param-value>
    </context-param>

    <servlet>
        <servlet-name>Info Servlet</servlet-name>
        <servlet-class>com.example.InfoServlet</servlet-class>
        <!-- This is a servlet init parameter -->
        <init-param>
            <param-name>name</param-name>
            <param-value>John Doe</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Info Servlet</servlet-name>
        <url-pattern>/test/ShowInfo.do</url-pattern>
    </servlet-mapping>

</web-app>


  • Accessing context init parameter in a servlet:
    getServletContext().getInitParameter(“email”);
  • Accessing servlet init parameter in a servlet for which it was defined in the deployment descriptor:
    getServletConfig().getInitParameter("name");

An alternative way of getting servlet init parameter is using a method defined in the abstract class GenericServlet:
public String getInitParameter(String name);
This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig object.

And there is also Enumeration<String> getInitParameterNames() method for both ServletContext and ServletConfig to get all init parameters.




回答2:


if you have overrided the default init() method, make sure that you pass Servlet config parameter to it and also call the super init method . cause if you dont do that , there no way that your code can find your servlet configuration.

here is the code for servlet init() code:

   public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // Rest of your code ...
    }

also i noticed that you used Servlet version 3, i am not sure if it supports defining servlet tags, so if the above solution dosen work , try to remove web-app attributes too.



来源:https://stackoverflow.com/questions/14665037/getting-the-init-parameters-in-a-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!