zoukankan      html  css  js  c++  java
  • 请求转发、包含、重定向 getAttribute 和 setAttribute POST和GET编码

     一、请求转发  请求包含  请求重定向

    Demo5.java  

    注意doPost()方法中别忘写doGet(request, response);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            
            String str = "aaaaa";
            
            System.out.println("A:我想办事");
            System.out.println("B:我办不了,但我可以找人帮你办");
            
            //将非表单的数据添加到request的域中
            request.setAttribute("s", str);
            //将请求转发到demo6中
            //request.getRequestDispatcher("/servlet/demo6").forward(request, response);
            
    //注:请求转发不能跳转到其它应用,只能在本web project下跳转(例如下面的跳转到百度是不行的)
            //request.getRequestDispatcher("http://www.baidu.com").forward(request, response);
            
            //使用重定向
            //response.sendRedirect(request.getContextPath()+"/servlet/demo6");
            //可以跳转到其它应用
            //response.sendRedirect("http://www.baidu.com");
            
            System.out.println("B:事办完了");
            
            //请求包含
            request.getRequestDispatcher("/servlet/demo6").include(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }

    Demo6.java 

    注意doPost()方法中别忘写doGet(request, response);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            System.out.println("这个事我能办");
            //将request对象中的移除
            //request.removeAttribute("s");
            //从request对象中获取s的值
            String s = (String) request.getAttribute("s");
            System.out.println(s);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }

    浏览器输入:

    http://localhost:8080/day9/servlet/demo5

    请求转发和请求包含的区别

    1)相同点:

          都是多个Servlet之间共同处理一个请求,并且在请求之间公用一个request对象和response对象

    2)不同点:

          如果是请求转发的话,那么前者将不能向客户端发出响应,这一工作就由后者来完成。

          请求包含大多应用在jsp页面中,完成多页面的合并。

    请求转发就是Servlet将请求转送给其他的Servlet或服务器资源

    请求包含就是指多个Servlet共同处理一个请求

     

    请求转发和请求重定向的区别:

    转发

    (1)客户端只发送一次请求

    (2)地址栏不变

    (3)servlet1servlet2两个应用程序之间能够通过request对象共享数据

    4)不可以跳转到其它项目的应用

    重定向 

    (1)客户端发送两次请求

    (2)地址栏会发生变化

    (3)servlet1servlet2两个应用程序之间不能通过request对象共享数据

    4)可以跳转到其它应用

    使用请求转发,减少浏览器对服务器的访问次数,减轻服务器的压力

    如果明确的希望浏览器的地址栏发生变化则使用请求重定向,例如登录页面,登录成功回到主页的过程。

    二、getAttribute 和 setAttribute

    request.getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型


     request.getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型。

     setAttribute的参数是String 和 Object ,

    1.放的时候:Double res = new Double(result);//包装
    request.setAttribute("result", res);//再设置进去

    2.取的时候:Double res = (Double)request.getAttribute("result");
    double result = res.doublue();
    另外,需要注意的是使用request.setAttribute时不能使redirect而是forward。即是将请求转发而不是重定向

    三、java中getAttribute和getParameter的区别 

    getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为Object对象类型 
    getParameter表示接收参数,参数为页面提交的参数,包括:表单提交的参数、URL重写(就是xxx?id=1中的id)传的参数等,因此这个并没有设置参数的方法(没有setParameter),而且接收参数返回的不是Object,而是String类型


    HttpServletRequest类既有getAttribute()方法,也由getParameter()方法,这两个方法有以下区别

    1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法

    2)当两个Web组件之间为链接关系,被链接的组件通过getParameter()方法来获得请求参数,

     例如:假定welcome.jsp和authenticate.jsp之间为链接关系,welcome.jsp中有以下代码:

    <a  href="authenticate.jsp?username=weiqin">authenticate.jsp  </a>
    
    或者:
    
    <form  name="form1"  method="post"  action="authenticate.jsp">
       请输入用户姓名:<input  type="text"  name="username">
       <input  type="submit"  name="Submit"  value="提交">
    </form>

    authenticate.jsp中通过request.getParameter("username")方法来获得请求参数username:

    <%  String  username=request.getParameter("username");  %>

    (3)当两个Web组件之间为转发关系,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据。

    例如:  authenticate.jsp和hello.jsp之间为转发关系。authenticate.jsp希望向hello.jsp传递当前的用户名字,  如何传递这一数据呢?

    先在authenticate.jsp中调用setAttribute()方法:

    <%
    String  username=request.getParameter("username");
    request.setAttribute("username",username);
    %>
    <jsp:forward  page="hello.jsp"  />

    hello.jsp中通过getAttribute()方法获得用户名字:

    <%  String  username=(String)request.getAttribute("username");  %>
    Hello:  <%=username  %>



    从更深的层次考虑,request.getParameter()方法传递的数据,会从Web客户端传到Web服务器端,代表HTTP请求数据。request.getParameter()方法返回String类型的数据。

    request.setAttribute()和getAttribute()方法传递的数据只会存在于Web容器内部,在具有转发关系的Web组件之间共享。这两个方法能够设置Object类型的共享数据

    request.getParameter()取得是通过容器的实现来取得通过类似post,get等方式传入的数据,,  request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段,这个的确是正解.

    getAttribute是返回对象,getParameter返回字符串

    request.getAttribute()方法返回request范围内存在的对象,而request.getParameter()方法是获取http提交过来的数据。

    四、POST和GET编码

    get方式有四种:

    1. 直接在URL地址栏中输入URL。
    2. 网页中的超链接。
    3. form中method为get。
    4. form中method为空时,默认是get提交。

    post只知道有一种:form中method属性为post。

        doGet()方法:处理GET方式请求 
        doPost()方法:处理POST方式请求 

  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/expedition/p/11213370.html
Copyright © 2011-2022 走看看