在Servlet中实现页面转发,使用的是RequestDispatcher对象的forward()方法,可以在Servlet中通过forward()方法将当前的请求转发到其他Web组件
index.jsp页面----<form>
中的action属性值为Servlet类在web.xml中配置的URL,提交方式为post
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="forward" 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>
新建ForwardServlet的Servlet类,在doPost()方法中获得表单请求信息
public class ForwardServlet 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{
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
}
}
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>
在Servlet的配置文件web.xml中配置RedirectServlet类
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>ServletTest</display-name>
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<servlet-class>com.cn.zj.Servlet.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/forward</url-pattern>
</servlet-mapping>
</web-app>