Servlet响应请求步骤:
Servlet是一个基于Java技术的Web组件,运行在服务器端,用户利用Servlet可以很轻松地扩展Web服务器的功能,使其满足特定的应用需要。Tomcat是一个常用的Servlet容器。接收客户端并请求做出响应的步骤如下:
- 客户端访问Web服务器,发送HTTP请求。
- Web服务器接收到请求后,传递给Servlet容器。
- Servlet容器加载Servlet,产生Servlet实例,并向其传递表示请求和响应的对象。
- Servlet得到客户端的请求信息,并进行相应处理。
- Servlet实例把处理结果以送回给客户端,容器负责确保响应正确送出,同时将控制返回给Web服务器。
Servlet生命周期:
- 第一阶段,装载Servlet,一般为动态执行。
- 第二阶段,创建一个Servlet实例
- 第三阶段,调用Servlet的init()方法
- 第四阶段,服务,如果容器接收到对此Servlet的请求,那么它将调用Servlet的service()方法
- 第五阶段,销毁,通过调用Servlet的destory()方法来销毁Servlet
开发Servlet程序示例:
为了简化开发,用户缩写的Servlet一般直接继承HttpServlet,HttpServlet封装了编写基于HTTP的Servlet大部分的功能。HelloWorldServlet.java文件代码如下:
package com.jsp.ch3; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloWorldServlet */ @WebServlet(description = "This is the description of my J2EE component", urlPatterns = { "/HelloWorldServlet" }) public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloWorldServlet() { super(); } public void destroy() { super.destroy(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println(" This is "); out.println(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println(" This is "); out.println(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } public void init() throws ServletException { } }
在WebContentWEB-INFweb.xml文件中描述Servlet。web.xml文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <!-- =====================开始============================================================================= --> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <!-- servlet类名称 --> <servlet-name>HelloWorldServlet</servlet-name> <!-- servlet类路径 --> <servlet-class>com.jsp.ch3.HelloWorldServlet</servlet-class> </servlet> <!-- 定义一个映射。<servlet-name>元素给出的Servlet名字必须是在<servlet>元素中声明过的Servlet的名字。 <url-pattern>元素指定对应于Servlet的URL路径,该路径是相对于Web应用程序上下文根的路径 --> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/servlet/HelloWorldServlet</url-pattern> </servlet-mapping> <!-- ==========================结束======================================================================= --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>