主頁 > 知識庫 > jsp和servlet中實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式實(shí)例總結(jié)

jsp和servlet中實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式實(shí)例總結(jié)

熱門標(biāo)簽:Linux服務(wù)器 Mysql連接數(shù)設(shè)置 電子圍欄 服務(wù)器配置 阿里云 團(tuán)購網(wǎng)站 銀行業(yè)務(wù) 科大訊飛語音識別系統(tǒng)

本文實(shí)例總結(jié)了jsp和servlet中實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式。分享給大家供大家參考,具體如下:

假設(shè)要求從test1.jsp 跳轉(zhuǎn)到test2.jsp

一. jsp中跳轉(zhuǎn):

1. 使用RequestDispatcher.forward方法轉(zhuǎn)發(fā)

%
 RequestDispatcher rd = getServletContext().getRequestDispatcher("/test/test2.jsp"); 
 rd.forward(request, response); 
%>

2. response.sendRedirect 重定向

%
  response.sendRedirect("test2.jsp");
%>

3.  使用forward標(biāo)簽

復(fù)制代碼 代碼如下:
jsp:forward page="test2.jsp"/>

4. html標(biāo)記中的meta標(biāo)記

復(fù)制代碼 代碼如下:
meta http-equiv="refresh" content="0; url=test2.jsp">

5. 使用response.setHeader

%
int stayTime=0;
String URL="test2.jsp";
String content=stayTime+";URL="+URL; 
response.setHeader("REFRESH",content);
%>

6. 使用response.setHeader和response.setStatus 發(fā)送重定向請求

%
 response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
 String newLocation = "test2.jsp"; 
 response.setHeader("Location",newLocation); 
%>

7. 使用javascript腳本

script type="text/javascript">
window.location.href="test2.jsp";
/script>

二. servlet中跳轉(zhuǎn):

假設(shè) 從 servlet中跳轉(zhuǎn)到test2.jsp

1. forward

ServletContext sc = getServletContext(); 
RequestDispatcher rd = sc.getRequestDispatcher("/test/test2.jsp"); //定向的頁面 
rd.forward(request, response);
public class ForwardServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 String id = request.getParameter("id");
 response.setContentType("text/html; charset=gb2312"); 
 ServletContext sc = getServletContext(); 
 RequestDispatcher rd = sc.getRequestDispatcher("/test/test2.jsp"); //定向的頁面 
 rd.forward(request, response); 
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 doGet(request, response);
 }
}

2. sendRedirect

package com.yanek.test;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RedirectServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 String id = request.getParameter("id");
 response.setContentType("text/html; charset=gb2312"); 
 response.sendRedirect("test/test2.jsp");
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 doGet(request, response);
 }
}

希望本文所述對大家JSP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • jsp頁面中獲取servlet請求中的參數(shù)的辦法詳解
  • JavaWeb實(shí)現(xiàn)用戶登錄注冊功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
  • Servlet+Jsp實(shí)現(xiàn)圖片或文件的上傳功能具體思路及代碼
  • JSP+Servlet制作Java Web登錄功能的全流程解析
  • JSP與Servlet的介紹說明
  • Servlet與JSP間的兩種傳值情況
  • JSP+Servlet+JavaBean實(shí)現(xiàn)登錄網(wǎng)頁實(shí)例詳解
  • 基于JSP HttpServlet的詳細(xì)介紹
  • JSP、Servlet中g(shù)et請求和post請求的區(qū)別總結(jié)
  • Servlet+JavaBean+JSP打造Java Web注冊與登錄功能
  • 基于jsp+servlet實(shí)現(xiàn)的簡單博客系統(tǒng)實(shí)例(附源碼)
  • jsp+servlet+javabean實(shí)現(xiàn)數(shù)據(jù)分頁方法完整實(shí)例
  • jsp+servlet+jdbc實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查
  • 在jsp中用bean和servlet聯(lián)合實(shí)現(xiàn)用戶注冊、登錄
  • jsp和servlet操作mysql中文亂碼問題的解決辦法
  • JSP使用Servlet作為控制器實(shí)現(xiàn)MVC模式實(shí)例詳解
  • 訪問JSP文件或者Servlet文件時(shí)提示下載的解決方法
  • jsp引用servlet生成的驗(yàn)證碼代碼演示
  • javascript與jsp發(fā)送請求到servlet的幾種方式實(shí)例

標(biāo)簽:大理 廣元 衢州 衡水 棗莊 蚌埠 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp和servlet中實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式實(shí)例總結(jié)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266