若想让Servlet正确地运行在服务器中并处理请求信息,必须进行适当的配置,关于Servlet的配置主要有两种方式,
分别是通过Web应用的配置文件web.xml来完成配置和使用@WebServlet注解的方式完成。
<servlet>
<servlet-name>HelloServletservlet-name>
<servlet-class>web.controller.HelloServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>HelloServletservlet-name>
<url-pattern>/servlet/HelloServleturl-pattern>
servlet-mapping>
@WebServlet(name = "HelloServlet",urlPatterns = "/HelloServlet")
public class HelloServlet extends HttpServlet{
//处理GET方法请求的方法
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {}
//处理POST方法请求的方法
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {}
}
03servlet两种配置方式_哔哩哔哩_bilibili
当客户端向Servlet容器发出HTTP请求访问Servlet时,Servlet容器首先会解析请求,检查内存中是否已经有
了该Servlet对象,如果有,直接使用该Servlet对象;如果没有,就创建Servlet实例对象,然后通过调用init()
方法完成Servlet的初始化。需要注意的是,在Servlet的整个生命周期内,它的init()方法只被调用一次。
这是Servlet生命周期中最重要的阶段,在这个阶段,Servlet容器会为客户端请求创建代表HTTP请求的
ServletRequest对象和代表HTTP响应的ServletResponse对象,然后将它们作为参数传递给
Servlet的service()方法。service()方法从ServletRequest对象中获得客户端请求信息并处理该请求,
通过ServletResponse对象生成响应结果。在Servlet的整个生命周期内,对于Servlet的每一次访问请求,
Servlet容器都会调用一次Servlet的service()方法,并且创建新的ServletRequest和ServletResponse对象,
也就是说,service()方法在Servlet的整个生命周期中会被调用多次。
当服务器关闭或web应用被移除出容器时,Servlet随着web应用的销毁而销毁。在销毁Servlet之前,Servlet容器
会调用Servlet的destroy()方法,以便让Servlet对象释放它所占用的资源。在Servlet的整个生命周期中,
destroy()方法也只被调用一次。
需要注意的是,Servlet对象一旦创建就会驻留在内存中等待客户端的访问,直到服务器关闭,或web应用被移除
出容器时Servlet对象才会销毁。
示例:
package com.miao;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 配置servlet 采用注解的方式
//@WebServlet(name = "ServletDemo03" , value = "/ServletDemo03")
//采用web.xml的方式配置
public class ServletDemo03 extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("init方法执行");
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("service方法执行了!!!!");
}
@Override
public void destroy() {
System.out.println("destroy方法执行了!!!!");
}
}
<servlet>
<servlet-name>ServletDemo03servlet-name>
<servlet-class>com.miao.ServletDemo03servlet-class>
servlet>
<servlet-mapping>
<servlet-name>ServletDemo03servlet-name>
<url-pattern>/ServletDemo03url-pattern>
servlet-mapping>
04servlet的生命周期_哔哩哔哩_bilibili
在Servlet运行期间,经常需要一些配置信息,例如,文件使用的编码等,这些信息都可以在@WebServlet注解
的属性中配置。当Tomcat初始化一个Servlet时,会将该Servlet的配置信息封装到一个ServletConfig对象中,
通过调用init(ServletConfig config)方法将ServletConfig对象传递给Servlet。ServletConfig定义了一系列
获取配置信息的方法。
方法说明 | 功能描述 |
---|---|
String getInitParameter(String name) | 根据初始化参数名返回对应的初始化参数值 |
Enumeration getInitParameterNames() | 返回一个Enumeration对象,其中包含了所有的初始化参数名 |
ServletContext getServletContext() | 返回一个代表当前Web应用的ServletContext对象 |
String getServletName() | 返回Servlet的名字 |
package com.miao;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
// 配置初始化参数
//配置单个参数
//@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
// initParams = @WebInitParam(name = "encoding" , value = "UTF-8"))
//配置多个参数
@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
initParams = {@WebInitParam(name = "encoding", value = "UTF-8"),
@WebInitParam(name="name" , value = "zhangsan")}
)
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String encoding = this.getServletConfig().getInitParameter("encoding");
System.out.println(encoding);
String name = this.getServletConfig().getInitParameter("name");
System.out.println(name);
// 看一看其他属性里边是什么内容
// servletContext
ServletConfig servletConfig = this.getServletConfig();
ServletContext servletContext = servletConfig.getServletContext();
System.out.println(servletContext.toString());
// initParameterNames
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String element = initParameterNames.nextElement();
System.out.print(element + "==>");
String parameter = servletConfig.getInitParameter(element);
System.out.println(parameter);
}
String servletName = servletConfig.getServletName();
System.out.println(servletName);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
05servletconfig接口_哔哩哔哩_bilibili
当Servlet容器启动时,会为每个Web应用创建一个唯一的ServletContext对象代表当前Web应用。ServletContext
对象不仅封装了当前Web应用的所有信息,而且实现了多个Servlet之间数据的共享。
在web.xml文件中,可以配置Servlet的初始化信息,还可以配置整个Web应用的初始化信息。Web应用初始化参
数的配置方式具体如下所示。
元素位于根元素中,它的子元素和分别用来
指定参数的名字和参数值。可以通过调用ServletContext接口中定义的getInitParameterNames()和
getInitParameter(String name)方法,分别获取参数名和参数值。
由于一个Web应用中的所有Servlet共享同一个ServletContext对象,所以ServletContext对象的域属性
可以被该Web应用中的所有Servlet访问。ServletContext接口中定义了用于增加、删除、设置
ServletContext域属性的四个方法。
ServletContext接口定义了一些读取Web资源的方法,这些方法是依靠Servlet容器来实现的。
Servlet容器根据资源文件相对于Web应用的路径,返回关联资源文件的IO流、资源文件在文件系统的绝对路径
等。
<context-param>
<param-name>参数名param-name>
<param-value>参数值param-value>
context-param>
<context-param>
<param-name>参数名param-name>
<param-value>参数值param-value>
context-param>
package com.miao;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
// 配置初始化参数
//配置单个参数
//@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
// initParams = @WebInitParam(name = "encoding" , value = "UTF-8"))
//配置多个参数
//@WebServlet(name = "ServletDemo06", value = "/ServletDemo06",
// initParams = {@WebInitParam(name = "encoding", value = "UTF-8"),
// @WebInitParam(name="name" , value = "zhangsan")}
//)
public class ServletDemo06 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ServletContext context = this.getServletContext();
Enumeration<String> initParameterNames = context.getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String element = initParameterNames.nextElement();
System.out.print(element + " ==> ");
System.out.println(context.getInitParameter(element));
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
方法说明 | 功能描述 |
---|---|
Enumeration getAttributeNames() | 返回一个Enumeration对象,该对象包含了所有存放在ServletContext中的所有域属性名 |
Object getAttibute(String name) | 根据参数指定的属性名返回一个与之匹配的域属性值 |
void removeAttribute(String name) | 根据参数指定的域属性名,从ServletContext中删除匹配的域属性 |
void setAttribute(String name,Object obj) | 设置ServletContext的域属性,其中name是域属性名,obj是域属性值 |
package com.miao;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
// 配置初始化参数
//配置单个参数
//@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
// initParams = @WebInitParam(name = "encoding" , value = "UTF-8"))
//配置多个参数
//@WebServlet(name = "ServletDemo06", value = "/ServletDemo06",
// initParams = {@WebInitParam(name = "encoding", value = "UTF-8"),
// @WebInitParam(name="name" , value = "zhangsan")}
//)
public class ServletDemo06 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ServletContext context = this.getServletContext();
Enumeration<String> initParameterNames = context.getInitParameterNames();
while (initParameterNames.hasMoreElements()) {
String element = initParameterNames.nextElement();
System.out.print(element + " ==> ");
System.out.println(context.getInitParameter(element));
}
context.setAttribute("cookie","123456");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.miao;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
// 配置初始化参数
//配置单个参数
//@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
// initParams = @WebInitParam(name = "encoding" , value = "UTF-8"))
//配置多个参数
//@WebServlet(name = "ServletDemo06", value = "/ServletDemo06",
// initParams = {@WebInitParam(name = "encoding", value = "UTF-8"),
// @WebInitParam(name="name" , value = "zhangsan")}
//)
public class ServletDemo07 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ServletContext context = this.getServletContext();
Object cookie = context.getAttribute("cookie");
resp.getWriter().println( "cookie ==> " + cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
方法说明 | 功能描述 |
---|---|
Set getResourcePaths(String path) | 返回一个Set集合,集合中包含资源目录中子目录和文件的路径名称。参数path必须以正斜线(/)开始,指定匹配资源的部分路径 |
String getRealPath(String path) | 返回资源文件在服务器文件系统上的真实路径(文件的绝对路径)。参数path代表资源文件的虚拟路径,它应该以正斜线(/)开始,“/”表示当前Web应用的根目录,如果Servlet容器不能将虚拟路径转换为文件系统的真实路径,则返回null |
URL getResource(String path) | 返回映射到某个资源文件的URL对象。参数path必须以正斜线(/)开始,“/”表示当前Web应用的根目录 |
InputStream getResourceAsStream(String path) | 返回映射到某个资源文件的InputStream输入流对象。参数path传递规则和getResource()方法完全一致 |
package com.miao;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
// 配置初始化参数
//配置单个参数
//@WebServlet(name = "ServletDemo05", value = "/ServletDemo05",
// initParams = @WebInitParam(name = "encoding" , value = "UTF-8"))
//配置多个参数
//@WebServlet(name = "ServletDemo06", value = "/ServletDemo06",
// initParams = {@WebInitParam(name = "encoding", value = "UTF-8"),
// @WebInitParam(name="name" , value = "zhangsan")}
//)
public class ServletDemo08 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
ServletContext context = this.getServletContext();
InputStream stream = context.getResourceAsStream("/WEB-INF/classes/demo.properties");
Properties properties = new Properties();
properties.load(stream);
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
06ServletContext接口_哔哩哔哩_bilibili