servlet从web.xml中获取已配置的初始化参数为null

前几天帮同学调代码的时候发现了一个小的问题:在web.xml文件中配置了servlet的初始化参数,但是在servlet中获取初始化参数时,获取到的值却一直未null

1、servlet的三种创建方式

(1)实现Servlet接口

(2)继承GenericServlet类,它实现了Servlet接口除了service的方法

(3)继承HttpServlet方法 

2、servlet的生命周期

(1)如果是第一次访问servlet,则创建servlet实例

(2)自动调用init(ServletConfig config)方法

(3)调用service()方法

(4)终止阶段调用destory()方法

【注】可用一个类去实现Servlet接口,就可以看到这些方法

3、问题的产生及解决方法

servlet3.0以后可以采用注解驱动了,这相当于在web.xml中配置了servlet的信息。如果再在web.xml中配置,可能会产生如下问题

(1)当在web.xml中配置的url-pattern与在注解驱动中配置的url-pattern相同时,则无法启动Tomcat

 1   <servlet>
 2       <servlet-name>ParamTest</servlet-name>
 3       <servlet-class>cn.test.ParamTest</servlet-class>
 4       <init-param>
 5           <param-name>message</param-name>
 6           <param-value>hello servlet!</param-value>
 7       </init-param>
 8   </servlet>
 9   
10   <servlet-mapping>
11       <servlet-name>ParamTest</servlet-name>
12       <url-pattern>/ParamTest</url-pattern>
13   </servlet-mapping>  
 1 @WebServlet("/ParamTest")
 2 public class ParamTest extends HttpServlet {
 3     private static final long serialVersionUID = 1L;
 4        
 5     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 6         String message = this.getInitParameter("message");
 7         response.getWriter().write(message);
 8     }
 9 
10     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
11         doGet(request, response);
12     }
13 }

错误信息截图:

(2)有人可能会想,那把web.xml中的servlet-mapping删掉,用注解驱动映射可以吗?

web.xml修改如下

1 <servlet>
2       <servlet-name>ParamTest</servlet-name>
3       <servlet-class>cn.test.ParamTest</servlet-class>
4       <init-param>
5           <param-name>message</param-name>
6           <param-value>hello servlet!</param-value>
7       </init-param>
8   </servlet>

运行结果:java.lang.NullPointerException 空指针异常

注解驱动和配置驱动毕竟不是一套体系,不能混用。但是如果你在web.xml中配置的url-pattern与注解驱动的url-pattern不同,则程序运行正常。原因,Tomcat在加载程序时会先去读取项目的web.xml配置

此时可以看到地址栏中的url-pattern采用的是web.xml中的配置。

个人建议:将配置驱动和注解驱动分开写,不要混用

(1)配置驱动:web.xml和继承HttpServlet

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>test</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   <servlet>
14       <servlet-name>ParamTest</servlet-name>
15       <servlet-class>cn.test.ParamTest</servlet-class>
16       <init-param>
17           <param-name>message</param-name>
18           <param-value>hello servlet!</param-value>
19       </init-param>
20   </servlet>
21   
22   <servlet-mapping>
23       <servlet-name>ParamTest</servlet-name>
24       <url-pattern>/param</url-pattern>
25   </servlet-mapping>  
26 </web-app>
 1 public class ParamTest extends HttpServlet{
 2     private static final long serialVersionUID = 1L;
 3     
 4     @Override
 5     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 6         String message = getInitParameter("message");
 7         response.getWriter().write(message);
 8     }
 9     
10     @Override
11     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
12         doGet(request, response);
13     }
14 }

(2)注解驱动

 1 @WebServlet(
 2         urlPatterns= {"/ParamServlet","/initparam"},
 3         initParams= {@WebInitParam(name="message",value="hello servlet")})
 4 public class ParamServlet extends HttpServlet {
 5     private static final long serialVersionUID = 1L;
 6 
 7     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 8         String message = getInitParameter("message");
 9         response.getWriter().write(message);
10     }
11 
12     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13         doGet(request, response);
14     }
15 }

<!--亲测有效-->

posted @ 2019-06-25 22:48  凭栏倚窗  阅读(1355)  评论(0编辑  收藏  举报