response对象的转发和重定向
程序员文章站
2022-04-20 22:20:59
...
1.forward的转发方式是直接转发的,而rediect则为间接转发
Forward:客户端和浏览器只发出一次请求, Servlet、 HTML、JSP或其它信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每个信息资源是共享的。
Redirect :实际是两次请求,服务器端在响应第一次请求的时候 ,让浏览器再向另外一个URL发出请求,从而达到转发的目的。
2.重定向
指客户端向服务器a发送一个请求,而服务器Servlet1经过处理返回一个新的重新定向的指令,告诉客户端你现在需要访问另一个服务器b。
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 '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>
处理登录
<%@ 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 ("wj".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>
4.转发
登录界面
<%@ 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 ("wj".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>