-
转发( forward) 和重定向( redirect )
-
Forward和Redirect代表了两种请求转发方式:直接转发和间接转发。
-
直接转发方式( Forward):客户端和浏览器只发出一次请求, Servlet、 HTML、JSP或其它信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每个信息资源是共享的。
-
间接转发方式( Redirect ):实际是两次请求,服务器端在响应第一次请求的时候 ,让浏览器再向另外一个URL发出请
求,从而达到转发的目的。
1.重定向( redirect ) 是指客户端向服务器Servlet1发送一个请求,而服务器Servlet1经过处理返回一个新的重新定向的指令,告诉客户端你现在需要访问另一个服务器Servlet2。
例如,我们编写一个能够处理登录服务的loginServlet,如果接收到客户端post请求经过loginServlet处理,如果登陆成功则跳转到登录界面
登录界面代码:
<%@ 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>
处理登录的loginServlet服务代码:
<%@ 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");
}
%>
</body>
</html>
登录成功界面代码:
<%@ 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>
接下来在浏览器中进行测试
1.在浏览器中输入post请求登录(客户端login界面向loginServlet服务器发送一次请求)2.当输入正确的用户名密码跳转到登陆成功界面(当用户名密码正确时,loginServlet服务器做出一次响应告诉客户端需要跳转到登陆成功界面,接着客户端对success.jsp做出一次请求,最终得到success.jsp页面登录成功的响应)
2.转发( forward) 是指当客户端向Servlet1发送一个请求时,Servlet1可以决定自己不错处理而转发给Servlet2做处理
例如,当输入错误的密码登陆失败时,将跳转到重新登录界面并反馈密码错误的信息。
login登录界面代码:
<%@ 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>
loginServlet服务代码:
<%@ 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>
接下来在浏览器中进行测试
1.在浏览器中输入错误的密码,点击登录(客户端login界面向loginServlet服务器发送一次请求)
2.当输入错误密码时跳转到登录页面,并且反馈出错误信息(loginServlet服务器将请求转发给login.jsp做出一次响应反馈出错误信息) 整个过程一个请求,一个响应。