JavaEE在线人数管理系统

网友投稿 191 2023-07-06


JavaEE在线人数管理系统

这篇博客是filter、listener和servlet技术的相关总结,实现了简单的显示在线人数、在线人详细信息、管理员踢人功能

下面是详细代码

web.xml

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

character

cn.hncu.filter.CharacterFilter

charset

utf-8

login

cn.hncu.filter.LoginFilter

character

/*

login

/servlet/*

/jsps/*

FORWARD

cn.hncu.listener.MySessionListener

LoginServlet

cn.hncu.servlet.LoginServlet

ShowServlet

cn.hncu.servlet.ShowServlet

This is the description of my J2EE component

This is the display name of my J2EE component

KickOutServlet

cn.hncu.servlet.KickOutServlet

This is the description of my J2EE component

This is the display name of my J2EE component

LoginOutServlet

cn.hncu.servlet.LoginOutServlet

LoginServlet

/LoginServlet

ShowServlet

/servlet/ShowServlet

KickOutServlet

/servlet/KickOutServlet

LoginOutServlet

/servlet/LoginOutServlet

index.jsp

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

character

cn.hncu.filter.CharacterFilter

charset

utf-8

login

cn.hncu.filter.LoginFilter

character

/*

login

/servlet/*

/jsps/*

FORWARD

cn.hncu.listener.MySessionListener

LoginServlet

cn.hncu.servlet.LoginServlet

ShowServlet

cn.hncu.servlet.ShowServlet

This is the description of my J2EE component

This is the display name of my J2EE component

KickOutServlet

cn.hncu.servlet.KickOutServlet

This is the description of my J2EE component

This is the display name of my J2EE component

LoginOutServlet

cn.hncu.servlet.LoginOutServlet

LoginServlet

/LoginServlet

ShowServlet

/servlet/ShowServlet

KickOutServlet

/servlet/KickOutServlet

LoginOutServlet

/servlet/LoginOutServlet

index.jsp

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

姓名:


欢迎回来,${ sessionScope.user.name}

管理员

会员


查看在线人信息

安全退出

/jsps/show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

${online.user.name }

游客

踢出

User.java(值对象)

package cn.hncu.domain;

public class User {

private String name;

private boolean admin;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public boolean isAdmin() {

return admin;

}

public void setAdmin(boolean admin) {

this.admin = admin;

}

@Override

public String toString() {

return "User [name=" + name + ", admin=" + admin + "]";

}

}

MySessionListener.java(监听器)

package cn.hncu.listener;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener{

@Override

public void sessionCreated(HttpSessionEvent se) {

Map onlines= (Map) se.getSession().getServletContext().getAttribute("onlines");

if(onlines==null){

onlines=Collections.synchronizedMap(new HashMap());//对map进行加上同步锁

se.getSession().getServletContext().setAttribute("onlines",onlines);

}

onlines.put(se.getSession().getId(), se.getSession());

}

@Override

public void sessionDestroyed(HttpSessionEvent se) {

Map onlines= (Map) se.getSession().getServletContext().getAttribute("onlines");

if(onlines.containsKey(se.getSession().getId())){

onlines.remove(se.getSession().getId());

}

}

}

CharacterFilter.java(字符过滤器)

package cn.hncu.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

public class CharacterFilter implements Filter {

private String charset;

@Override

public void init(FilterConfig filterConfig) throws ServletException {

charset=filterConfig.getInitParameter("charset");

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

request.setCharacterEncoding(charset);

response.setCharacterEncoding(charset);

HttpServletRequest req=(HttpServletRequest) request;

if(req.getSession().getAttribute("ip")==null){

req.getSession().setAttribute("ip", req.getRemoteAddr());

}

chain.doFilter(request, response);

}

@Override

public void destroy() {

}

}

LoginFilter.java(防止游客乱访问过滤器)

package cn.hncu.filter;

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter {

@Override

public void init(FilterConfig filterConfig) throws ServletException {

}

@Override

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest req=(HttpServletRequest) request;

if(req.getSession().getAttribute("user")==null){

((HttpServletResponse)response).sendRedirect(req.getContextPath()+"/index.jsp");

return;

}

chain.doFilter(request, response);

}

@Override

public void destroy() {

}

}

LoginServlet.java

package cn.hncu.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import cn.hncu.domain.User;

public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String name=request.getParameter("name");

User user=(User) request.getSession().getAttribute("user");

if (user==null&&name!=null&&name.trim().length()>0) {//封装User

user = new User();

user.setName(name);

if(name.equals("admin")){

user.setAdmin(true);

}

}

request.getSession().setAttribute("user", user);

response.sendRedirect(request.getContextPath()+"/index.jsp");

}

}

ShowServlet.java

package cn.hncu.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class ShowServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

Map onlines=(Map) request.getServletContext().getAttribute("onlines");

List> list=new ArrayList>();//吧Online封装成list

if (onlines!=null) {

Iterator it = onlines.values().iterator();

while (it.hasNext()) {

HttpSession s = it.next();

Map map = new HashMap();

map.put("id", s.getId());

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

map.put("createTime", sdf.format(new Date(s.getCreationTime())));

map.put("lastAccessedTime",sdf.format(new Date(s.getLastAccessedTime())) );

map.put("ip", s.getAttribute("ip"));//ip信息在characterFilter中补

map.put("user", s.getAttribute("user"));

list.add(map);

}

}

request.setAttribute("onlines", list);

request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);

}

}

KickOutServlet.java

package cn.hncu.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class KickOutServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String id=request.getParameter("id");

Map onlines=(Map) request.getServletContext().getAttribute("onlines");

if (onlines!=null) {

if (onlines.containsKey(id)) {

onlines.get(id).invalidate();

}

}

request.getRequestDispatcher("/servlet/ShowServlet").forward(request, response);

}

}

LoginOutServlet.java

package cn.hncu.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class LoginOutServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.getSession().invalidate();

response.sendRedirect(request.getContextPath()+"/index.jsp");

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:基于JAVA每月运势api调用代码实例
下一篇:Java注解Annotation解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~