HttpServletRequest和HttpServletResponse实例
先看一下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> <servlet-name>RequestDemo</servlet-name> <servlet-class>cn.test.request.RequestDemo</servlet-class> </servlet> <servlet> <servlet-name>ResponseDemo</servlet-name> <servlet-class>cn.test.response.ResponseDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>RequestDemo</servlet-name> <url-pattern>/RequestDemo</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ResponseDemo</servlet-name> <url-pattern>/ResponseDemo</url-pattern> </servlet-mapping> </web-app>
当一个请求发送到tomcat服务器时,服务器会查询web.xml文件,先访问所有的<servlet-mapping>,匹配其中<url-pattern>,找到相应的信息后,提取<servlet-name>,然后分别匹配<servlet>中的<servlet-name>,当匹配到结果时,再根据相对应的<servlet-class>找到后台程序并执行,这就完成了一次访问过程。
requestdemo
1 public class RequestDemo extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 /** 6 * 设置参数查询的编码 7 * 该方法只能对请求实体内容的数据编码起作用。POST提交的数据在实体内容中,所以该方法对POST方法有效! 8 * GET方法的参数放在URI后面,所以对GET方式无效!!! 9 */ 10 request.setCharacterEncoding("utf-8"); 11 12 13 /* System.out.println("GET方式"); 14 //接收GET方式提交的参数 15 String value = request.getQueryString(); 16 System.out.println(value);*/ 17 18 19 /** 20 * 统一方便地获取请求参数的方法 21 */ 22 System.out.println(request.getMethod()+"方式"); 23 //getParameter(name): 根据参数名得到参数值(只能获取一个值的参数) 24 String name = request.getParameter("name"); 25 26 /** 27 * 手动重新解码(iso-8859-1 字符串-> utf-8 字符串) 28 */ 29 /*if("GET".equals(request.getMethod())){ 30 name = new String(name.getBytes("iso-8859-1"),"utf-8"); 31 }*/ 32 33 String password = request.getParameter("password"); 34 35 /*if("GET".equals(request.getMethod())){ 36 password = new String(password.getBytes("iso-8859-1"),"utf-8"); 37 }*/ 38 39 System.out.println(name+"="+password); 40 41 System.out.println("============================="); 42 Enumeration<String> enums = request.getParameterNames(); 43 while( enums.hasMoreElements() ){ 44 String paramName = enums.nextElement(); 45 46 //如果参数名是hobit,则调用getParameterValues 获取多个参数值 47 if("hobit".equals(paramName)){ 48 /** 49 * getParameterValues(name): 根据参数名获取参数值(可以获取多个值的同名参数) 50 */ 51 System.out.println(paramName+":"); 52 String[] hobits = request.getParameterValues("hobit"); 53 for(String h: hobits){ 54 /* if("GET".equals(request.getMethod())){ 55 h = new String(h.getBytes("iso-8859-1"),"utf-8"); 56 }*/ 57 System.out.print(h+","); 58 } 59 System.out.println(); 60 //如果不是hobit,则调用getParameter 61 }else{ 62 String paramValue = request.getParameter(paramName); 63 /* 64 if("GET".equals(request.getMethod())){ 65 paramValue = new String(paramValue.getBytes("iso-8859-1"),"utf-8"); 66 }*/ 67 68 System.out.println(paramName+"="+paramValue); 69 } 70 } 71 } 72 public void doPost(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException, IOException { 74 /*System.out.println("POST方式"); 75 InputStream in = request.getInputStream(); 76 byte[] buf = new byte[1024]; 77 int len = 0; 78 while( (len=in.read(buf))!=-1 ){ 79 System.out.println(new String(buf,0,len)); 80 }*/ 81 82 /** 83 * 统一方便地获取请求参数的方法 84 */ 85 /*System.out.println("POST方式"); 86 //根据参数名得到参数值 87 String name = request.getParameter("name"); 88 String password = request.getParameter("password"); 89 System.out.println(name+"="+password); 90 91 System.out.println("============================="); 92 Enumeration<String> enums = request.getParameterNames(); 93 while( enums.hasMoreElements() ){ 94 String paramName = enums.nextElement(); 95 String paramValue = request.getParameter(paramName); 96 System.out.println(paramName+"="+paramValue); 97 }*/ 98 99 //一定调用doGet方式 100 this.doGet(request, response); 101 } 102 103 }
responsedemo
public class ResponseDemo extends HttpServlet { /** * 1)tomcat服务器把请求信息封装到HttpServletRequest对象,且把响应信息封装到HttpServletResponse * 2)tomcat服务器调用doGet方法,传入request,和response对象 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 3)通过response对象改变响应信息 */ /** * 3.1 响应行 */ //response.setStatus(404);//修改状态码 //response.sendError(404); // 发送404的状态码+404的错误页面 /** * 3.2 响应头 */ response.setHeader("server", "JBoss"); /** * 3.3 实体内容(浏览器直接能够看到的内容就是实体内容) */ // response.getWriter().write("01.hello world"); //字符内容。 response.getOutputStream().write("02.hello world".getBytes());//字节内容 } /** * 4)tomcat服务器把response对象的内容转换成响应格式内容,再发送给浏览器解析。 */ }
response重定向:
1 public class ResponseDemo2 extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 /** 6 * 需求: 跳转到adv.html 7 * 使用请求重定向: 发送一个302状态码+location的响应头 8 */ 9 /*response.setStatus(302);//发送一个302状态码 10 response.setHeader("location", "/day09/adv.html"); //location的响应头 11 */ 12 13 //请求重定向简化写法 14 response.sendRedirect("/day09/adv.html"); 15 16 17 } 18 19 }
定时刷新:
1 public class ResponseDemo extends HttpServlet { 2 3 public void doGet(HttpServletRequest request, HttpServletResponse response) 4 throws ServletException, IOException { 5 /** 6 * 定时刷新 7 * 原理:浏览器认识refresh头,得到refresh头之后重新请求当前资源 8 */ 9 //response.setHeader("refresh", "1"); //每隔1秒刷新次页面 10 11 /** 12 * 隔n秒之后跳转另外的资源 13 */ 14 response.setHeader("refresh", "3;url=/day09/adv.html");//隔3秒之后跳转到adv.html 15 } 16 17 }