<listener> <listener-class>net.jvsun.ListenerTest</listener-class> </listener>
public class JDBCHelper {
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
public static final String URL = "jdbc:oracle:thin:@localhost:1521:xxx";
public static final String DBNAME = "scott";
public static final String PASSWORD = "xxx";
public static Connection getConn() throws Exception{
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, DBNAME, PASSWORD);
return conn;
}
}
public class UserPOJO implements Serializable{
private static final long serialVersionUID = 7554548269035753256L;
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserPOJO(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public UserPOJO(String username, String password) {
this.username = username;
this.password = password;
}
public UserPOJO() {
super();
// TODO Auto-generated constructor stub
}
}
public class UserDAO {
public UserPOJO login(String username, String password) {
UserPOJO user=null;
Connection conn = null;
PreparedStatement pstate = null;
ResultSet res = null;
try {
conn=JDBCHelper.getConn();
String sql="select id,username from userinfo where username=? and password=?";
pstate = conn.prepareStatement(sql);
pstate.setString(1, username);
pstate.setString(2, password);
res = pstate.executeQuery();
while(res.next()){
user=new UserPOJO(res.getInt(1),username,null);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
}
public class UserServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getContextPath();
response.setContentType("text/html; charset=utf-8");
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
ServletContext application = this.getServletContext();//取得application对象
List<String> list = (List<String>) application.getAttribute("allUser");
if(list.indexOf(username) == -1){
UserPOJO pojo = new UserDAO().login(username, password);
if(null != pojo){
HttpSession session = request.getSession(true);//取得session对象
session.setAttribute("userName", username);
}
}
response.sendRedirect(path+"/index.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
public class ListenerTest implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener{
ServletContext application = null;
public void contextDestroyed(ServletContextEvent event) {
System.out.println("服务器关闭");
}
public void contextInitialized(ServletContextEvent event) {
List<String> list = new ArrayList<String>();
//用来保存所有已登录的用户
application = event.getServletContext();
//取得application对象
application.setAttribute("allUser", list);
//将集合设置到application范围属性中去
}
public void attributeAdded(HttpSessionBindingEvent se) {
List<String> list = (List<String>)application.getAttribute("allUser");
//假设:用户登陆成功之后,只将户名设置到session中
String userName = (String)se.getValue();
//取得用户名
if(list.indexOf(userName) == -1){
//表示此用户之前没有登陆
list.add(userName);
application.setAttribute("allUser", list);
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
List<String> list = (List<String>)application.getAttribute("allUser");
list.remove((String)se.getValue());
application.setAttribute("allUser", list);
}
public void attributeReplaced(HttpSessionBindingEvent se) {
}
public void sessionCreated(HttpSessionEvent event) {
}
public void sessionDestroyed(HttpSessionEvent event) {
}
}
<%@page contentType="text/html; charset=utf-8"%> <%@page import="java.util.*" %> <% String path = request.getContextPath(); %> <html> <body> <form action="<%=path %>/Login" method="post"> 账号: <input type="text" name="username" /> <br /> 密码: <input type="password" name="password" /> <br /> <input type="submit" value="提交" /> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
<title></title>
</head>
<body>
<ul>
<%
System.out.println(application.getAttribute("allUser"));
if(null != application.getAttribute("allUser")){
List<String> list = (List<String>)application.getAttribute("allUser");
%>
<h2>在线人数:<%=list.size() %></h2>
<%
for(String s:list){
%>
<a>姓名:</a><%=s %><a>---->此时在线</a><br>
<%
}
}
%>
</ul>
<hr/>
<a href="<%=path %>/logout.jsp" rel="external nofollow" >注销</a>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
<title>logout</title>
</head>
<body>
<%
session.removeAttribute("username");
session.invalidate();
response.sendRedirect("login.jsp");
%>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有