标签:
参考书籍:《J2EE开源编程精要15讲》
Servlet是可以处理客户端传来的HTTP请求,并返回响应,由服务器端调用执行,有一定编写规范的Java类。
例如:
1 package test; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class Test extends HttpServlet { 12 13 public Test() { 14 super(); 15 } 16 17 public void destroy() { 18 super.destroy(); // Just puts "destroy" string in log 19 // Put your code here 20 } 21 22 //处理HTTP GET请求 23 public void doGet(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 //设定Web服务器的响应方式是HTML 26 response.setContentType("text/html"); 27 //获得PrintWriter对象,以打印输出响应 28 PrintWriter out = response.getWriter(); 29 //吧HTML形式的响应显示在浏览器上 30 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 31 out.println("<HTML>"); 32 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 33 out.println(" <BODY>"); 34 out.print(" This is "); 35 out.print(this.getClass()); 36 out.println(", using the GET method"); 37 out.println(" </BODY>"); 38 out.println("</HTML>"); 39 out.flush(); 40 out.close(); 41 } 42 //处理HTTP POST请求 43 public void doPost(HttpServletRequest request, HttpServletResponse response) 44 throws ServletException, IOException { 45 46 response.setContentType("text/html"); 47 PrintWriter out = response.getWriter(); 48 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 49 out.println("<HTML>"); 50 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 51 out.println(" <BODY>"); 52 out.print(" This is "); 53 out.print(this.getClass()); 54 out.println(", using the POST method"); 55 out.println(" </BODY>"); 56 out.println("</HTML>"); 57 out.flush(); 58 out.close(); 59 } 60 61 public void init() throws ServletException { 62 // Put your code here 63 } 64 }
Servlet需要部署在Web容器中,生命周期由Web容器管理,可分为以下几个阶段:
主要接口类:
HttpServletRequest接口提供了处理客户请求的方法,如String getParameter(String name)等(注:name为表单标签的name属性名),从客户页面窗口取得数据,如取得用户名
1 HttpServletRequest request; 2 String name=request.getParameter("username");
HttpServletResponse接口提供了以HTML页面形式把请求发给客户的方法,主要有setContentType()等,如请求转发给下一个页面
1 HttpServletResponse response; 2 response.sendRedirect("next.jsp");
HttpSession接口记录当前Servlet中的用户对话,可用于存储关于用户对话的信息,可用HttpSession接口的putValue()和getValue()等方法存取数据,通过HttpServeltRequest对象的getSession()方法可生成一个HttpSession对象,如
1 HttpServletRequest request; 2 HttpSession session=request.getSession(true);
RequestDispatcher接口可通过forward(HttpServletRequest request,HttpServletResponse response)方法把Servlet的请求提交或委派给另一个资源,如Servlet、HTML或JSP。RequestDispatcher对象可通过HttpServletRequest对象的getRequestDispatcher()方法生成。如
1 RequestDispatcher rd=request.getRequestDispathcer("reply.jsp"); 2 rd.forward(request,response);
综合使用示例:
1 package test; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 import javax.servlet.http.HttpSession; 12 13 public class Test extends HttpServlet { 14 15 public Test() { 16 super(); 17 } 18 19 public void destroy() { 20 super.destroy(); // Just puts "destroy" string in log 21 // Put your code here 22 } 23 24 //处理HTTP GET请求 25 public void doGet(HttpServletRequest request, HttpServletResponse response) 26 throws ServletException, IOException { 27 doPost(request,response); 28 } 29 //处理HTTP POST请求 30 public void doPost(HttpServletRequest request, HttpServletResponse response) 31 throws ServletException, IOException { 32 String name=request.getParameter("username"); 33 RequestDispatcher rd; 34 35 try{ 36 try{ 37 if(name.equals("Tom")){ 38 HttpSession session=request.getSession(); 39 session.setAttribute("nm", name); 40 response.sendRedirect("reply.jsp"); 41 } 42 else{ 43 rd=request.getRequestDispatcher("error.jsp"); 44 rd.forward(request, response); 45 } 46 }catch(ServletException e){ 47 e.printStackTrace(); 48 } 49 }catch(IOException e){ 50 e.printStackTrace(); 51 } 52 } 53 54 public void init() throws ServletException { 55 // Put your code here 56 } 57 }
当Servlet部署到Web服务器中时,需要在Web配置文件web.xml中声明,如:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>Login_Proj</display-name> 4 <servlet> 5 <description>This is the description of my J2EE component</description> 6 <display-name>This is the display name of my J2EE component</display-name> 7 <servlet-name>LoginServlet</servlet-name> 8 <servlet-class>login.LoginServlet</servlet-class> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet-name>LoginServlet</servlet-name> 13 <url-pattern>/LoginServlet</url-pattern> 14 </servlet-mapping> 15 <welcome-file-list> 16 <welcome-file>index.jsp</welcome-file> 17 </welcome-file-list> 18 </web-app>
假设用户表单action="LoginServlet",表单提交跳转到/LoginServlet,web.xml中<servlet-mapping>标签内<url-pattern>/LoginServlet</url-pattern>对应的<servlet-name>为LoginServlet,于是在<servlet>标签下找<servlet-name>为LoginServlet的servlet,找到对应的类login.LoginServlet。
标签:
原文地址:http://www.cnblogs.com/cqq626/p/4319247.html