jsp或Servlet都会用到页面跳转,可以用
request.getRequestDispatcher("p3.jsp").forward(request,response);这种方法称为转发,地址栏上的URL不会改变;
response.sendRedirect("p3.jsp");这种方法称为重定向,地址栏的URL会改变;
这样实现跳转到p3.jsp;可是这两种方法有着本质的不同,requset是请求,是在服务器端运行的,response是相应,是在客户端运行的;

例如:
有3个页面p1.jsp , p2.jsp , p3.jsp ;
p1.jsp提交给p2.jsp , p2.jsp在转发给p3.jsp
如果在p2用第一种方法发到到p3,由于是浏览器请求的是p2,请求的一些参数被封装在request中,发给服务器,服务器用request.getRequestDispatcher("p3.jsp").forward(request,response);
转到p3,把p3发给浏览器,浏览器不知道被偷梁换柱了,所以URL还是p2.jsp;也完全可以用request.getParameter("name");得到p1.jsp中的表单控件的值,因为在这里用了forward()把request传了下去

如用第二种方法,这是浏览器主动请求了p3,所以浏览器知道请求的地址,所以URL变了,
而又一次请求,产生了另外一个request,这个和请求p2的request不同,所以在p3中用request.getParameter("name");得不到p1.jsp中的表单控件的值

总结:
1、request.getRequestDispatcher("a.jsp").forward(rquest,response); request转发 它可以保存request中的数据 页面调整 但是地址是不调整的
2、response.sendRedirect("b.jsp"); 方式是重定向 它的数据是不共享的 也就是说 request中保存的数据在b.jsp页面中是获取不到的 这种方式是表单是不能重复提交的 ,
respons跳转是可以实现跨域的 地址栏也会变化

 

 

三、response.sendRedirect(url)跳转到指定的URL地址后,上个页面(跳转之前的原来页面)中的请求全部结束,原request对象将会消亡,数据将会消失。紧接着,
当前新页面会新建request对象,即产生新的request对象。

【详细过程:redirec 会首先发一个response给浏览器,然后浏览器收到这个response后再发一个requeset给服务器,服务器接收后发新的response给浏览器。这时页面从浏览器获取来的是一个新的request。
这时,在原来跳转之前的页面用request.setAttribute存的东西都没了,如果在当前的新页面中用request.getAttribute取,得到的将会是null。】

request.getRequestDispatcher(url).forward(request,response)是采用请求转发方式,在跳转页面的时候是带着原来页面的request和response跳转的,request对象始终存在,不会重新创建。

【详细过程:forward 发生在服务器内部, 是在浏览器完全不知情的情况下发给了浏览器另外一个页面的response. 这时页面收到的request不是从浏览器直接发来的,
可能是在转页时己经用request.setAttribute在request里放了数据,在转到的页面就可以直接用request.getAttribute获得数据了。】

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>TestForward</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>com.hust.go.Login</servlet-class>
    </servlet>

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

 

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="login" method="get">
用户名:<input type="text" name="username"><br/>
密    码:<input type="text" name="passwd"><br/>
<input type="submit" value="提交"><br/>

</form>
</body>
</html>

loginResult.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>66666<%=request.getParameter("username")%></p>
</body>
</html>

 

public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
try {
            String u = req.getParameter("username");
            String p = req.getParameter("passwd");
            System.out.println("username:" + u);
            System.out.println("passwd:" + p);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        if (false) {
            System.out.println("转发");
            req.getRequestDispatcher("loginResult.jsp").forward(req,resp);
        } else {
            System.out.println("重定向");
            resp.sendRedirect("loginResult.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

 

访问网址:http://localhost:8080/TestForward/

转发后网址:http://localhost:8080/TestForward/login?username=123&passwd=8888  request.getParameter("username")为123

重定向后网址:http://localhost:8080/TestForward/loginResult.jsp   request.getParameter("username")为null