一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別

1.request.getRequestDispatcher()是請求轉(zhuǎn)發(fā),前后頁面共享一個request ;
response.sendRedirect()是重新定向,前后頁面不是一個request。

2.RequestDispatcher.forward()是在服務(wù)器端運(yùn)行;
HttpServletResponse.sendRedirect()是通過向客戶瀏覽器發(fā)送命令來完成.
所以RequestDispatcher.forward()對于瀏覽器來說是“透明的”;
而HttpServletResponse.sendRedirect()則不是。

3.ServletContext.getRequestDispatcher(String url)中的url只能使用絕對路徑; 而ServletRequest.getRequestDispatcher(String url)中的url可以使用相對路徑。因?yàn)镾ervletRequest具有相對路徑的概念;而ServletContext對象無次概念。

RequestDispatcher對象從客戶端獲取請求request,并把它們傳遞給服務(wù)器上的servlet,html或jsp。它有兩個方法:

1.void forward(ServletRequest request,ServletResponse response)

用來傳遞request的,可以一個Servlet接收request請求,另一個Servlet用這個request請 求來產(chǎn)生response。request傳遞的請求,response是客戶端返回的信息。forward要在response到達(dá)客戶端之前調(diào)用,也 就是 before response body output has been flushed。如果不是的話,它會報(bào)出異常。

2.void include(ServletRequest request,ServletResponse response)

用來記錄保留request和response,以后不能再修改response里表示狀態(tài)的信息。

如果需要把請求轉(zhuǎn)移到另外一個Web App中的某個地址,可以按下面的做法:
1. 獲得另外一個Web App的ServletConext對象(currentServletContext.getContext(uripath)).

2. 調(diào)用ServletContext.getRequestDispatcher(String url)方法。

eg:ServletContext.getRequestDispatcher(“smserror.jsp”).forward(request,response);

代碼實(shí)例:
index.jsp:
復(fù)制代碼 代碼如下:
<%@ page language="Java" import="Java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

My JSP 'index.jsp' starting page
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<form action="servlet/session" method="post">
  用戶名:<input type="text" name="username" />

  密碼:<input type="password" name="password" />

  <input type="submit" />
  </form>

session.Java
復(fù)制代碼 代碼如下:
import Java.io.IOException;
import Java.io.PrintWriter;

import Javax.servlet.RequestDispatcher;
import Javax.servlet.ServletException;
import Javax.servlet.http.HttpServlet;
import Javax.servlet.http.HttpServletRequest;
import Javax.servlet.http.HttpServletResponse;
import Javax.servlet.http.HttpSession;

public class session extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public session() {
        super();
    }

    /**
     * Destruction of the servlet.

     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet.

     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * The doPost method of the servlet.

     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = "";
        String password = "";
        username = request.getParameter("username");
        password = request.getParameter("password");
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        session.setAttribute("password", password);
        request.setAttribute("name", username);
        request.setAttribute("pwd", password);

        RequestDispatcher dis = request.getRequestDispatcher("/getsession.jsp");
        dis.forward(request, response);

        /*
        response.sendRedirect("http://localhost:8080/sessiontest/getsession.jsp");
        */
                //這個路徑必須是這樣寫,而不能像上面的request.getRequestDispatcher那樣使用相對路徑
                //  而且要是使用response.sendRedirect的話在下面的session.jsp中不能通過request.getAttribute來獲取request對象
                //因?yàn)榍昂笫褂玫牟皇峭粋€request,但是session可以,因?yàn)閟ession會一直存在直到用戶關(guān)閉瀏覽器
    }

    /**
     * Initialization of the servlet.

     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

getsession.jsp:

復(fù)制代碼 代碼如下:
<%@ page language="Java" import="Java.util.*" pageEncoding="GBK"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

My JSP 'getsession.jsp' starting page

<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  <%   out.print("");   String username = (String)session.getAttribute("username");  
String password = (String)session.getAttribute("password");  
String name = (String)request.getAttribute("name");  
String pwd = (String)request.getAttribute("pwd"); 
 out.println("username " + username + " password " +password);
 //如果上面是使用response.sendRedirect的話就不能獲取到name和pwd  
 out.println("name " + name + "pwd "+ pwd);   
  %>

jsp技術(shù)jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲操综合 | 日产国产精品久久久久久 | 人人公开免费超级碰碰碰视频 | 久热爱精品视频在线观看久爱 | 福利在线小视频 | 亚洲一区精品伊人久久伊人 | 免费国产成人高清视频网站 | 国产精品成人观看视频免费 | 中文字幕一二三四区 | 亚洲国产成人最新精品资源 | 国产亚洲精品俞拍视频 | 福利在线观看视频 | 综合成人 | 欧美日韩亚洲区久久综合 | 久久免费99精品国产自在现线 | 我色网 | 91精品在线免费视频 | 99综合在线| 五月婷婷激情六月 | 一级做a爰片性色毛片男 | 美女又美女又黄又免费网站 | 欧美日本韩国一区 | 天天在线综合网 | 久久久久久91香蕉国产 | 中文字幕在线观看日韩 | 国产成人久久精品麻豆二区 | 国产日韩欧美一区二区三区视频 | 在线播放69热精品视频 | 国产日韩中文字幕 | 国产成在线观看免费视频 | 久久不见久久见免费影院 | 国产69页 | 国产色视频一区二区三区 | 曰本还a大片免费无播放器 曰本视频网络www色 | 成人精品视频一区二区三区 | fenfencao在线观看免费视频 | 91av一区| 日韩精品片 | 亚洲大片免费观看 | 91美女视频在线 | 精品国产一区二区三区免费看 |