【JSP篇】——5.JSP之使用jsp:forward实现用户信息验证的页面跳转

1.工程说明:使用Jsp实现页面跳转动作

1.Information.html:电话号码,用户名,email--->用户信息填写
2.isAccess.jsp:检验Information.html页面信息的正确性,正确则打印信息,错误则跳转到error.jsp

3.error.jsp:需要用户重新填写信息

2.jsp:forward

<jsp forward page="anotherPage.jsp">

这里将当前页面跳转到anotherPage.jsp这个页面中,第二个页面是显示在第一个页面中的。

3.Information.html页面信息

<!DOCTYPE html>
<html>
  <head>
    <title>用户身份信息页面</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
   <h3>please write your information!</h3> <br>
   <form action="isAccess.jsp" name="infor" method="post">
   <table>
   		<tr>
   			<td>name:</td>
   			<td><input type="text" name="name"></td>
   		</tr>
   		<tr>
   			<td>telephone:</td>
   			<td><input type="text" name="tel"></td>
   		</tr>
   		<tr>
   			<td>Email:</td>
   			<td><input type="text" name="email"></td>
   		</tr>
   		<tr>
   			<td colspan="2"><input type="submit" value="submit"></td>
   		</tr>
   </table>
   
   </form>
  </body>
</html>

4.检验用户输入信息是否正确的isAccess.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=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">
	-->

  </head>
  
  <body>
    <h2>我是用于您的身份信息验证的JSP</h2> <br>
    <hr>
    
    <%! int count=0;%>
    <%!String name,tel,email=null;%>
    
    <%
    	
    	name=request.getParameter("name");
    	tel=request.getParameter("tel");
    	email=request.getParameter("email");
		
		if(name.equals("") || tel.equals("") || email.equals("")){
		%>
		<jsp:forward page="error.jsp"></jsp:forward>
		
		<%}else{
			out.print("姓名:"+name+"<br>");
			out.print("联系电话:"+tel+"<br>");
			out.print("Eamil:"+email+"<br>");
			} 
		
		%>
	
  </body>
</html>

5.用户信息输入错误的页面:error.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>
    <h2>oh no!您的身份填错了!重新填写吧~</h2> <br>
       <form action="isAccess.jsp" name="infor_another">
   <table>
   		<tr>
   			<td>姓名:</td>
   			<td><input type="text" name="name"></td>
   		</tr>
   		<tr>
   			<td>联系电话:</td>
   			<td><input type="text" name="tel"></td>
   		</tr>
   		<tr>
   			<td>Email:</td>
   			<td><input type="text" name="email"></td>
   		</tr>
   		<tr>
   			<td colspan="2"><input type="submit" value="确定"></td>
   		</tr>
   </table>
   
   </form>
  </body>
</html>

6.部署到服务器上,运行的结果(工程下载地址

用户信息测试点一:正确信息测试

           

     图1:Information.html页面                                        图2.填写完整的用户信息


  图3.点击跳转之后的页面-->isAccess.jsp


用户信息测试点二:错误信息测试

       

        图1:Information.html页面                          图2.未填写邮箱的用户信息


    图3.点击跳转之后的页面-->isAccess.jsp

   在这里我们可以看见,根据isAccess.jsp中的代码逻辑可知,用户输入为空的时候,就跳转到我们的error.jsp页面,在这个页面内用户需要重新填写信息,填写完之后再会跳转到isAccess.jsp中进行正确性验证。而这里要强调的是:根据我们跳转之后的页面,虽然显示的是error.jsp中的内容,但是实际上还是在isAccess.jsp页面内(这里从最上面的地址可以发现:http://localhost:8080/JspJump/isAccess.jsp)。

   使用jsp:forward进行页面的跳转,实际上就像是跳转的页面嵌套在我们的原页面中。

  • 7
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值