欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于response对象的转发和重定向

程序员文章站 2022-04-20 22:20:35
...

Response对象的转发和重定向如何理解

  1. 转发( forward) 和重定向( redirect )的·区别方式就在于他们的
    转发形式。forward的转发方式是直接转发的,而rediect则为间接转发。
  2. 直接转发方式定义:客户端和浏览器只发出一次请求, Servlet、 HTML、JSP或其它信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每个信息资源是共享的。
    间接转发方式定义:实际是两次请求,服务器端在响应第一次请求的时候 ,让浏览器再向另外一个URL发出请求,从而达到转发的目的。
    写作

写作代码如下:

1:登录代码如下

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'login.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
	</head>
	<body>
		<form action="loginServlet.jsp" method="post">
			用户名
			<input type="text" name="userName" />
			</br>
			密码
			<input type="text" name="pwd" />
			</br>
			<input type="submit" value="登录" />
		</form>
	</body>
</html>

2:创建登录的代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'loginServlet.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
	</head>
	<body>
		<%
			String pwd = request.getParameter("pwd");
			String userName = request.getParameter("userName");
			if ("zhangsan".equals(userName) && "123".equals(pwd)) {
				response.sendRedirect("success.jsp");
			}
		%>
	</body>
</html>

3:打开服务器登录成功的代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'success.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<h1>
			登录成功!
		</h1>
	</body>
</html>

4:在浏览器中出入localhost:8080+自己的项目名和包名
显示界面如下
关于response对象的转发和重定向
5:进行下一步操作并输入正确格式的账号密码跳转至登录成功界面。当用户名密码正确时,loginServlet服务器做出一次响应告诉客户端需要跳转到登陆成功界面,接着客户端对success.jsp做出一次请求,最终得到success.jsp页面登录成功的响应。关于response对象的转发和重定向
6:转发界面的登录代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'login.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
	</head>
	<body>
		<%
			String message = "";
			message = (String) request.getAttribute("message");
			if (message == null)
				message = "";
		%>
		<form action="loginServlet.jsp" method="post">
			用户名
			<input type="text" name="userName" />
			</br>
			密码
			<input type="text" name="pwd" />
			<span style="color: red"><%=message%></span>
			</br>
			<input type="submit" value="登录" />
		</form>
	</body>
</html>


7:服务代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">
		<title>My JSP 'loginServlet.jsp' starting page</title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
	</head>
	<body>
		<%
			//接收客户端发送的用户名,密码
			String pwd = request.getParameter("pwd");
			String userName = request.getParameter("userName");
			//我们设定用户名为:zhangsan,密码为:123
			if ("zhangsan".equals(userName) && "123".equals(pwd)) {
				//当登录成功时跳转到登陆成功界面
				//发送重定向响应
				response.sendRedirect("success.jsp");
			} else {

				//当登陆失败时跳转到登录页面并反馈错误信息
				//转发
				//设置转发内容
				request.setAttribute("message", "密码错误!");
				//调用getRequestDispacther()方法,把请求转发给login.jsp
				request.getRequestDispatcher("login.jsp").forward(request,
						response);

			}
		%>
	</body>
</html>


这次我们在浏览器中输入错误的账号名密码点击登录,就会出现反馈信息。一个做请求操作,一个做响应操作。
关于response对象的转发和重定向