public static synchronized HttpSession getSession(String session_id) {
if (session_id == null)
return null;
return (HttpSession)mymap.get(session_id);
}
public static HashMap<String, HttpSession> getMymap() {
return mymap;
}
}
Session监听器
public class SessionCounter implements HttpSessionListener {
private static int activeSessions = 0;
public void sessionCreated(HttpSessionEvent se) {
MySessionContext.AddSession(se.getSession());
activeSessions++;
System.out.println(“++++++++玩家上线了++++++++”);
}
public void sessionDestroyed(HttpSessionEvent se) {
if(activeSessions > 0)
activeSessions–;
HttpSession session = se.getSession();
MySessionContext.DelSession(session);
}
public static int getActiveSessions() {
return activeSessions;
}
}
踢人下线过滤器核心代码
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
String localIp = request.getRemoteAddr();//获取本地ip
HttpSession session = null;
String user_id = (String)request.getParameter(“userId”); //登录请求时填写的
if(StringUtils.isNotBlank(user_id))
{
session = getLoginedUserSession(user_id);
}
String loginedIp = null;
if(session!=null)
{
loginedIp = (String)session.getAttribute(ESessionKey.LoginIP.key);//获取已登录者ip(如果有)
}
if(StringUtils.isNotBlank(loginedIp) && !localIp.equals(loginedIp)){
MySessionContext.DelSession(session);//踢人–找到并销毁Session
request.setAttribute(“msg”, “您的账号在其它ip登录,您被踢下线了!”);
request.getRequestDispatcher(“/login.jsp”).forward(request, response);
}else{
chain.doFilter(request, response);//放行
}
}
Session过期过滤器 核心代码
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
System.out.println(“过滤请求…”);
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
//session中获取用户名信息
String userId = (String)request.getSession().getAttribute(“userId”);
String admin = (String)request.getSession().getAttribute(“admin”);
//普通用户登录过滤,如果用户名为空说明带有登录标记的Session过期了
if (userId==null||“”.equals(userId.toString()) ) {
//超级管理员过滤
if(admin==null||“”.equals(admin.toString())){
response.sendRedirect(request.getContextPath()+ADMIN_URL);
return ;
}
//如果普通用户和超级管理员都没有登陆内容,说明登录过期
System.out.println(“登录过期,请重新登录!”);
response.sendRedirect(request.getContextPath()+LOGIN_URL);
PrintWriter printWriter = response.getWriter();
String relogin = “登录过期,请重新登录!”;
printWriter.write(relogin);
printWriter.flush();
printWriter.close();
return ;
}
//过滤通过,放行
chain.doFilter(request, response);
System.out.println(“过滤响应!”);
}
web.xml配置
TickFronterFilter
com.fengyun.web.filter.TickFronterFilter
TickFronterFilter
/login.html
com.fengyun.web.filter.SessionCounter
Loginfilter
com.fengyun.web.filter.LoginOverdueFilter
Loginfilter
/material/*
…等等需要过滤的url地址…当然可以使用通配方式写(此处不详述)
/operate_editeCompact.html
REQUEST
FORWARD
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
最后
无论是哪家公司,都很重视基础,大厂更加重视技术的深度和广度,面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。
针对以上面试技术点,我在这里也做一些分享,希望能更好的帮助到大家。
[外链图片转存中…(img-oMwUuuwi-1714327425027)]
[外链图片转存中…(img-i38CXmep-1714327425028)]
[外链图片转存中…(img-lTIDhAtI-1714327425028)]