查找从给定客户端IP创建的活动会话数 [英] Find number of active sessions created from a given client IP
问题描述
有没有办法确定从给定客户端IP地址创建的活动会话数?
Is there a way to determine the number of active sessions created from a given client IP address?
推荐答案
标准Servlet API不提供相关设施。最好的办法是维护 自己映射< HttpSession,String>
(其中 String
是IP地址)并检查每个 ServletRequest
如果 HttpSession#isNew()
并将其添加到 Map
以及 ServletRequest#getRemoteAddr()
。然后,您可以使用 download.oracle.com/javase/6/docs/api/java/util/Map.html#values%28%29\"rel =noreferrer>
。您只需要确保在地图中删除 Map#values()
HttpSession
.oracle.com / javaee / 5 / api / javax / servlet / http / HttpSessionListener.html#sessionDestroyed%28javax.servlet.http.HttpSessionEvent%29rel =noreferrer> HttpSessionListener #sessionDestroyed()
。
The standard Servlet API doesn't offer facilities for that. Best what you can do is to maintain a Map<HttpSession, String>
yourself (where the String
is the IP address) with and check on every ServletRequest
if the HttpSession#isNew()
and add it to the Map
along with ServletRequest#getRemoteAddr()
. Then you can get the amount of IP addresses with an active session using Collections#frequency()
on Map#values()
. You only need to ensure that you remove the HttpSession
from the Map
during HttpSessionListener#sessionDestroyed()
.
这一切都可以在一个监听器
中实现 ServletContextListener
, HttpSessionListener
和 ServletRequestListener
。
This all can be done in a single Listener
implementing the ServletContextListener
, HttpSessionListener
and ServletRequestListener
.
这是一个启动示例:
public class SessionCounter implements ServletContextListener, HttpSessionListener, ServletRequestListener {
private static final String ATTRIBUTE_NAME = "com.example.SessionCounter";
private Map<HttpSession, String> sessions = new ConcurrentHashMap<HttpSession, String>();
@Override
public void contextInitialized(ServletContextEvent event) {
event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
}
@Override
public void requestInitialized(ServletRequestEvent event) {
HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
HttpSession session = request.getSession();
if (session.isNew()) {
sessions.put(session, request.getRemoteAddr());
}
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
sessions.remove(event.getSession());
}
@Override
public void sessionCreated(HttpSessionEvent event) {
// NOOP. Useless since we can't obtain IP here.
}
@Override
public void requestDestroyed(ServletRequestEvent event) {
// NOOP. No logic needed.
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// NOOP. No logic needed. Maybe some future cleanup?
}
public static SessionCounter getInstance(ServletContext context) {
return (SessionCounter) context.getAttribute(ATTRIBUTE_NAME);
}
public int getCount(String remoteAddr) {
return Collections.frequency(sessions.values(), remoteAddr);
}
}
在<$ c $中定义c> web.xml 如下所示:
<listener>
<listener-class>com.example.SessionCounter</listener-class>
</listener>
您可以在以下任何servlet中使用它:
You can use it in any servlet like follows:
SessionCounter counter = SessionCounter.getInstance(getServletContext());
int count = counter.getCount("127.0.0.1");
这篇关于查找从给定客户端IP创建的活动会话数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!