response对象的转发和重定向
Forward和Redirect代表了两种请求转发方式:直接转发和间接转发
直接转发方式(Forward),客户端和浏览器只发出一次请求,Servlet、HTML
、JSP或其他信息资源,由第二个信息资源响应该请求,在请求对象request中,保存的对象对于每一个信息资源是共享的。
间接转发方式(Redirect)实际是两次请求
,服务器端在响应第一次请求的时候,让浏览器在向另外一个URL发出请求,从而达到转发的目的。
举个实际例子解释下转发和重定向
现在有四个同学他们分别叫小红、小芳、小明、小李。
转发:有一天小明问小李借1000块钱,小李同意了。但是小李手里没那么多钱,于是他就去找小红借了300块然后给了小明。
小明最后还钱是需要还给小李的,至于小红跟小明没有任何关系,在整个借钱过程中小明就向小李发出了一次借钱的请求!
重定向:有一天小明问小李借1000块钱*(第一次请求),小李说我没有那么多钱,但是小芳有,你去找小芳借吧。然后小明找到小芳然后说:“你借我1000块钱吧。”(第二次请求)然后小芳给了他1000块钱。
小明最后还钱是需要还给小芳的,他一共发出了两次请求!!第一次他是找小李借,小李推荐说小芳有。然后第二次他找到小芳发出第二次请求
接下来是代码实现转发和重定向的过程
DBUtil工具类
package org.work.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static {
try {
// 加载jdbc驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
// 使用DriverManager建立数据库连接
conn = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=consumer",
"sa", "1");
} catch (SQLException e) {
System.err.println("连接失败");
}
return conn;
}
public static void getClose(PreparedStatement ps, ResultSet rs,
Connection conn) {
// 关闭资源
try {
if (ps != null) {
ps.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
login.jsp文件别忘记吧pageEncoding="ISO-8859-1"改为pageEncoding=“utf-8”
<%@ 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%>">
<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">
-->
<style type="text/css">
table {
margin: 0 auto;
}
.login {
margin: 0 auto;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<%
String userName = request.getParameter("userName");
String msg = "";
msg = (String) request.getAttribute("msg");
if (msg == null)
msg = "";
%>
<form action="secondLogin.jsp" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="userName" />
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="password" name="pwd" />
</td>
</tr>
<tr>
<td colspan=2">
<span style="color: red"><%=msg%></span>
</td>
</tr>
<tr>
<td colspan="2" class="login">
<input type="submit" value="登录" />
<a href="index.jsp"><input type="button" value="未有账号?注册" /> </a>
</td>
</tr>
</table>
</form>
</body>
</html>
secondLogin.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="org.work.util.DBUtil"%>
<%@page import="java.sql.SQLException"%>
<%
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 'index.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">
-->
<style type="text/css">
table {
margin: 0 auto;
}
.login {
margin: 0 auto;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<%
// 1:首先把编码全部改为utf-8
// 将请求中的编码改为utf-8
request.setCharacterEncoding("utf-8");
// 将响应中的编码改为utf-8
response.setCharacterEncoding("utf-8");
// 设置响应的显示格式(类型)
response.setContentType("text/html");
// 2:处理请求
String pwd = request.getParameter("pwd");
String userName = request.getParameter("userName");
// 登录业务
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
boolean isTrue = false;
// 1.调用util包下的DBUtil.getConn();加载驱动并和数据库建立连接
conn = DBUtil.getConn();
try {
// 2.准备一个sql语句
String sql = "SELECT * FROM users WHERE user_name=? and pwd=?";
// 3.使用prepareStatement访问数据库
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
// 5.使用ResultSet接收访问数据库的反馈结果
rs = ps.executeQuery();
if (rs.next()) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
// 7.调用util包下的getClose();方法关闭资源
DBUtil.getClose(ps, rs, conn);
if (isTrue) {
//跳转到成功页面
//重定向
//这个就是小明问小李借钱后,小明在问小芳借钱,最后小芳把钱交给小明的过程
// secondLogin.jsp文件相当于小李 success.jsp相当于小芳
//从login.jsp request来的
response.sendRedirect("success.jsp");
} else {
//跳转到登录页面,并输出错误信息
//转发
//这个就是小明向小李借钱,小李在问小红借钱,最后小李把钱给小明的过程
//小李代表secondLogin.jsp文件,小红代表thirdLogin.jsp
request.setAttribute("msg", "密码错误!");
request.getRequestDispatcher("thirdLogin.jsp").forward(request,
response);
}
%>
</body>
</html>
thirdLogin.jsp文件
<%@ 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 'thirdLogin.jsp.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>
<%
String userName = request.getParameter("userName");
String msg = "";
msg = (String) request.getAttribute("msg");
if (msg == null)
msg = "";
%>
<form>
<table>
<tr>
<td>
用户名:
</td>
<td><%=userName%></td>
</tr>
<tr>
<td>
msg:
</td>
<td><%=msg%></td>
</tr>
</table>
</form>
</body>
</html>
success.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.work.util.DBUtil"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%
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>
<table>
<tr>
<th>
编号
</th>
<th>
姓名
</th>
<th>
年龄
</th>
<th>
用户名
</th>
<th>
密码
</th>
</tr>
<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 1.调用util包下的DBUtil.getConn();加载驱动并和数据库建立连接
conn = DBUtil.getConn();
// 2.准备一个sql语句
String sql = "SELECT * FROM users";
try {
// 3.使用prepareStatement访问数据库
ps = conn.prepareStatement(sql);
// 4.使用ResultSet接收访问数据库的反馈结果
rs = ps.executeQuery();
while (rs.next()) {
%>
<tr>
<th>
<%=rs.getInt("id")%>
</th>
<th>
<%=rs.getString("name")%>
</th>
<th>
<%=rs.getInt("age")%>
</th>
<th>
<%=rs.getString("user_name")%>
</th>
<th>
<%=rs.getString("pwd")%>
</th>
</tr>
<%
}
} catch (SQLException e) {
e.printStackTrace();
}
// 5.调用util包下的getClose();方法关闭资源
DBUtil.getClose(ps, rs, conn);
%>
</table>
</body>
</html>
最后运行结果