Java Servlet
Servlet是Java开发Web应用的基础技术,封装了HTTP技术与JavaBean互相转化的复杂性
Servlet是Java程序,运行在Servlet容器之中
Servlet容器
Java EE的各种技术都有自己的容器,所谓容器,就是为其提供一个可管理的运行环境
Servlet容器将用户的请求传递给Servlet程序,并将Servlet程序的响应结果返回给用户
Servlet容器管理Servlet程序的生命周期,并提供必要的实例化对象
Servlet应用程序目录
Servlet程序必须以特定的目录结构进行部署
<root>
|- WEB-INF
|- classes
|- lib
|- web.xml Servlet部署描述符
Servlet API
java.servlet包
1. javax.servlet.Servlet
Defines methods that all servlets must implement.
所有的Servlet都必须直接或间接实现该接口
void init(ServletConfig config) throws ServletException
// 第一次请求Servlet程序时调用此方法
// Servlet容器会为其提供ServletConfig实例对象
void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
// 每次请求Servlet程序都会调用此方法
// Servlet容器将HTTP请求封装成ServletRequest实例对象,并提供一个ServletResponse实例对象以便返回响应结果
void destroy()
// 关闭Servlet程序时会调用此方法
ServletConfig getServletConfig()
// 取得init()中传入的ServletConfig实例对象
2. javax.servlet.ServletRequest
Defines an object to provide client request information to a servlet.
对于每个HTTP请求,Servlet容器都会建立一个ServletRequest实例对象
/** 取得GET/POST参数信息 **/
String getParameter(String name)
// 根据参数名称取一个值
String [] getParameterValues(String name)
// 根据参数名称取一组值
Enumeration<String> getParameterNames()
// 取得所有参数名称
/** 管理手动配置的属性 **/
void setAttribute(String name, Object o)
// 保存一组属性数据(键值对),只能通过get方法获取
Object getAttribute(String name)
// 根据属性名称取得属性值
Enumeration<String> getAttributeNames()
// 取得所有属性名称
void removeAttribute(String name)
// 删除指定的属性数据
/** 管理编码信息 **/
void setCharacterEncoding(String charset) throws UnsupportedEncodingException
// 设置编码信息
String getCharacterEncoding()
// 取得当前编码信息
String getContentType()
// 取得MIME类型信息
3. javax.servlet.ServletResponse
Defines an object to assist a servlet in sending a response to the client.
Servlet容器在建立ServletRequest实例对象的同时也会建立一个对应的ServletResponse实例对象
ServletResponse用于封装Servlet程序的响应结果
/** 向Response中写入信息 **/
PrintWriter getWriter() throws IOException
// 取得一个可将文本信息传递给客户端的实例对象
ServletOutputStream getOutputStream() throws IOException
// 取得一个可将二进制信息传递给客户端的实例对象
/** 管理编码信息 **/
void setCharacterEncoding(String charset)
// 设置编码信息
String getCharacterEncoding()
// 取得当前编码信息
void setContentType(String type)
// 设置MIME类型信息
String getContentType()
// 取得MIME类型信息
4. javax.servlet.ServletConfig
A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
ServletConfig用于保存初始配置信息
String getInitParameter(String name)
// 取得指定的初始信息
Enumeration<String> getInitParameterNames()
// 取得所有初始信息名称
ServletContext getServletContext()
// 取得ServletContext实例对象
5. javax.servlet.ServletContext
Defines a set of methods that a servlet uses to communicate with its servlet container
通过ServletContext可以访问应用范围的参数和属性
/** 管理属性信息 **/
void setAttribute(String name, Object o)
// 保存一组属性数据(键值对),只能通过get方法获取
Object getAttribute(String name)
// 根据属性名称取得属性值
Enumeration<String> getAttributeNames()
// 取得所有属性名称
void removeAttribute(String name)
// 删除指定的属性数据
/** 管理初始化参数信息 **/
boolean setInitParameter(String name, String value)
// 设置初始信息
String getInitParameter(String name)
// 取得指定的初始信息
Enumeration<String> getInitParameterNames()
// 取得所有初始信息名称
/** 应用程序相关 **/
String getContextPath()
// 取得应用程序路径信息
String getServletContextName()
// 取得应用程序名称
6. javax.servlet.GenericServlet
Defines a generic, protocol-independent servlet.
GenericServlet是一个抽象类,提供了Servlet接口的方法实现
public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
// 继承GenericServlet后只需要覆写service()即可,简化了开发流程
javax.servlet.http包
1. javax.servlet.http.HttpServlet
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site.
HttpServlet继承自javax.servlet.GenericServlet,只需要覆写doXXX()方法即可,进一步简化了开发流程
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
// 继承GenericServlet之后新增了该方法
// 用于将请求分发到对应的doXXX()方法中
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
// Called by the server (via the service method) to allow a servlet to handle a GET request.
// 处理GET请求
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
// Called by the server (via the service method) to allow a servlet to handle a POST request.
// 处理POST请求
2. javax.servlet.http.HttpServletRequest
Extends the ServletRequest interface to provide request information for HTTP servlets.
HttpServletRequest继承自javax.servlet.ServletRequest,添加了关于HTTP协议的处理
String getMethod()
// 取得请求的提交方式
String getHeader(String name)
// 取得头信息,根据名称取一个值
Enumeration<String> getHeaders(String name)
// 取得头信息,根据名称取一组值
Enumeration<String> getHeaderNames()
// 取得所有头信息名称
HttpSession getSession()
// 取得与当前请求相关的Session信息
Cookie [] getCookies()
// 取得Cookie信息
String getContextPath()
// 取得应用程序访问地址
3. javax.servlet.http.HttpServletResponse
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.
HttpServletResponse继承自javax.servlet.ServletResponse,添加了关于HTTP协议的处理
/** 响应代码 **/
void setStatus(int sc)
// 设置响应代码
int getStatus()
// 取得响应代码
/** 管理头信息 **/
void setHeader(String name, String value)
// 设置头信息
void addHeader(String name, String value)
// 添加头信息
String getHeader(String name)
// 取得头信息,根据名称取一个值
Collection<String> getHeaders(String name)
// 取得头信息,根据名称取一组值
Collection<String> getHeaderNames()
// 取得所有头信息名称
boolean containsHeader(String name)
// 判断是否存在指定头信息
/** 添加Cookie **/
void addCookie(Cookie cookie)
// 添加Cookie
void sendRedirect(String location) throws IOException
// 设置重定向
4. javax.servlet.http.HttpSession
Provides a way to identify a user across more than one page request or
visit to a Web site and to store information about that user.
因为Session是HTTP所特有的概念,所以该类并不继承Servlet包中的接口
String getId()
// 取得Session ID
/** 管理属性信息 **/
void setAttribute(String name, Object value)
// 设置session属性
Object getAttribute(String name)
// 取得Session属性
void removeAttribute(String name)
// 删除Session属性
/** 管理Session时间 **/
long getCreationTime()
// 取得Session建立时间
long getLastAccessedTime()
// 取得Session最后一次激活时间
int getMaxInactiveInterval()
// 取得Session最大空闲时间
void setMaxInactiveInterval(int interval)
// 设置Session最大空闲时间
/** 管理Session状态 **/
void invalidate()
// 销毁Session
boolean isNew()
// 判断Session是否已被使用过至少一次
部署描述符(web.xml)
部署Servlet应用程序有两种方式:
1. 使用注解(@WebServlet)
2. 使用部署描述符(web.xml)
当两种部署方式都存在并互相冲突时,部署描述符(web.xml)中的配置会覆盖注解(@WebServlet)中的配置
部署描述符的基本使用格式
<web-app>
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>pkg.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>
</web-app>
配置容器启动时编译Servlet程序
<web-app>
<servlet>
......
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
......
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>