濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別

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

熱門標(biāo)簽:電銷機(jī)器人是有一些什么技術(shù) 北票市地圖標(biāo)注 四川保險(xiǎn)智能外呼系統(tǒng)商家 地圖標(biāo)注線上教程 高德地圖標(biāo)注樣式 杭州語(yǔ)音電銷機(jī)器人軟件 商洛電銷 電銷機(jī)器人好賣么 杭州ai語(yǔ)音電銷機(jī)器人功能

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

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

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

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

1.void forward(ServletRequest request,ServletResponse response)

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

2.void include(ServletRequest request,ServletResponse response)

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

如果需要把請(qǐng)求轉(zhuǎn)移到另外一個(gè)Web App中的某個(gè)地址,可以按下面的做法:
1. 獲得另外一個(gè)Web App的ServletConext對(duì)象(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");
        */
                //這個(gè)路徑必須是這樣寫,而不能像上面的request.getRequestDispatcher那樣使用相對(duì)路徑
                //  而且要是使用response.sendRedirect的話在下面的session.jsp中不能通過(guò)request.getAttribute來(lái)獲取request對(duì)象
                //因?yàn)榍昂笫褂玫牟皇峭粋€(gè)request,但是session可以,因?yàn)閟ession會(huì)一直存在直到用戶關(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 request.setAttribute()詳解及實(shí)例
  • JSP利用過(guò)濾器解決request中文亂碼問(wèn)題
  • JSP用過(guò)濾器解決request getParameter中文亂碼問(wèn)題
  • 通過(guò)過(guò)濾器(Filter)解決JSP的Post和Request中文亂碼問(wèn)題
  • java和jsp中的request使用示例
  • JSP內(nèi)置對(duì)象:Request和Response的簡(jiǎn)單介紹及使用
  • JSP request(return String)用法詳例
  • JSP XMLHttpRequest動(dòng)態(tài)無(wú)刷新及其中文亂碼處理
  • jsp 對(duì)request.getSession(false)的理解(附程序員常疏忽的一個(gè)漏洞)
  • JSP的request對(duì)象實(shí)例詳解

標(biāo)簽:宿州 西藏 青島 江西 紅河 貴州 云浮 丹東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別》,本文關(guān)鍵詞  jsp,跳轉(zhuǎn),getRequestDispatcher,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于jsp跳轉(zhuǎn)getRequestDispatcher()和sendRedirect()的區(qū)別的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    垦利县| 阜阳市| 鹤庆县| 扬中市| 盐亭县| 长葛市| 禹城市| 龙井市| 健康| 若尔盖县| 汝州市| 东光县| 平阴县| 安宁市| 肃南| 平邑县| 镇雄县| 安图县| 顺昌县| 文昌市| 凌云县| 溆浦县| 通城县| 巴东县| 顺昌县| 茶陵县| 海伦市| 朔州市| 乳源| 鹿泉市| 潞西市| 新建县| 黑山县| 福贡县| 永川市| 平顶山市| 绥芬河市| 富源县| 永和县| 青海省| 陵川县|