jsp和servlet中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的方式實(shí)例總結(jié)
本文實(shí)例總結(jié)了jsp和servlet中實(shí)現(xiàn)頁(yè)面跳轉(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)簽
4. html標(biāo)記中的meta標(biāo)記
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ā)送重定向請(qǐng)求
<%
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"); //定向的頁(yè)面
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"); //定向的頁(yè)面
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);
}
}
希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。
- jsp頁(yè)面中獲取servlet請(qǐng)求中的參數(shù)的辦法詳解
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(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)頁(yè)實(shí)例詳解
- 基于JSP HttpServlet的詳細(xì)介紹
- JSP、Servlet中g(shù)et請(qǐng)求和post請(qǐng)求的區(qū)別總結(jié)
- Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
- 基于jsp+servlet實(shí)現(xiàn)的簡(jiǎn)單博客系統(tǒng)實(shí)例(附源碼)
- jsp+servlet+javabean實(shí)現(xiàn)數(shù)據(jù)分頁(yè)方法完整實(shí)例
- jsp+servlet+jdbc實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查
- 在jsp中用bean和servlet聯(lián)合實(shí)現(xiàn)用戶注冊(cè)、登錄
- jsp和servlet操作mysql中文亂碼問題的解決辦法
- JSP使用Servlet作為控制器實(shí)現(xiàn)MVC模式實(shí)例詳解
- 訪問JSP文件或者Servlet文件時(shí)提示下載的解決方法
- jsp引用servlet生成的驗(yàn)證碼代碼演示
- javascript與jsp發(fā)送請(qǐng)求到servlet的幾種方式實(shí)例
相關(guān)文章
web 開發(fā)之創(chuàng)建本地文件夾的實(shí)現(xiàn)方法
這篇文章主要介紹了web 開發(fā)之創(chuàng)建本地文件夾的實(shí)現(xiàn)方法的相關(guān)資料,filemanage_util.fullPath 就是創(chuàng)建文件的路徑 這是跨平臺(tái)的創(chuàng)建文件夾,需要的朋友可以參考下2017-08-08
JSP由淺入深(3)—— 通過表達(dá)式增加動(dòng)態(tài)內(nèi)容
JSP由淺入深(3)—— 通過表達(dá)式增加動(dòng)態(tài)內(nèi)容...2006-10-10
用連接池提高Servlet訪問數(shù)據(jù)庫(kù)的效率(1)
用連接池提高Servlet訪問數(shù)據(jù)庫(kù)的效率(1)...2006-10-10
JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例
這篇文章主要介紹了JSP 自定義標(biāo)簽實(shí)現(xiàn)數(shù)據(jù)字典的實(shí)例的相關(guān)資料,這里實(shí)現(xiàn)這樣的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08
JSP實(shí)現(xiàn)簡(jiǎn)單的用戶登錄并顯示出用戶信息的方法
這篇文章主要介紹了JSP實(shí)現(xiàn)簡(jiǎn)單的用戶登錄并顯示出用戶信息的方法,通過簡(jiǎn)單的登陸及登陸顯示頁(yè)面實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02

