下面的这个是可以的,谁能帮我修改下,就是用数据库来实现,登陆一个用户用库就记录进数据库,输出的时候将数据库数据写出就可以了,会写的话应该很快的。
package online.MyEclipse.outpackage.online;import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class CountListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener { private ServletContext application = null ;
private HttpSession session = null ; public void contextInitialized(ServletContextEvent sce) { //初始化一个application对象,即application就是一个
//以tomcat的一次启动为整个周期的ServletContext
this.application=sce.getServletContext(); //设置一个列表属性online,用于保存在线用户名
this.application.setAttribute("online", new ArrayList());
} public void contextDestroyed(ServletContextEvent sce) {} public void attributeAdded(ServletContextAttributeEvent scab) {} public void attributeRemoved(ServletContextAttributeEvent scab) {} public void attributeReplaced(ServletContextAttributeEvent scab) {} public void sessionCreated(HttpSessionEvent se) {} public void sessionDestroyed(HttpSessionEvent se) {
//取得用户名列表
List online=(List)this.application.getAttribute("online") ;
//取得当前用户名
String username=(String)se.getSession().getAttribute("name") ;
//将此用户名从列表中删除
online.remove(username) ;
//将删除后的列表重新设置到application属性中
this.application.setAttribute("online", online) ;
}
//当session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法
public void attributeAdded(HttpSessionBindingEvent se) { //取得用户名列表
List online=(List)this.application.getAttribute("online") ;
//将当前用户名添加到列表中.list数组添加值的方式 list.add();
online.add(se.getValue()) ;
//若是多维数组则调用相对应的属性的时候se.getValue("name");
//将添加后的列表重新设置到application属性中 this.application.setAttribute("online", online) ;
} public void attributeRemoved(HttpSessionBindingEvent se) {} public void attributeReplaced(HttpSessionBindingEvent se) {}
}<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>sessionlistener</title>
</head> <body>
<form action="sessionlistener.jsp" method="post">
用户名:
<input type="text" name="username" />
<input type="submit" value="登录" />
<a href="logout.jsp">注销</a>
</form>
<%
String username = request.getParameter("username");
if (username != null) { session.setAttribute("name", username);
}
%>
<p>
<h3>
在线用户:
</h3>
<hr>
<%
List online = (List) getServletContext().getAttribute("online");
Iterator iter = online.iterator();
while (iter.hasNext()) {
%>
<li>
<%=iter.next()%>
</li>
<%
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>logout</title>
</head>
<body>
<%
//将session销毁
session.invalidate() ;
response.setHeader("refresh","3;URL=sessionlistener.jsp") ;
%>
<h3>注销成功!</h3>
3秒后自动返回登录页面<br>
如果没有跳转,请点<a href="sessionlistener.jsp">这里</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>online.MyEclipse.outpackage.online.CountListener</listener-class>
</listener>
</web-app>驱动和url这样就可以了
<init-param>
<param-value>jdbc:mysql://localhost:3306/user</param-value>
</init-param>
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>前面发过帖子可是都解决不了 可以见:http://topic.csdn.net/u/20081017/13/69df628a-41d3-46e7-a51c-c1e29b937cd0.html ,所以索性全部贴上来了,谁先帮我写好分全给,谢谢了。
package online.MyEclipse.outpackage.online;import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class CountListener implements ServletContextListener,ServletContextAttributeListener,HttpSessionListener,HttpSessionAttributeListener { private ServletContext application = null ;
private HttpSession session = null ; public void contextInitialized(ServletContextEvent sce) { //初始化一个application对象,即application就是一个
//以tomcat的一次启动为整个周期的ServletContext
this.application=sce.getServletContext(); //设置一个列表属性online,用于保存在线用户名
this.application.setAttribute("online", new ArrayList());
} public void contextDestroyed(ServletContextEvent sce) {} public void attributeAdded(ServletContextAttributeEvent scab) {} public void attributeRemoved(ServletContextAttributeEvent scab) {} public void attributeReplaced(ServletContextAttributeEvent scab) {} public void sessionCreated(HttpSessionEvent se) {} public void sessionDestroyed(HttpSessionEvent se) {
//取得用户名列表
List online=(List)this.application.getAttribute("online") ;
//取得当前用户名
String username=(String)se.getSession().getAttribute("name") ;
//将此用户名从列表中删除
online.remove(username) ;
//将删除后的列表重新设置到application属性中
this.application.setAttribute("online", online) ;
}
//当session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法
public void attributeAdded(HttpSessionBindingEvent se) { //取得用户名列表
List online=(List)this.application.getAttribute("online") ;
//将当前用户名添加到列表中.list数组添加值的方式 list.add();
online.add(se.getValue()) ;
//若是多维数组则调用相对应的属性的时候se.getValue("name");
//将添加后的列表重新设置到application属性中 this.application.setAttribute("online", online) ;
} public void attributeRemoved(HttpSessionBindingEvent se) {} public void attributeReplaced(HttpSessionBindingEvent se) {}
}<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>sessionlistener</title>
</head> <body>
<form action="sessionlistener.jsp" method="post">
用户名:
<input type="text" name="username" />
<input type="submit" value="登录" />
<a href="logout.jsp">注销</a>
</form>
<%
String username = request.getParameter("username");
if (username != null) { session.setAttribute("name", username);
}
%>
<p>
<h3>
在线用户:
</h3>
<hr>
<%
List online = (List) getServletContext().getAttribute("online");
Iterator iter = online.iterator();
while (iter.hasNext()) {
%>
<li>
<%=iter.next()%>
</li>
<%
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>logout</title>
</head>
<body>
<%
//将session销毁
session.invalidate() ;
response.setHeader("refresh","3;URL=sessionlistener.jsp") ;
%>
<h3>注销成功!</h3>
3秒后自动返回登录页面<br>
如果没有跳转,请点<a href="sessionlistener.jsp">这里</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>online.MyEclipse.outpackage.online.CountListener</listener-class>
</listener>
</web-app>驱动和url这样就可以了
<init-param>
<param-value>jdbc:mysql://localhost:3306/user</param-value>
</init-param>
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>前面发过帖子可是都解决不了 可以见:http://topic.csdn.net/u/20081017/13/69df628a-41d3-46e7-a51c-c1e29b937cd0.html ,所以索性全部贴上来了,谁先帮我写好分全给,谢谢了。
package online.MyEclipse.outpackage.online;import javax.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class CreateDBServlet extends HttpServlet{
private String url;
private String user;
private String password; public void init() throws ServletException{
String driverClass = getInitParameter("driver");
url = getInitParameter("url");//这里这些都是从web.xml里面获取的,所以xml要修改
user = getInitParameter("user");//修改好的xml下面帖出来
password = getInitParameter("password"); try{
Class.forName(driverClass);//加载驱动
}
catch(ClassNotFoundExcrption ce){//捕获异常
throw new UnavailableException("加载数据库驱动失败!");
} } //下面就是对前台jsp页面发送的请求做处理,比如就是对用户登陆进行判断,
//看数据库中是否有该用户 }
接着写:package online.MyEclipse.outpackage.online;import javax.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class CreateDBServlet extends HttpServlet{
private String url;
private String user;
private String password; public void init() throws ServletException{
String driverClass = getInitParameter("driver");
url = getInitParameter("url");//这里这些都是从web.xml里面获取的,所以xml要修改
user = getInitParameter("user");//修改好的xml下面帖出来
password = getInitParameter("password"); try{
Class.forName(driverClass);//加载驱动
}
catch(ClassNotFoundExcrption ce){//捕获异常
throw new UnavailableException("加载数据库驱动失败!");
} } //下面就是对前台jsp页面发送的请求做处理,比如就是对用户登陆进行判断,
//看数据库中是否有该用户
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String user = null;
String pwd = null; req.setCharacterEncoding("gb2312"); //支持客户端中文输入的获取
resp.setContentType("text/html;charset=gb2312");//定义输出显示页面的编码方式
PrintWriter out = resp.getWriter(); user = req.getParameter("username");//获取jsp页面传入的用户名
pwd = req.getParameter("password");//你再在jsp页面中加入登陆密码 try{
conn.DriverManager(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from userinfo");
//这里假设你在user数据库中建立userinfo表
while(rs.net()){
String userDB = rs.getString(1);
//为什么没用0而用1,你在建表时,把ID作为代理主键
String pwdDB = rs.getString(2);
if(null != user && null !=pwd){
if(user.equals(userDB)&&pwd.equals(pwdDB)){
out.println("success!");
}
else{
out.println("用户名或密码不存在!请重新登陆");
out.println("<a href="login.jsp">请点击这里重新登陆</a>");
//这里假定你的登陆页面为login.jsp }
}
else{
out.println("用户名或密码不能为空!请重新登陆");
out.println("<a href="login.jsp">请点击这里重新登陆</a>");
//这里假定你的登陆页面为login.jsp
}
}
out.close();
}
catch(SQLException e){
e.printStackTrace();
}
} }
<init-param>
<param-name>url </param-name>
<param-value>jdbc:mysql://localhost:3306/user </param-value>
</init-param>
<init-param>
<param-name>driver </param-name>
<param-value>com.mysql.jdbc.Driver </param-value>
</init-param>
<init-param>
<--这里假设你的mysql数据库用户,其实默认也是这样的-->
<param-name>user </param-name>
<param-value>root </param-value>
</init-param>
<init-param>
<param-name>password </param-name>
<param-value>123456</param-value>
</init-param>
加入代码的具体位置:
就是在while(rs.next())中
对获取的每个用户信息把他add到你要保存到session的list当中就可以了
这样在jsp页面中再次显示时就会有该功能那这样你就要分两个jsp 页面了,一个 是登陆的login.jsp
另一个是你成功登陆后的统计页面
你在CreateDBServlet中修改为
if(user.equals(userDB)&&pwd.equals(pwdDB)){
out.println("success!");
out.println("<a href="list.jsp">欢迎进入!</a>");
}
就可以了
试下.