请求转发&&重定向

重定向

      地址栏:

http://localhost:8080/ServletRedirectionDemo/login_success.html

服务器返回的响应消息头:Response Header

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Location: http://localhost:8080/ServletRedirectionDemo/login_success.html
Content-Type: text/html;charset=UTF-8
Content-Length: 0
Date: Thu, 29 Nov 2018 09:45:10 GMT

用法:

之前的写法
	response.setStatus(302);
	response.setHeader("Location", "login_success.html");*/
			
重定向写法: 重新定位方向 参数即跳转的位置
	response.sendRedirect("login_success.html");

Note:

1. 地址上显示的是最后的那个资源的路径地址。

2. 请求次数最少有两次, 服务器在第一次请求后,会返回302 以及一个地址, 浏览器在根据这个地址,执行第二次访问。

3. 可以跳转到任意路径。 不是自己的工程也可以跳。

4. 效率稍微低一点, 执行两次请求。 

 5. 后续的请求,没法使用上一次的request存储的数据,或者 没法使用上一次的request对象,因为这是两次不同的请求。


请求转发

     地址栏:

扫描二维码关注公众号,回复: 4362036 查看本文章
http://localhost:8080/ServletRedirectionDemo/Login?username=admin&password=12345

     服务器返回的响应消息头:Response Header

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/"148-1543483949046"
Last-Modified: Thu, 29 Nov 2018 09:32:29 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 148
Date: Thu, 29 Nov 2018 09:46:44 GMT

    请求转发的写法: 参数即跳转的位置

request.getRequestDispatcher("login_success.html").forward(request, response);

    Note:

        1. 地址上显示的是请求servlet的地址。  返回200 ok

        2. 请求次数只有一次, 因为是服务器内部帮客户端执行了后续的工作。 

        3. 只能跳转自己项目的资源路径 。  

        4. 效率上稍微高一点,因为只执行一次请求。 

        5. 可以使用上一次的request对象。 


实例:ServletRedirectionDemo

package it.cast.redirection.demo;

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 RedirectionDemo extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	//中文输出乱码的处理
	response.setContentType("text/html;charset=UTF-8");
	//获取请求的数据
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	
	if("admin".equals(username)&&"12345".equals(password)) {
		//之前跳转页面的写法
//		response.setStatus(302);
//		response.setHeader("Location", "login_success.html");
	
		//重定向
//		response.sendRedirect("login_success.html");
		
		//请求转发
		request.getRequestDispatcher("login_success.html").forward(request, response);
		
	}else{
		response.getWriter().write("登录失败");
	}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

登录页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>登录页面</h1><br/>
	<form action="Login"method="get">
		账号:<input type="text" name="username"/><br/>
		密码:<input type="text" name="password"/><br/>
		<input type="submit" value="登录"/>
	</form>
</body>
</html>

登录成功页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>登录成功</h1>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/TDOA1024/article/details/84768972