编写一个用户登录页面,包含账户和密码及登录按钮,如果用户登录表单中没有填写任何数据,单登录按钮后,则通过<jsp:forward>元素转发到错误页面;如果用户在登录表单中填写用户名与密码,单登录按钮后,则通过<jsp:forward>元素转发到正确页面。
index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>
用户登录
</title>
</head>
<body>
<center>
<p>用户登录</p>
<form name="form1" method="post" action="xx.jsp">
<label>
姓名:<input name="userName" type="text" size="15">
</label><br>
<label>
密码:<input name="passWord" type="password" size="15"></label><br>
<label><input name="Submit" type="submit" value="提交"></label>
<label><input name="Submit2" type="reset" value="重置"></label>
</form>
</center>
</body>
</html>
error.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>登录失败</title>
</head>
<body>
<img src="1.jpg">
登录失败哦亲,再试一次吧!error<a href="index.html">重试</a>
</body>
</html>
correct.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>登录成功</title>
</head>
<body>
<h1>ok</h1>
</body>
</html>
XX.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'xx.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>
<%
String Name=request.getParameter("userName");
String Pwd=request.getParameter("passWord");
if (Name.equals("mars") && Pwd.equals("haha"))
{%>
<jsp:forward page="correct.html"/>
<%
}
%>
<jsp:forward page="error.html"/>
</body>
</html>