[英]Getting the init parameters in a servlet
I am new to servlets. 我是servlets的新手。 I got the init
parameters in DD within the init()
method using getInitParameter
("name"). 我使用getInitParameter
(“name”)在init()
方法中获得了DD中的init
参数。 I tried a lot from within doGet()
method to access the init
parameters, but it always returns null
. 我在doGet()
方法中尝试了很多来访问init
参数,但它总是返回null
。
I tried with 我试过了
getServletContext().getInitParametr("name")
and with 与
getServletConfig().getInitParametr("name")
but they all return null
. 但他们都返回null
。 Can I get the init parameters in the doGet()
? 我可以在doGet()
获取init参数吗?
The answer is - Yes, you can . 答案是 - 是的,你可以 。
OK, besides the JB Nizet's comment here are a few suggestions. 好的,除了JB Nizet的评论之外,还有一些建议。
1) Have you added your init parameters while the Web Container / Application Server was running? 1)您是否在Web容器 / 应用程序服务器运行时添加了init参数?
Quote from " Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam " : 引自“ Head First Servlets&JSP:通过Sun认证Web组件开发人员考试 ” :
The servlet init parameters are read only ONCE - when the Container initializes the servlet . servlet init参数只读取ONCE - 当Container初始化servlet时 。 ... ...
When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig. 当Container创建一个servlet时,它会读取DD并为ServletConfig创建名称/值对。 The Container never reads the init parameters again! Container永远不会再次读取init参数! Once the parameters are in the ServletConfig, they won't be read again until/unless you redeploy the servlet . 一旦参数在ServletConfig中, 除非重新部署servlet,否则不会再次读取它们 。
2) There are two types of init parameters available. 2)有两种类型的init参数可用。 Another quote from " Head First Servlets and JSP " (emphasis mine): 另外引用“ Head First Servlets and JSP ”(强调我的):
There are context init parameters (defined in
<context-param>
element) and servlet init parameters (defined in<init-param>
element). 有上下文init参数 (在<context-param>
元素中定义)和servlet init参数 (在<init-param>
元素中定义)。 They are both referred to as init parameters , although defined in different elements. 它们都被称为init参数 ,尽管在不同的元素中定义。
Context init parameters are available to any servlet or JSP that are part of the current web app. Context init参数可用于当前Web应用程序的任何 servlet或JSP。
Servlet init parameters are available to only the servlet for which the
<init-param>
was configured. Servlet init参数仅适用于配置了<init-param>
的servlet。Context init parameters are defined within the
<web-app>
element. Context init参数在<web-app>
元素中定义。Servlet init parameters are defined within the
<servlet>
element for each specific servlet . Servlet init参数在每个特定servlet的<servlet>
元素中定义。
<?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>
getServlet Context ().getInitParameter(“email”);
getServlet Config ().getInitParameter("name");
An alternative way of getting servlet init parameter is using a method defined in the abstract class GenericServlet : 获取servlet init参数的另一种方法是使用抽象类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. 它从servlet的ServletConfig对象获取命名参数的值。
And there is also Enumeration<String> getInitParameterNames()
method for both ServletContext and ServletConfig to get all init parameters. 并且还有ServletContext和ServletConfig的 Enumeration<String> getInitParameterNames()
方法来获取所有 init参数。
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 . 如果你已经覆盖了默认的init()方法,请确保将Servlet配置参数传递给它,并调用super init方法。 cause if you dont do that , there no way that your code can find your servlet configuration. 因为如果你不这样做,你的代码就无法找到你的servlet配置。
here is the code for servlet init() code: 这是servlet init()代码的代码:
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. 另外我注意到你使用的是Servlet版本3,我不确定它是否支持定义servlet标签,所以如果上面的解决方案工作,请尝试删除web-app属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.