jsp和servlet中實(shí)現(xiàn)頁面跳轉(zhuǎn)的方式實(shí)例總結(jié)
本文實(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)簽
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ā)送重定向請求
<% 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文件時提示下載的解決方法
- jsp引用servlet生成的驗(yàn)證碼代碼演示
- javascript與jsp發(fā)送請求到servlet的幾種方式實(shí)例
相關(guān)文章
jsp網(wǎng)頁計(jì)數(shù)器實(shí)現(xiàn)示例
網(wǎng)頁計(jì)數(shù)器想必大家都有見到過吧,記錄每一個訪問者,下面有個不錯的示例,感興趣的朋友可以參考下2014-01-01JSP 開發(fā)之Spring BeanUtils組件使用
這篇文章主要介紹了JSP 開發(fā)之Spring BeanUtils組件使用的相關(guān)資料,這里提供實(shí)例幫助大家理解如何使用Spring BeanUtils組件,需要的朋友可以參考下2017-08-08運(yùn)用JSP+ajax實(shí)現(xiàn)分類查詢功能的實(shí)例代碼
本篇文章主要介紹了運(yùn)用JSP+ajax實(shí)現(xiàn)分類查詢功能的實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07