首页 > 编程 > JSP > 正文

JSP显示会话信息实例

2024-09-05 00:19:58
字体:
来源:转载
供稿:网友

package hall;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;

public class showsession extends httpservlet {
public void doget(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
httpsession session = request.getsession(true);
response.setcontenttype("text/html");
printwriter out = response.getwriter();
string title = "searching the web";
string heading;
integer accesscount = new integer(0);;
if (session.isnew()) {
heading = "welcome, newcomer";
} else {
heading = "welcome back";
integer oldaccesscount =
// 在servlet api 2.2中使用getattribute而不是getvalue
(integer)session.getvalue("accesscount");
if (oldaccesscount != null) {
accesscount =
new integer(oldaccesscount.intvalue() + 1);
}
}
// 在servlet api 2.2中使用putattribute
session.putvalue("accesscount", accesscount);

out.println(servletutilities.headwithtitle(title) +
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1 align=\"center\">" + heading + "</h1>\n" +
"<h2>information on your session:</h2>\n" +
"<table border=1 align=center>\n" +
"<tr bgcolor=\"#ffad00\">\n" +
" <th>info type<th>value\n" +
"<tr>\n" +
" <td>id\n" +
" <td>" + session.getid() + "\n" +
"<tr>\n" +
" <td>creation time\n" +
" <td>" + new date(session.getcreationtime()) + "\n" +
"<tr>\n" +
" <td>time of last access\n" +
" <td>" + new date(session.getlastaccessedtime()) + "\n" +
"<tr>\n" +
" <td>number of previous accesses\n" +
" <td>" + accesscount + "\n" +
"</table>\n" +
"</body></html>");
}
public void dopost(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
doget(request, response);
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表