JSP_<jsp:forward>应用

本文出自:http://blog.csdn.net/svitter


实验环境:Myeclipse10 + tomcat7

简单应用于登陆界面。jsp:forward的作用是,把当前的JSP页引导到另一个页面上,浏览器地址本显示的是当前网页的地址,内容则是另一个界面的。

1.User.html

<!DOCTYPE html>
<html>
<head>
<title>登陆信息</title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

</head>

<body>
	<form action="Login.jsp" method="post" name="Loginfrm" id="Loginform">
		<table width="298" border="0" align="center" cellpadding="2" cellspacing="1">
			<tr>
				<td align="right">用户名:</td>
				<td align="left"><input name="User" type="text" size="30">
				</td>
			</tr>
			<tr>
				<td align="right">密码:</td>
				<td align="left"><input name="Password" type="password"
					size="30">
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="login">
				 
				<input type="reset" value="reset"></td>
		</table>
	</form>
</body>
</html>

2.Login.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">
	-->

  </head>
  
  <body>
    <% 
    String User=request.getParameter("User");
    String Password=request.getParameter("Password");
    if(User.equals("Admin") && Password.equals("Admin")){%>
    	<jsp:forward page="welcome.jsp"/>
	<%} else { %>
		<jsp:forward page="errorPage.jsp"/>
	<%} %>
  </body>
</html>

3.errorPage.jsp; 注意isErrorPage=TRUE;

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isErrorPage="true"%>
<%
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>
    <h1>错误信息</h1>
    <hr><center>
    <h3><%=exception%>
    </h3>
    </center>
  </body>
</html>
4.welcome.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 ‘welcome.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>
    Welcome!<br>
  </body>
</html>

对应资源下载:http://download.csdn.net/detail/svitter/7358523

JSP_<jsp:forward>应用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。