在Servlet中实现页面转发

在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>
在Java WebServlet是一种Java程序,用于处理客户端请求并生成响应。Servlet可以实现页面转发和重定向,具体实现方式如下: 1. 页面转发(Forward) 页面转发是指将请求转发给另一个资源进行处理,并将其响应返回给客户端。在Servlet,可以使用RequestDispatcher类的forward()方法实现页面转发。 代码示例: ```java RequestDispatcher dispatcher = request.getRequestDispatcher("target.jsp"); dispatcher.forward(request, response); ``` 说明: - `request.getRequestDispatcher("target.jsp")`表示获取目标页面(target.jsp)的请求调度器对象。 - `dispatcher.forward(request, response)`表示将当前请求和响应对象转发给目标页面进行处理。 2. 页面重定向(Redirect) 页面重定向是指将请求重定向到另一个资源进行处理,并将其响应返回给客户端。在Servlet,可以使用HttpServletResponse类的sendRedirect()方法实现页面重定向。 代码示例: ```java response.sendRedirect("target.jsp"); ``` 说明: - `response.sendRedirect("target.jsp")`表示将当前请求重定向到目标页面(target.jsp)进行处理。 需要注意的是,页面转发和重定向的区别在于: - 页面转发是在服务器端完成的,客户端并不知道服务器端进行了转发操作,因此客户端的URL地址不会发生变化。 - 页面重定向是在客户端完成的,客户端会向服务器发出新的请求,因此客户端的URL地址会发生变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值