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