gpt4 book ai didi

java - 在 servlet 中获取初始化参数

转载 作者:IT老高 更新时间:2023-10-28 21:17:05 27 4
gpt4 key购买 nike

我是 servlet 的新手。我使用 getInitParameter("name") 在 init() 方法中获得了 DD 中的 init 参数。我在 doGet() 方法中尝试了很多方法来访问 init 参数,但它总是返回 null

我试过了

getServletContext().getInitParametr("name")

getServletConfig().getInitParametr("name")

但它们都返回 null。我可以在 doGet() 中获取初始化参数吗?

最佳答案

答案是 - 可以

好的,除了JB Nizet 的评论,这里还有一些建议。

1) 您是否在 Web Container 时添加了初始化参数?/Application Server正在运行吗?

引自 "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)有两种类型的初始化参数可用。 “Head First Servlets and JSP”的另一句话(强调我的):

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.


示例:

<?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>


  • 在 servlet 中访问 上下文初始化参数:
    getServlet<b>Context</b>().getInitParameter(“email”);
  • 在其所在的 servlet 中访问 servlet 初始化参数deploymentdescriptor 中定义:
    getServlet<b>Config</b>().getInitParameter("name");

获取servlet 初始化参数 的另一种方法是使用抽象类GenericServlet 中定义的方法。 :
public String getInitParameter(String name);
提供此方法是为了方便。它从 servlet 的 ServletConfig 对象中获取命名参数的值。

还有Enumeration<String> getInitParameterNames()两种方法ServletContextServletConfig获取所有初始化参数。

关于java - 在 servlet 中获取初始化参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com