java web里面页面跳转的方法总结

 在Java Web开发中,常常会用到跳转页面的方法,通常有下面两种方法。html

Java代码 java

HttpServletResponse response = new HttpServletResponse();  
response.sendRedirect(location)  

Java代码web

RequestDispatcher rd = new RequestDispatcher();  
rd.forward(request, response)   

 

  1. 跳转方式 
    http://localhost:8080/Test应用 
    运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可让你重定向到任何URL。 
    表单form的action= "/uu ";sendRedirect( "/uu ");表示相对于服务器根路径。如http://localhost:8080/Test应用(则提交至http://localhost:8080/uu); 
    Forward代码中的 "/uu "则表明相对与WEB应用的路径。如http://localhost:8080/Test应用(则提交至http://localhost:8080/Test/uu);
  2. (运用RequestDispatcher接口的Forward)方法 
    forward()没法重定向至有frame的jsp文件,能够重定向至有frame的html文件, 
    同时forward()没法在后面带参数传递,好比servlet?name=frank,这样不行,能够程序内经过response.setAttribute( "name ",name)来传至下一个页面. 
    重定向后浏览器地址栏URL不变. 

    只有在客户端没有输出时才能够调用forward方法。若是当前页面的缓冲区(buffer)不是空的,那么你在调用forward方法前必须先清空缓冲区。 
    "/ "表明相对与web应用路径 (注意最好带"/"开始,不然认为路径相对于原来的请求开始,容易出错)

    RequestDispatcher   rd   =   request.getRequestDispatcher( "/ooo "); 
    rd.forward(request,   response);提交至http://localhost:8080/Test/ooo 

    RequestDispatcher   rd   =   getServletContext().getRequestDispatcher( "/ooo "); 
    rd.forward(request,   response);提交至http://localhost:8080/Test/ooo 

    RequestDispatcher   rd   =getServletContext().getNamedDispatcher( "TestServlet ");(TestServlet为一个 <servlet-name> ) 
    rd.forward(request,   response);提交至名为TestServlet的servlet 

    若是在 <jsp:forward> 以前有不少输出,前面的输出已使缓冲区满,将自动输出到客户端,那么该语句将不起做用,这一点应该特别注意。 
    另外要注意:它不能改变浏览器地址,刷新的话会致使重复提交 
    从http://localhost:8080/Test/gw/page.jsp中转发 
    <jsp:forward   page= "OtherPage.jsp "/> 在JSP页面被解析后转换成pageContext.forward( "OtherPage.jsp "); 
    "/OtherPage.jsp "提交到http://localhost:8080/Test/OtherPage.jsp 
    "OtherPage.jsp "提交到http://localhost:8080/Test/gw/OtherPage.jsp 


    (运用HttpServletResponse接口的sendRedirect)方法302 
    是在用户的浏览器端工做,sendRedirect()能够带参数传递,好比servlet?name=frank传至下个页面, 
    同时它能够重定向至不一样的主机上,sendRedirect()能够重定向有frame.的jsp文件. 

    假设转发代码包含于注册的servlet-url为/ggg/tt;jsp为/ggg/tt.jsp: 
    绝对路径:response.sendRedirect( "http://www.brainysoftware.com ")发送至http://www.brainysoftware.com 
    根路径:response.sendRedirect( "/ooo ")发送至http://localhost:8080/ooo 
    相对路径:response.sendRedirect( "ooo ")发送至http://localhost:8080/Test/ggg/ooo, 

    sendRedirect等同于此方式 
    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); 
    String   newLocn   =   "/newpath/jsa.jsp "; 
    response.setHeader( "Location ",newLocn); 


    (Meta   Refresh)方法200 
    这种方法是由HTML提供的,Meta自己就是HTML标签。使用方法是: <meta   http-equiv= "refresh "   content= "5;   url=http://www.dreamdu.com/ "   /> 
    相应的java代码 
    String   content=stayTime+ ";URL= "+URL; 
    response.setHeader( "REFRESH ",content);
  3. 使用response.sendRedirect()地址栏将改变 
    使用request.getRequestDispatcher().forward(request,response)地址栏中的信息保持不变
  4. request.setAttribute存的东西
    只用经过方法2跳转   才能在新页取出来
  5. redirect   会首先发一个response给浏览器,   而后浏览器收到这个response后再发一个requeset给服务器,   而后服务器发新的response给浏览器.   这时页面收到的request是一个新从浏览器发来的.
    forward   发生在服务器内部,   在浏览器彻底不知情的状况下发给了浏览器另一个页面的response.   这时页面收到的request不是从浏览器直接发来了,可能己经放了数据.
    因此: 
            request.setAttribute存的东西
            只用经过方法2跳转   才能在新页取出来

示例:项目的路径以下,能够看得出login.jsp在WEB-INF下面,而index.jsp在和WEB-INF同一级浏览器

我要实现从index.jsp跳转到login.jsp里面服务器

index.jsp:app

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> 
<%response.sendRedirect(request.getContextPath()+"/login");%>

servlet.java以下:jsp

注意:"/"就表示是项目名,其实就是WebContentui

public class LoginController extends HttpServlet{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		System.out.println("enter to doGet");
		//注意在字符串里面//表示一个/
		RequestDispatcher view=request.getRequestDispatcher("/WEB-INF/login.jsp");
		view.forward(request,response);
	}
}

web.xmlurl

<web-app 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_3_0.xsd"
	version="3.0" metadata-complete="true">

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<servlet>
		<servlet-name>Login Servlet</servlet-name>
		<servlet-class>com.kedacom.servletdemo.controller.LoginController</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>Login Servlet</servlet-name>
		<url-pattern>/login</url-pattern>
	</servlet-mapping>
	
	
</web-app>

便可实现从index.jsp跳转到login.jsp页面spa

注意:

response.sendRedirect(request.getContextPath()+"/list"); 必须加项目路径,否则就没有项目名了,且这个默认为后面发出的请求为GET方法。

重定向请求为:http://localhost:8080/OriginalServletDemo/list

而RequestDispatcher view = request .getRequestDispatcher("/list");
       view.forward(request, response);

而这儿路径"/" 就自动有加项目名字,因此就直接"/list"就好了,并且要注意的是后面处理/list的servlet的处理究竟是doGet仍是doPost,是有调用这个RequestDispatcher是doPost仍是doGet决定的。

相关文章
相关标签/搜索