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)用戶(hù)登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- Servlet+Jsp實(shí)現(xiàn)圖片或文件的上傳功能具體思路及代碼
- JSP+Servlet制作Java Web登錄功能的全流程解析
- JSP與Servlet的介紹說(shuō)明
- 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)用戶(hù)注冊(cè)、登錄
- jsp和servlet操作mysql中文亂碼問(wèn)題的解決辦法
- JSP使用Servlet作為控制器實(shí)現(xiàn)MVC模式實(shí)例詳解
- 訪(fǎng)問(wèn)JSP文件或者Servlet文件時(shí)提示下載的解決方法
- jsp引用servlet生成的驗(yàn)證碼代碼演示
- javascript與jsp發(fā)送請(qǐng)求到servlet的幾種方式實(shí)例
相關(guān)文章
jsp登錄會(huì)話(huà)的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇jsp登錄會(huì)話(huà)的簡(jiǎn)單實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
jsp網(wǎng)頁(yè)計(jì)數(shù)器實(shí)現(xiàn)示例
網(wǎng)頁(yè)計(jì)數(shù)器想必大家都有見(jiàn)到過(guò)吧,記錄每一個(gè)訪(fǎng)問(wèn)者,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2014-01-01
JSP 開(kāi)發(fā)之Spring BeanUtils組件使用
這篇文章主要介紹了JSP 開(kāi)發(fā)之Spring BeanUtils組件使用的相關(guān)資料,這里提供實(shí)例幫助大家理解如何使用Spring BeanUtils組件,需要的朋友可以參考下2017-08-08
運(yùn)用JSP+ajax實(shí)現(xiàn)分類(lèi)查詢(xún)功能的實(shí)例代碼
本篇文章主要介紹了運(yùn)用JSP+ajax實(shí)現(xiàn)分類(lèi)查詢(xún)功能的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07

