Servlet生命周期以及web.xml设置参数后取参一直为null

servlet生命周期

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

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

    3.调用service()方法

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

Servlet为接口,GenericServlet是一个实现Servlet接口的抽象类,HttpServlet是GenericServlet类的子类

GenericServlet类

    该类覆盖了init(ServletConfig config)方法,同时新加了一个init()方法(此方法是无参的),并且前者调用了后者。

@Override
public void init(ServletConfig config) throws ServletException {
	this.config = config;
	this.init();
}
public void init() throws ServletException {
        // NOOP by default
}

    init(ServletConfig confg)方法中this.config = config将该类的成员变量赋值,同时使得该类中的getInitParameter()方法得以使用this.config来调用ServletConfig类的getInitParameter()方法来取得初始值。所以在GenericServlet类的子类中如果要重写inti(ServletConfig config)方法则应该调用super.init(config),使之完成this.config的赋值。

HttpServlet类

    当实现该类时如果要重写init(ServletConfig config)方法则应该要调用super.init(config),但是由于其父类中有init()方法,并且父类中的init(ServletConfig)调用了init()方法,所以在HttpServlet类中直接重写init()方法可以有效避免重写init(ServlerConfig config)方法时没有调用super.init(config)造成的空指针异常。

web.xml初始化参数后得到的值一直为null

   造成这种情况的原因有很多,下面这种不是很明白原因

   在servlet3.0中新增了注解功能,并且在创建servlet时会默认使用注解,此时在web.xml中是没有<servlet></servlet>标签的,如果此时在web.xml中加入标签例如:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWebDemo2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>ServletDemo2</servlet-name>
  	<servlet-class>psl.servlet.study.ServletDemo2</servlet-class>
  	<init-param>
  		<param-name>name</param-name>
  		<param-value>happy</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletDemo2</servlet-name>
    <url-pattern>/ServletDemo2</url-pattern>
  </servlet-mapping>
 </web-app>

    servlet部分代码

/*
  webServlet注解就相当于<servlet-mapping>标签,注意此处的“/ServletDemo2”与xml中<url-pattern>是 
  一样的功能也一样
*/
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
    private ServletConfig config;
    //此处省略若干代码
    public void init()throws ServletException{
        config = getServletConfig();
        String myname=config.getInitParameter("name");
        
    }
    //此处省略若干代码
}

    由于@WebServlet中的参数与web.xml中<url-pattern>标签一样,则会报错:Servlet Tomcat v9.0 Server at localhost failed to start。如果将web.xml中的<servlet-mapping>标签整个删掉变成如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>javaWebDemo2</display-name>
  <welcome-file-list>
    <!--
        此处省略若干代码
    -->
  </welcome-file-list>
  <servlet>
  	<servlet-name>ServletDemo2</servlet-name>
  	<servlet-class>psl.servlet.study.ServletDemo2</servlet-class>
  	<init-param>
  		<param-name>name</param-name>
  		<param-value>happy</param-value>
  	</init-param>
  </servlet>
 </web-app>

则init()方法中的myname始终为null,但是config不为null,重写init(ServletConfig config)方法所得结果一样。如果在web.xml中将ServletDemo2配置一个不同于@WebServlet中的url,例如在xml中加入如下代码:

<servlet-mapping>

  <servlet-name>ServletDemo2</servlet-name>

  <url-pattern>/dsfa</url-pattern>

</servlet-mapping>

则myname值不为null。

~~~~~~~~~~~~~~~~~~知识水平有限,如有错误还望指正~~~~~~~~~~~~~~~~~~~~~~

参考:https://www.cnblogs.com/tabchanj/p/5516340.html

           https://www.cnblogs.com/xdp-gacl/p/3760336.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring MVC中,web.xml文件用于配置Spring MVC框架的相关设置。其中,可以配置如下内容: 1. 使用<bean class="org.springframework.web.servlet.DispatcherServlet"/>标签将DispatcherServlet配置为servlet。 2. 使用<context-param>标签配置Spring MVC上下文参数。 3. 使用<listener>标签配置监听器,以支持Spring MVC的事件和生命周期。 4. 使用<filter>标签配置过滤器,用于处理请求和响应。 5. 使用<servlet-mapping>标签将URL映射到DispatcherServlet。 总结起来,Spring MVC的web.xml文件主要用于配置DispatcherServlet、上下文参数、监听器和过滤器等组件,以及URL的映射。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring MVC的web.xml配置详解](https://blog.csdn.net/Mynewclass/article/details/78501604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [springmvc配置文件web.xml详解各方总结。](https://blog.csdn.net/bestone0213/article/details/49303467)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值