Servlet接口

本文详细介绍了Servlet的生命周期,包括init、service、destroy方法的作用及调用时机;同时阐述了ServletConfig接口的功能,如获取servlet名称、配置参数等;并通过web.xml示例展示了如何配置servlet。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public abstract interface Servlet {
	public abstract void init(ServletConfig paramServletConfig) throws ServletException;
    init方法在容器启动的时候被调用,当load-on-startup设置为负数或者不设置时会在servlet第一次被访问的时候调用,并且只会被调用一次。
    init被调用时候会传入一个ServletConfig类型的参数,是容器传进去的。也就是servlet的配置信息
    比如web.xml,下方init-param配置的信息就是通过ServletConfig来保存。
    在定义springMvc的Servlet,指定配置文件位置的参数contextConfigLocation就是存放在ServletConfig中。

	public abstract ServletConfig getServletConfig();
    该方法用于获取Servlet的配置文件

	public abstract void service(ServletRequest paramServletRequest, ServletResponse paramServletResponse)
			throws ServletException, IOException;
    具体处理一个请求

	public abstract String getServletInfo();
    获取servlet相关的一些信息,如作者版权,这个方法需要自己实现,默认返回空字符串。

	public abstract void destroy();
	servlet销毁,或服务器关闭时候调用。只会调用一次。
}

ServletConfig接口

public abstract interface ServletConfig {
	public abstract String getServletName();
    用于获取servlet的名字,也就是web.xml中配置的servlet-name

	public abstract ServletContext getServletContext();
    返回的是这个应用本身,它里面的所有参数都可用被当前应用的所有Servlet共享。
    我们在项目中,参数可以保存在session中,也可以保存在Application中。其实保存在Application中就是保存在ServletContext中
    ServletConfig是servlet级的,一般地只是保存配置参数
    而ServletContext是Context级的,并不只是保存配置参数,还有其他的功能。

	public abstract String getInitParameter(String paramString);
    用于获取用init-param配置的参数

	public abstract Enumeration<String> getInitParameterNames();
	用于获取配置文件中所有init-param的名字的集合
}

web.xml片段:

<!-- springmvc的前端控制器 -->
<servlet>
    <servlet-name>selfstudy</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
    springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- springmvc的前端控制器 -->
<servlet>
    <display-name>initParam Demo</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>application-context.xml</param-value>
    </context-param>
    <servlet-name>selfstudy</servlet-name>
    <servlet-class>com.study.springmvn</servlet-class>
    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
    springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/*.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
这个例子:
通过context-param配置的contextConfigLocation配置到了ServletContext中
通过servlet下init-param的contextConfigLocation配置到了ServlerConfig中
在Servlet中可以分别通过它们的getInitParameter方法获取
String contextLocation = getServletConfig().getServletContext().getInitParameter("contextConfigLocation");
String servletLocation = getServletConfig().getInitParameter("contextConfigLocation");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值