1、客户端跳转
在Servlet中如果要想进行客户端跳转,直接使用HttpServletResponse接口的sendRedirect()方法即可,要注意的是:此跳转只能传递session范围的属性,无法传递request范围属性。
客户端跳转————ClientRedirect.java
package com.ls.servlet_04; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ClientRedirect extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("name", "刘胜"); request.setAttribute("info", "Java Web"); response.sendRedirect("get_info.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ClientRedirect</servlet-name> <servlet-class>com.ls.servlet_04.ClientRedirect</servlet-class> </servlet> <servlet-mapping> <servlet-name>ClientRedirect</servlet-name> <url-pattern>/servlet_ClientRedirect</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
接收属性————get_info.jsp
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>getInfo</title> </head> <%request.setCharacterEncoding("GBK"); %> <body> <h2>session属性:<%=session.getAttribute("name") %></h2> <h2>request属性:<%=request.getAttribute("info") %></h2> </body> </html>
在地址栏输入“http://localhost:8080/servletdemo/servlet_ClientRedirect”(servletdemo根据自己的Myeclipse创建的工程名而定)
运行结果:
由于是客户端跳转,从运行程序结果可以发现,跳转后的地址栏是会发生变化的,只能接收session属性范围的内容,不能接收request属性范围的内容
2、服务器跳转
在Servlet中没有像JSP中<jsp:forward>指令,要想执行服务器跳转就必须依靠RequestDispatcher接口完成,此接口有两种方法:
public void forward (ServletRequest request, ServletResponse response)throws ServletException,IOException 页面跳转
public void include(ServletRequest request, ServletResponse response)throws ServletException,IOException 页面包含
使用RequestDispatcher接口的forward()方法即可完成跳转功能的实现,但是如果要想使用此接口需要使用ServletRequest接口提供的方法实例化:
public RequestDispatcher getRequestDispatcher(String path) 取得RequestDispatcher接口实例
使用服务器跳转————ServerRedirect.java
package com.ls.servlet_05; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServerRedirect extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().setAttribute("name", "刘胜"); request.setAttribute("info", "Java Web"); RequestDispatcher rd=request.getRequestDispatcher("get_info.jsp"); rd.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ServerRedirect</servlet-name> <servlet-class>com.ls.servlet_05.ServerRedirect</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServerRedirect</servlet-name> <url-pattern>/servlet_ServerRedirect</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
接收属性————get_info.jsp
<?xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>getInfo</title> </head> <%request.setCharacterEncoding("GBK"); %> <body> <h2>session属性:<%=session.getAttribute("name") %></h2> <h2>request属性:<%=request.getAttribute("info") %></h2> </body> </html>
在地址栏输入“http://localhost:8080/servletdemo/servlet_ServerRedirect”(servletdemo根据自己的Myeclipse创建的工程名而定)
运行结果:
服务器跳转后,地址栏不发生变化,而且此时尅在跳转后的JSP文件中接收到session及request范围的属性