当前位置: 代码迷 >> Java相关 >> Request 跟 Response 原理
  详细解决方案

Request 跟 Response 原理

热度:390   发布时间:2016-04-22 19:16:47.0
Request 和 Response 原理

* Request 和 Response 原理:


    * request对象和response对象由服务器创建,我们只需要在service方法中使用这两个对象即可
    
    * 继承体系结构:
        ServletRequest <--继承-- HttpServletRequest <--实现--- RequestFacade

* Response:封装响应消息,设置响应消息
    * 设置响应行:HTTP/1.1 200 ok
        * setStatus(int code):设置响应状态码
            * 演示重定向:302
                //简化版的重定向
                response.sendRedirect("http://192.168.1.66:8080/zxc/one");
                
                * 实现原理
                //1.设置响应状态码为 302
                response.setStatus(302);
                //2.设置响应头 location
                response.setHeader("location", "http://192.168.1.66:8080/zxc/one");
        
    * 设置响应头:
        * setHeader(String name,String value):
        * setDateHeader(String name, long date)
        * setIntHeader(String name, int value)

    * 设置响应体:
        * getOutputStream():发送字节数据
            * 验证码:
                * 防止恶意注册
        
        * getWriter():发送字符数据
            * 发送中文数据时,需要在获取流之前设置编码方式,并且要告诉客户端浏览器,服务器使用的编码方式
                //简化版设置编码的方式
                response.setContentType("text/html;charset=utf-8");

 1 package Response; 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 ResponseTest extends HttpServlet {12 13     14     private static final long serialVersionUID = -3007244764565301466L;15 16     public void doGet(HttpServletRequest request, HttpServletResponse response)17             throws ServletException, IOException {18     /*19      * 设置响应消息体:20      *     getOutputStream():发送字节数据21      *     getWriter():发送字符数据22      */23         /*24         //设置流的编码方式25         response.setCharacterEncoding("utf-8");26         //告诉客户端,服务器使用的编码方式27         response.setHeader("content-type", "text/html;charset=utf-8");28         */29         30         //设置编码方式31         response.setContentType("text/html;charset=utf-8");32         //获取字符输出流33         PrintWriter pw = response.getWriter();//默认编码是 iso-8859-134         //设置数据35         pw.write("<h3>你好啊 ! response...</h3>");36         //刷新37         pw.flush();38     }39 40     public void doPost(HttpServletRequest request, HttpServletResponse response)41             throws ServletException, IOException {42         this.doGet(request, response);43 44     }45 46 }

 


* Request:封装请求消息,获取请求消息
    * 获取请求消息:
        * 获取请求行:GET /day08/req1?name=zs HTTP/1.1
            * getMethod():获取请求方式
            * getContextPath():获取虚拟目录
            
            * getRequestURI():获取URI(统一资源标识符)    /day07/one        /one
            * getRequestURL():获取URL(统一资源定位符)    http://localhost:8080/day07/one
            * getRemoteAddr():获取客户机的IP地址
            
            * getQueryString():get方式获取请求参数
        * 获取请求头:
            * String getHeader(String name)
            * long getDateHeader(String name)
            * int getIntHeader(String name)  
            
            

 1 package Request; 2  3 import java.io.IOException; 4  5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class RequestTest2 extends HttpServlet {11 12     private static final long serialVersionUID = -5095708647887091449L;13 14     public void doGet(HttpServletRequest request, HttpServletResponse response)15             throws ServletException, IOException {16 17         //获取请求头18         String agent = request.getHeader("user-agent");19         System.out.println(agent);20         21         if(agent.contains("MSIE")){22             System.out.println("IE  来了...");23         }else if(agent.contains("Firefox")){24             System.out.println("火狐   来了...");25         }else if(agent.contains("Chrome")){26             System.out.println("谷歌    来了...");27         }else{28             System.out.println("其他浏览器  来了...");29         }30         31         System.err.println("..............................");32     }33 34     public void doPost(HttpServletRequest request, HttpServletResponse response)35             throws ServletException, IOException {36         this.doGet(request, response);37 38     }39 40 }

 


        * 获取请求体:封装post请求参数数据
            * BufferedReader getReader():
            * ServletInputStream getInputStream():
        
        
            * 通用的获取请求参数的方式:
                * String getParameter(String name):根据键名获取值
                * String[] getParameterValues(String name):根据键名获取值的数组,一般用于复选框
                * Map<String,String[]> getParameterMap():获取所有参数的map集合
                * Enumeration getParameterNames():获取所有的键名枚举
        
        

 1 package Request; 2  3 import java.io.IOException; 4  5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class RequestTest extends HttpServlet {11 12     private static final long serialVersionUID = 4919849090529066482L;13 14     public void doGet(HttpServletRequest request, HttpServletResponse response)15             throws ServletException, IOException {16 17         //演示获取请求行数据18         19         System.out.println(request.getMethod());//获取请求发式20         System.out.println(request.getContextPath());//获取虚拟目录21         System.out.println(request.getRequestURI());//获取URI22         System.out.println(request.getRequestURL());//获取URL23         System.out.println(request.getRemoteAddr());//获取ip24         System.out.println(request.getQueryString());//获取请求参数25         System.out.println(request.getProtocol());//获取协议26         27         System.err.println("--------------------------");28         29         response.sendRedirect(request.getContextPath()+"/index.jsp");30     }31 32     public void doPost(HttpServletRequest request, HttpServletResponse response)33             throws ServletException, IOException {34         this.doGet(request, response);35 36     }37 38 }

 

 

   * 请求参数中文乱码问题:
                1.通用:
                    //1.解码  iso
                    byte[] bytes = username.getBytes("iso-8859-1");
                    //2.编码 utf-8
                    username = new String(bytes, "utf-8");
                2.post专用
                    //设置输入流的编码为 utf-8
                    request.setCharacterEncoding("utf-8");
    
    * 域对象:代表一次请求
    
    * 请求转发:
        * 转发特点:
            1.地址栏不发生变化
            2.一次请求,一次响应
            3.不能转发外部站点

 1 package Request; 2  3 import java.io.IOException; 4  5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class RequestTest7 extends HttpServlet {11 12     private static final long serialVersionUID = 3744695995118989884L;13 14     public void doGet(HttpServletRequest request, HttpServletResponse response)15             throws ServletException, IOException {16 17         System.out.println("req7 被访问了。。。");18         19         //存储数据20         request.setAttribute("msg", "hehehe...");21         22         //转发至req23         request.getRequestDispatcher("/req").forward(request, response);24     }25 26     public void doPost(HttpServletRequest request, HttpServletResponse response)27             throws ServletException, IOException {28         this.doGet(request, response);29 30     }31 32 }

 


        * 重定向的特点:
            1.地址栏发生变化
            2.两次请求,两次响应
            3.可以访问外部的站点

 1 package Response; 2  3 import java.io.IOException; 4  5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class ResponseTest2 extends HttpServlet {11 12     /**13      * 14      */15     private static final long serialVersionUID = -449184946348811489L;16 17     public void doGet(HttpServletRequest request, HttpServletResponse response)18             throws ServletException, IOException {19 20         System.out.println("resp2 被访问了。。。");21         //演示重定向22         //1.设置相应状态码 30223         response.setStatus(302);24         25         //2.设置相应头  location26         //注意: http:// 不可以少27         //response.setHeader("location", "http://192.168.1.66:8080/zxc/resp");28         29         //简化版的重定向30         response.sendRedirect("http://192.168.1.66:8080/zxc/resp");31     }32 33     public void doPost(HttpServletRequest request, HttpServletResponse response)34             throws ServletException, IOException {35         this.doGet(request, response);36 37     }38 39 }

 

  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Tue Oct 23 20:42:39 CST 2018
  • 最近登录:Tue Oct 23 20:42:39 CST 2018
  • 最近登录:Tue Oct 23 20:42:39 CST 2018
  • 最近登录:Tue Oct 23 20:42:39 CST 2018
  • 最近登录:Tue Oct 23 20:42:39 CST 2018