an login(String username) throws Exception
{ //处理登录
this.User.name=username;
this.addappses(this.User,this.vsid);
return true;
}
public boolean logout() throws Exception
{ //处理注销
this. finalize();
this.User=null;
this.vsid=null;
return true;
}
}
Java中finalize()的另类用法(2)
时间:2010-12-13
每一个用户均建立一个testSession对象,来保存该用户的信息。为了对类testSession进行说明,必须同时引人另一个文件logintest.jsp。这个用于示例的JSP文件提供一个简单的界面进行登录、注销处理。文件内容如下:
<%@ page import=" com.testSession,
java.util.Vector"%>
<%@page contentType="text/html;charset=GBK" %>
<% request.setCharacterEncoding(response.
getCharacterEncoding());%>
<%
String actionType=request.getParameter("actiontype");
String actionResult="";
if(actionType!=null) {
if(actionType.equals("login")){ // -1-
String userName=request.getParameter("username");
if(userName==null || userName.equals("")){
;
}
else{
String password=request.getParameter("password");
if(password==null)
password="";
testSession ts=
(testSession)session.getAttribute("testSession");
if(ts!=null) { //-1.1-
session.removeAttribute("testSession");
if( !ts.User.name.equals(""))
ts.logout();
}
ts=new testSession();
if(!ts.verify(userName,password)) {
//验证用户与密码,看是否合法用户
actionResult="login fail";
//非法用户,显示错误信息
}
else{ //验证成功
session.setAttribute("testSession",ts);
Vector app_vts=
(Vector)application.getAttribute("app_vts");
if(app_vts==null) {
app_vts=new Vector();
application.setAttribute("app_vts",app_vts);
}
ts.setSessionVar(session.getId(),app_vts);
ts.login(userName);
actionResult=userName+" login success";
}
}
}
if(actionType.equals("logout")){
testSession ts=
(testSession)session.getAttribute("testSession");
if(ts!=null) {
session.removeAttribute("testSession");
if( !ts.User.name.equals("")){ //-2-
actionResult=ts.User.name;
ts.logout();
}
session.invalidate();
}
actionResult=actionResult+" logout success";
}
}
else
actionResult="null";
%>
<head>
<script LANGUAGE="Javascript"></script>
<script>
function doAction(actionType)
{
document.test.actiontype.value=actionType;
document.test.submit();
}
</script>
</head>
<body>
<table width="80%" border="1" align="center" >
<form method="POST" action="logintest.jsp"
name="test">
<tr>
<td height="33" align="right">用户:</td>
<td width="70%"> <input name="username"
type="text" value="" size="20">
</td>
</tr>
<tr>
<td width="27%" height="22" align="right">密码:</td>
&l
|