主要是要分清楚application和session的作用域。
- 登录页面:index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" //注意编码
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">//编码
<title>Insert title here</title>
</head>
<body>
<h1>登陆</h1>
<form action="login.jsp" method = "post">//提交表单,登录页面
<input type = "text" name = "username">
<br/>
<input type = "submit" value = "登陆">
</form>
</body>
</html>
- 处理登录的数据的:login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
//导入集合类jar包
<%@ page import = "java.util.*" %>
<%
//取出登录表单里的username,取出来的都是object对象所以要强制类型转换
String username = request.getParameter("username");
List olUserList = (List)application.getAttribute("olUserList");
//application是应用范围内有效,用这个来存放在线列表。
//如果取出来的是空的,要初始化
if(olUserList == null)
{
olUserList = new ArrayList();
}
//把username添加进去
olUserList.add(username);
//重新赋值回去
application.setAttribute("olUserList", olUserList);
//当前用户的session
session.setAttribute("username", username);
//重定向到result.jsp
response.sendRedirect("result.jsp");
%>
</body>
</html>
- 显示在线结果:result.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%@ page pageEncoding="utf-8" import="java.util.*"%>
//取出当前会话的username问好
你好:<%=session.getAttribute("username")%>
//提供注销功能
<form action="loginOut.jsp" method="post">
<input type="submit" value="注销">
</form>
//提供继续登录的请求功能
<form action="index.jsp" method="post">
<input type="submit" value="登陆">
</form>
//循环将用户列表输出
<br /> 当前在线的用户为:
<%
List olUserList = (List) application.getAttribute("olUserList");
for (int i = 0; i < olUserList.size(); i++) {
String olUsername = (String) olUserList.get(i);
out.println("<tr>");
out.println("<td>"+ olUsername +"</td>");
out.println("</tr>");
}
%>
</body>
</html>
- 提供注销功能的:loginOut.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%@ page import = "java.util.*" %>
<%
//取出当前session的username并在olUserList中删除
String username = (String)session.getAttribute("username");
List olUserList = (List)application.getAttribute("olUserList");
olUserList.remove(username);
application.setAttribute("olUserList", olUserList);
//销毁会话
session.invalidate();
%>
<form action="result.jsp">
<input type = "submit" value = "返回" />
</form>
<form action="index.jsp">
<input type = "submit" value = "登陆" />
</form>
<h1>注销成功</h1>
</body>
</html>