在 servlet 中获取初始化参数 [英] Getting the init parameters in a servlet

查看:17
本文介绍了在 servlet 中获取初始化参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试过

getServletContext().getInitParametr("name")

getServletConfig().getInitParametr("name")

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

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

推荐答案

答案是 - 是的,你可以.

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

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

1) 您是否在 Web Container/应用服务器正在运行?

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

引用自 "Head First Servlets & JSP: 通过 Sun 认证 Web 组件开发人员考试":

servlet init 参数只读一次 - 当容器初始化 servlet....
当 Container 创建一个 servlet 时,它读取 DD 并为 ServletConfig 创建名称/值对.Container 再也不会读取 init 参数了!一旦参数在 ServletConfig 中,它们不会被再次读取直到/除非您重新部署 servlet.


2) 有两种类型的初始化参数可用.Head First Servlets and JSP"中的另一个引述(重点是我的):

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

上下文初始化参数(在元素中定义)和servlet初始化参数(在中定义) 元素).它们都被称为初始化参数,尽管定义在不同的元素中.

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 参数可用于任何 servlet 或 JSP,它们是当前 Web 应用程序的一部分.

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

Servlet init 参数仅可用于为其配置了 的 servlet.

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

Context init 参数在 元素中定义.

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

Servlet init 参数在 元素中为每个特定的 servlet 定义.

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 中访问上下文初始化参数:
    getServletContext().getInitParameter(email");
  • 在一个 servlet 中访问 servlet init 参数部署中定义描述符:
    getServletConfig().getInitParameter("name");
  • 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");

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

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.

还有EnumerationgetInitParameterNames() 方法用于 ServletContextServletConfig 获取所有初始化参数.

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

这篇关于在 servlet 中获取初始化参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆