实例说明:
网站可以由多个动态页面组成,并且每一个动态页面直接都存在着联系。为了保证网站内信息资源的安全,程序员应禁止浏览者不通过登录页面而强行进入其他页面进行浏览。
设计过程:
(1)、创建DoyouLogon类来验证并存储用户信息。关键代码如下:
package cn.com;
publicclass DoyouLogon {
private String username = "";
private String userpassword = "";
public String getUsername() {
returnusername;
}
publicvoid setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
returnuserpassword;
}
publicvoid setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public DoyouLogon(){}
public String checkuser(){
String backstr = "";
if(this.username.equals("")){
backstr += "<li>请输入<b>用户名!</b></li></br>";
}
if(this.userpassword.equals("")){
backstr += "<li>请输入<b>密 码!</b></li>";
}
return backstr;
}
}
(2)、创建登录的首页index.jsp。关键代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录页面</title>
</head>
<%
session.invalidate();
%>
<body>
<form action="dologon.jsp">
<table>
<tr bgcolor="lightgrey">
<td align="center">请先登录</td>
</tr>
<tr height="50">
<td align="center">
用户名:<input type="text" name="username" size="30"><br/>
密 码:<input type="password" name="userpassword" size="30" redisplay="false">
</td>
</tr>
<tr bgcolor="lightgrey">
<td align="center">
<input type="submit" name="logon" value="登录">
<input type="reset" name="clear" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
(3)、创建接受Form表单的页面dologon.jsp 。在该页面中对用户输入的信息进行判断,看其是否为空,如果为空则跳转到tishi.jsp页面显示提示信息,否则进入welcome.jsp欢迎页面。其关键代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:useBean id="mylogon" class="cn.com.DoyouLogon" scope="session" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>dologon.jsp</title>
</head>
<body>
<%
String username = request.getParameter("username");
String userpassword = request.getParameter("userpassword");
mylogon.setUsername(username);
mylogon.setUserpassword(userpassword);
String mess = mylogon.checkuser();//获取验证结果信息
if("".equals(username) || "".equals(userpassword)){//如果用户输入的登录信息为空
session.setAttribute("logonuser","");
session.setAttribute("error",mess);
response.sendRedirect("tishi.jsp");//转到提示信息页面
}else{
session.setAttribute("logonuser",mylogon);//将用户名存入回话对象
response.sendRedirect("welcome.jsp");//转到欢迎页面
}
%>
</body>
</html>
(4)、创建一个提示页面tishi.jsp,用来显示提示信息。在该页面中首先判断Session中是否存储了登录用户的信息,如果不存在则提示登录。其关键代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>tishi.jsp</title>
</head>
<body>
<%
String message = "";
if(session.getAttribute("logonuser") == null){ //如果用户没有登录
message = "请先<a href='index.jsp'>[登录]</a>";
}else{ //如果用户登录失败
message = (String)session.getAttribute("error");
}
%>
<table>
<tr bgcolor="lightgrey">
<td align="center">友情提示!</td>
</tr>
<tr height="50">
<td align="center"><%=message %></td>
</tr>
</table>
</body>
</html>
(5)、创建登录成的欢迎页面welcome.jsp用于显示欢迎信息。关键代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:useBean id="mylogon" class="cn.com.DoyouLogon" scope="session"></jsp:useBean>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>welcome.jsp</title>
</head>
<body>
<%
String message = "";
if(session.getAttribute("logonuser") == null){
response.sendRedirect("tishi.jsp");
}else{
message="您好!<b>"+mylogon.getUsername()+"</b>[女士/先生]!欢迎登录!";
}
%>
<table>
<tr bgcolor="lightgrey" height="30">
<td align="center">友情提示!</td>
</tr>
<tr height="50">
<td align="center"><%=message %></td>
</tr>
</table>
</body>
</html>
秘笈心法:
Session作用于同一个浏览器之中,它共享同一个浏览器中的各个页面的数据。无论当前浏览器是否在多个页面间进行了跳转操作,整个用户会话将一直存在下去,直到关闭浏览器。