科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件Java中finalize()的另类用法(2)

Java中finalize()的另类用法(2)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

通过使用finalize(),就可以在垃圾收集器运行期间进行一些特殊的工作。而本文举例说明了finalize()的一种巧妙用法……

作者:李炳容 来源:xxol.net  2007年9月2日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
每一个用户均建立一个testSession对象,来保存该用户的信息。为了对类testSession进行说明,必须同时引人另一个文件logintest.jsp。这个用于示例的JavaScript/" target="_blank">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>

<td width="73%">

<input name="password" type="password" size="20">

</td>

</tr>

<tr>

<td height="32" colspan="2" align="right">

<div align="center">

<input name="B1" type="button" value="登录"

onclick="doAction('login')">

<input name="B2" type="reset"

value="重写">

<input name="B3" type="button" value="注销"

onclick="doAction('logout')">

 </div></td>

</tr>

<tr>

<td width="27%" height="22" align="right">

<input name="actiontype" type="hidden"

value=""></td>

<td width="73%"> <input name="info" type="text"

size="20" value="<%=actionResult%>">

</td>

</tr>

</form>

</table>

</body>

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章