下面是登录页面index.jsp文件代码:
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录页面</title>
<style type="text/css">
<!--
.STYLE1 {
font-family: "宋体";
font-size: 36px;
color: #FF0000;
}
body {
background-image: url(cqu3.jpg);
background-repeat: no-repeat;
}
body,td,th {
font-size: medium;
}
.STYLE2 {
font-size: 24px;
color: #000000;
}
.STYLE4 {font-size: 18px}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="${pageContext.request.contextPath}/responseLogin.jsp">
<p align="center" class="STYLE1">重庆大学本科教学管理系统</p>
<p align="center" class="STYLE2">用户登录页面</p>
<p align="center">
<label><span class="STYLE4">用户名</span>
<input name="userName" type="text" id="userName" maxlength="8" />
</label>
</p>
<p align="center">
<label><span class="STYLE4">密 码</span>
<input name="password" type="text" id="password" />
</label>
</p>
<p align="center">
<label>
<input name="login" type="submit" id="login" value="登录">
</label>
<label></label>
<input name="reset" type="reset" id="reset" value="重置">
</p>
<p align="center">版权归 <a href="http://www.cqu.edu.cn/">重庆大学</a> 周键所有</p>
<p> </p>
</form>
</body>
</html>
处理登录信息页面,这个页面主要是判断用户输入的登录信息是否正确。在实际的项目中,很少用JSP页面来处理登录信息,一般利用Servlet来处理,这里我们只是举一个利用JSP处理登录信息的例子。responseLogin.jsp代码:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312" />
<title>相应登录</title>
</head>
<body>
<%! String userName,password;
String targetPage=null;
RequestDispatcher requestDispatcher=null;<%--页面跳转--%>
%>
<%
userName=request.getParameter("userName");
password=request.getParameter("password");
if(userName.equals("20095397")&&password.equals("123456"))
{
targetPage="/successfullLogin.jsp";
request.setAttribute("userName", userName);<%--只需一次请求,故利用request来存储参数--%>
}
else
{
targetPage="/failLogin.jsp";
request.setAttribute("error", "fail"+userName);
}
requestDispatcher=request.getRequestDispatcher(targetPage);
requestDispatcher.forward(request, response);<%--跳转到对应页面--%>
%>
</body>
</html>
登录成功页面successfullLogin.jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312" />
<title>登录成功</title>
</head>
<body>
登录成功!
</body>
</html>
登录失败页面failLogin.jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312" />
<title>登录失败</title>
</head>
<body>
登录失败!
</body>
</html>