如何检索<servlet><init-param>来自 Servlet 中 web.xml 的值? [英] How to retrieve <servlet><init-param> value from web.xml in Servlet?

查看:26
本文介绍了如何检索<servlet><init-param>来自 Servlet 中 web.xml 的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 xml 中检索 init-param 值到我使用以下代码的 Servlet

I need to retrieve init-param value from xml to Servlet i used following code

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
    <init-param>
        <param-name>jdbcDriver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </init-param>
</servlet>

服务代码

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    System.out.println(config.getInitParameter("jdbcDriver"));
}

但它显示为 null .. 谁能帮我做到这一点.提前致谢

But It displayed null .. could any one help me to do that . thanks in advance

推荐答案

我看不出一个原因,为什么你必须重写你的 init(ServletConfig sc) 方法,因为你总是可以通过调用您继承的 getServletConfig() 方法来获取您的 ServletConfig.

I can't see a single reason, why you have to override your init(ServletConfig sc) method, since you can always get your ServletConfig by calling your inherited getServletConfig() method.

System.out.println(getServletConfig().getInitParameter("jdbcDriver"));

这篇关于如何检索&lt;servlet&gt;&lt;init-param&gt;来自 Servlet 中 web.xml 的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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