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

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

问题描述

我是servlets的新手。我使用 getInitParameter init()方法中的DD中获得了 init 参数C $ C>( 名称)。我在 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()中获取init参数吗?

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容器中添加了init参数/ Application Server 正在运行?

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

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


servlet init参数只读取ONCE - 当Container
时初始化servlet

...

当Container创建一个servlet时,
读取DD并为ServletConfig创建名称/值对。
Container永远不会再次读取init参数!一旦
参数在ServletConfig中,它们将不再被读取
,直到/除非你重新部署servlet


2) init参数有两种类型可用。 Head First Servlets and JSP 的另一个引用(强调我的):

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


上下文init参数(在< context-param> 元素中定义)和 servlet init参数(在中定义) < init-param> 元素)。它们都被称为 init参数,虽然在不同的元素中定义。

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.


  • 上下文 init参数可用于属于当前Web应用程序的任何 servlet或JSP。

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

Servlet 初始化参数仅适用于 c c>的servlet < init-param> 已配置。

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

上下文 init参数在<$中定义c $ c>< web-app> 元素。

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

Servlet init参数在每个特定servlet的< servlet> 元素

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>


获取 servlet init参数的另一种方法是使用抽象类中定义的方法 GenericServlet

public String getInitParameter(字符串名称);

此方法是为方便起见而提供的。它从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.

还有 Enumeration< String> <的getInitParameterNames()方法em> ServletContext ServletConfig 获取所有 init参数。

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

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

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