在Servlet中实现页面重定向

实现页面重定向主要应用HTTPServletResponse对象的sendRedirect()方法,与页面转发的forward()不同,使用forward()方法时,会将当前正在处理的请求转发到其他web组件(Servlet,JSP,HTML)。而sendRedirect()方法不会转发请求,只是页面跳转
在这里插入图片描述在这里插入图片描述
新建RedirectServlet的Servlet类,继承HTTPServlet类,在该类的doPost()方法中判断用户名和密码是否正确,不正确会使用sendRedirect()方法将页面重定向到错误页

public class RedirectServlet extends HttpServlet{
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		request.setCharacterEncoding("utf-8");	//设置请求的字符编码格式
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		if((name!=null&&!name.equals(""))&&(pwd!=null&&!pwd.equals(""))){
			if(name.equals("zj")&&pwd.equals("123")){
				//使用RequestDispatcher对象将页面请求转发到success.jsp页面
				request.getRequestDispatcher("success.jsp").forward(request, response);
			}else{
				response.sendRedirect("error.jsp");
			}
		}
	}
}

index.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>实现页面重定向</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{
			font-size:12px;
			font-family: 隶书;
			color:gray;
			border: 1px green solid;
		}
		input{
			font-size:12px;
			font-family: 隶书;
			color:gray;
		}
	</style>
  </head>
  
  <body>
  
   	<form action="redirect" method="post">
   		<table align="center">
   			<tr>
   				<td>用户名:</td>
   				<td><input type="text" name="name" /></td>
   			</tr>
   			<tr>
   				<td>密码:</td>
   				<td><input type="password" name="pwd" /></td>
   			</tr>
   			<tr>
   				<td colspan="2"><input type="submit" value="登 录" /></td>
   			</tr>
   		</table>
   	</form>
  </body>
</html>

success.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 '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">
	-->
	<style type="text/css">
		table{
			font-size:12px;
			font-family: 隶书;
			color:gray;
			border: 1px green solid;
		}
		input{
			font-size:12px;
			font-family: 隶书;
			color:gray;
		}
	</style>
  </head>
  
  <body>
  <table align="center">
  	<tr>
  		<td>
  			 <font color="green">
    			恭喜您【<%=request.getParameter("name")%>】,登录成功!
    		</font>
  		</td>
  	</tr>
  </table>
  
  </body>
</html>

error.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 'error.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 align="center">
  	<tr>
  		<td>
  			 <font color="red">
    			登录失败!
    		</font>
  		</td>
  	</tr>
  </table>
  </body>
</html>

web.xml配置

<servlet>
  	<servlet-name>RedirectServlet</servlet-name>
  	<servlet-class>com.cn.zj.Servlet.RedirectServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>RedirectServlet</servlet-name>
  	<url-pattern>/redirect</url-pattern>
  </servlet-mapping>

HTTPServletResponse对象的sendRedirect()方法一律返回状态代码为302的响应结果,浏览器接收到这种响应结果后,立即自动请求访问重定向的目标web组件,客户端最后接受到的是目标web组件的响应结果

猜你喜欢

转载自blog.csdn.net/weixin_44234912/article/details/88424138