Skip to content

Instantly share code, notes, and snippets.

Created April 20, 2017 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f9c30c3c6a17492c7fe3949992c4094e to your computer and use it in GitHub Desktop.
Save anonymous/f9c30c3c6a17492c7fe3949992c4094e to your computer and use it in GitHub Desktop.
JAVA - Find number of active sessions created from a given client IP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script type="text/javascript" src="/static/js/analytics.js?v=1492440594.0" charset="utf-8"></script>
<script type="text/javascript">archive_analytics.values.service='wb';archive_analytics.values.server_name='wwwb-app22.us.archive.org';archive_analytics.values.server_ms=461;</script>
<script type="text/javascript" src="/static/js/wbhack.js?v=1492440594.0" charset="utf-8"></script>
<script type="text/javascript">
__wbhack.init('');
</script>
<link rel="stylesheet" type="text/css" href="/static/css/banner-styles.css?v=1492440594.0" />
<link rel="stylesheet" type="text/css" href="/static/css/iconochive.css?v=1492440594.0" />
<!-- End Wayback Rewrite JS Include -->
<title>Big Oh Software - Java Enterpise Platform - Request Observer Listener</title>
<!-- Meta tags -->
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="keywords" content="ServletRequestListener, ServletRequest, ServletResponse, log"/>
<meta name="description" content="A tutorial describing a ServletRequestListener that exposes the steps in servicing a ServletRequest"/>
<meta name="verify-v1" content="0bcmfqPEzugETUaSrFgoU2IBqrG849cNLmRJwDldaJ8="/>
<!-- Style sheets -->
<link href="/web/20111211181624cs_/http://www.big-oh.net/BigOhSoftwareWeb/css/elegance.css" rel="stylesheet" type="text/css"/>
<!-- Javascript files -->
<script type="text/javascript" src="/web/20111211181624js_/http://www.big-oh.net/BigOhSoftwareWeb/js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="/web/20111211181624js_/http://www.big-oh.net/BigOhSoftwareWeb/js/bigOhSoftwareWeb.js"></script>
</head>
<body><!-- BEGIN WAYBACK TOOLBAR INSERT -->
<script type="text/javascript" src="/static/js/timestamp.js?v=1492440594.0" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/graph-calc.js?v=1492440594.0" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/auto-complete.js?v=1492440594.0" charset="utf-8"></script>
<script type="text/javascript" src="/static/js/toolbar.js?v=1492440594.0" charset="utf-8"></script>
<style type="text/css">
body {
margin-top:0 !important;
padding-top:0 !important;
min-width:800px !important;
}
.wb-autocomplete-suggestions {
text-align: left; cursor: default; border: 1px solid #ccc; border-top: 0; background: #fff; box-shadow: -1px 1px 3px rgba(0,0,0,.1);
position: absolute; display: none; z-index: 2147483647; max-height: 254px; overflow: hidden; overflow-y: auto; box-sizing: border-box;
}
.wb-autocomplete-suggestion { position: relative; padding: 0 .6em; line-height: 23px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 1.02em; color: #333; }
.wb-autocomplete-suggestion b { font-weight: bold; }
.wb-autocomplete-suggestion.selected { background: #f0f0f0; }
</style>
<div id="wm-ipp" lang="en" style="display:none;direction:ltr;">
<div style="position:fixed;left:0;top:0;width:100%!important">
<div id="wm-ipp-inside">
<table style="width:100%;">
<tbody>
<tr>
<td id="wm-logo">
<a href="/web/" title="Wayback Machine home page"><img src="/static/images/toolbar/wayback-toolbar-logo.png" alt="Wayback Machine" width="110" height="39" border="0" /></a>
</td>
<td class="c">
<table style="margin:0 auto;">
<tbody>
<tr>
<td class="u" colspan="2">
<form target="_top" method="get" action="/web/submit" name="wmtb" id="wmtb"><input type="text" name="url" id="wmtbURL" value="http://www.big-oh.net/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp" style="width:400px;" onfocus="this.focus();this.select();" /><input type="hidden" name="type" value="replay" /><input type="hidden" name="date" value="20111211181624" /><input type="submit" value="Go" /></form>
</td>
<td class="n" rowspan="2">
<table>
<tbody>
<!-- NEXT/PREV MONTH NAV AND MONTH INDICATOR -->
<tr class="m">
<td class="b" nowrap="nowrap"><a href="http://web.archive.org/web/20111110234545/http://www.big-oh.net:80/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp" title="10 Nov 2011"><strong>Nov</strong></a></td>
<td class="c" id="displayMonthEl" title="You are here: 18:16:24 Dec 11, 2011">DEC</td>
<td class="f" nowrap="nowrap">Jan</td>
</tr>
<!-- NEXT/PREV CAPTURE NAV AND DAY OF MONTH INDICATOR -->
<tr class="d">
<td class="b" nowrap="nowrap"><a href="http://web.archive.org/web/20111110234545/http://www.big-oh.net:80/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp" title="23:45:45 Nov 10, 2011"><img src="/static/images/toolbar/wm_tb_prv_on.png" alt="Previous capture" width="14" height="16" border="0" /></a></td>
<td class="c" id="displayDayEl" style="width:34px;font-size:24px;" title="You are here: 18:16:24 Dec 11, 2011">11</td>
<td class="f" nowrap="nowrap"><img src="/static/images/toolbar/wm_tb_nxt_off.png" alt="Next capture" width="14" height="16" border="0" /></td>
</tr>
<!-- NEXT/PREV YEAR NAV AND YEAR INDICATOR -->
<tr class="y">
<td class="b" nowrap="nowrap">2010</td>
<td class="c" id="displayYearEl" title="You are here: 18:16:24 Dec 11, 2011">2011</td>
<td class="f" nowrap="nowrap">2012</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="s">
<div id="wm-nav-captures">
<a class="t" href="/web/20111211181624*/http://www.big-oh.net/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp" title="See a list of every capture for this URL">2 captures</a>
<div class="r" title="Timespan for captures of this URL">10 Nov 2011 - 11 Dec 2011</div>
</div>
</td>
<td class="k">
<a href="" id="wm-graph-anchor">
<div id="wm-ipp-sparkline" title="Explore captures for this URL" style="position: relative">
<canvas id="wm-sparkline-canvas" width="550" height="27" border="0"></canvas>
</div>
</a>
</td>
</tr>
</tbody>
</table>
</td>
<td class="r">
<a href="#" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=http://web.archive.org/web/20111211181624/http://www.big-oh.net:80/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp', '', 'height=400,width=600'); return false;" title="Share on Facebook" style="top: 4px; right: 16px;" target="_blank"><span class="iconochive-facebook" style="color:#3b5998;font-size:160%;"></span></a>
<a href="#" onclick="window.open('https://twitter.com/intent/tweet?text=http://web.archive.org/web/20111211181624/http://www.big-oh.net:80/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp&amp;via=internetarchive', '', 'height=400,width=600'); return false;" title="Share on Twitter" style="bottom:14px;right:16px;" target="_blank"><span class="iconochive-twitter" style="color:#1dcaff;font-size:160%;"></span></a>
<a href="http://faq.web.archive.org/" title="Get some help using the Wayback Machine" style="bottom:14px;right:5px;padding-right:0;"><span class="iconochive-question" style="color:rgb(87,186,244);font-size:160%;"></span></a>
<a href="#close" onclick="__wm.h(event);return false;" style="top:-2px;right:0;padding-right:0;" title="Close the toolbar"><span class="iconochive-remove-circle" style="color:#888888;font-size:240%;"></span></a>
<a id="wm-expand" class="wm-btn" href="#expand" onclick="__wm.ex(event);return false;"><span class="iconochive-down-solid"></span> <span style="font-size:80%">About this capture</span></a>
</td>
</tr>
</tbody>
</table>
<div id="wm-capinfo" style="border-top:1px solid #777;display:none;">
<div style="background-color:#353535;color:#aaa;font-weight:bold;text-align:center;"><a class="wm-selector selected" href="javascript:void(0)">COLLECTED BY</a></div>
<div style="padding:3px;position:relative;">
<div style="display:inline-block;vertical-align:top;width:50%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls);"></span>
Organization: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
</div>
</div>
<div style="display:inline-block;vertical-align:top;width:49%;">
<span class="c-logo" style="background-image:url(https://archive.org/services/img/alexacrawls)"></span>
<div>Collection: <a style="color:#33f;" href="https://archive.org/details/alexacrawls" target="_new"><span class="wm-title">Alexa Crawls</span></a></div>
<div style="max-height:75px;overflow:hidden;position:relative;">
<div style="position:absolute;top:0;left:0;width:100%;height:75px;background:linear-gradient(to bottom,rgba(255,255,255,0) 0%,rgba(255,255,255,0) 90%,rgba(255,255,255,255) 100%);"></div>
Starting in 1996, <a href="http://www.alexa.com/">Alexa Internet</a> has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the <a href="http://web.archive.org/">Wayback Machine</a> after an embargo period.
</div>
</div>
</div></div></div></div></div><script type="text/javascript">
__wm.bt(550,27,25,2,"web","http://www.big-oh.net/BigOhSoftwareWeb/content/tutorials/requestObserverListener.jsp","2011-12-11",1996);
</script>
<!-- END WAYBACK TOOLBAR INSERT -->
<!-- start of 'wrap' div -->
<div id="wrap">
<!-- start of 'header' div -->
<div id="header">
<h1>Big Oh Software</h1>
<h2>... computer scientists find big oh!!</h2>
</div>
<!-- end of 'header' div -->
<!-- start of 'nav' div -->
<div id="nav">
<ul>
<li><a href="/web/20111211181624/http://www.big-oh.net/BigOhSoftwareWeb/index.jsp" class="" title="Home">Home</a></li>
<li><a href="/web/20111211181624/http://www.big-oh.net/BigOhSoftwareWeb/content/frameworks/index.jsp" class="" title="Frameworks">Frameworks</a></li>
<li><a href="/web/20111211181624/http://www.big-oh.net/BigOhSoftwareWeb/content/tutorials/index.jsp" class="selected" title="Tutorials">Tutorials</a></li>
<li><a href="/web/20111211181624/http://www.big-oh.net/BigOhSoftwareWeb/content/studyguides/index.jsp" class="" title="Study Guides">Study Guides</a></li>
</ul>
</div>
<!-- end of 'nav' div -->
<!-- start of 'content' div -->
<div id="content">
<!-- start of 'page' div -->
<div id="page">
<!-- start of 'content' block -->
<h3>Java Enterpise Platform - Request Observer Listener<a name="Java_Enterpise_Platform___Request_Observer_Listener"></a></h3>
<!-- start of 'subContent' block -->
<h5>Motivation<a name="Motivation"></a></h5>
<p>
Whereas <a href="/web/20111211181624/http://www.big-oh.net/BigOhSoftwareWeb/content/tutorials/responseObserverFilter.jsp">observing ServletResponses</a> is non-trivial, Sun makes it much easier to observe ServletRequests by including the ServletRequestListener interface in the J2EE servlet specification.
Observing ServletRequests is not only easy, it's also very useful when you want to record or log the request that was made.
Recording and logging ServletRequests with a listener is preferable to doing that same work in the requested resource (servlet, JSP, etc.) since the former strategy achieves a <a href="http://web.archive.org/web/20111211181624/http://en.wikipedia.org/wiki/Separation_of_concerns">separation of concerns</a> that is difficult to achieve with the latter strategy.
</p>
<p>
This tutorial first describes a reusable, abstract ServletRequestListener that can be extended to observe ServletRequests in myriad meaningful ways.
After introducing the abstract observer listener, we'll see that the listener can easily be extended to record ServletRequests to the application container's log file.
</p>
<!-- end 'subContent' block -->
<!-- start of 'subContent' block -->
<h5>The Code<a name="The_Code"></a></h5>
<p>
The ServletRequestListener API defines two methods, of which we need only extend the first.
</p>
<!--start of 'code' block -->
<div style="height:375px;width:95%; overflow:auto; border-width:2px; border-style:dotted; padding: 10px 10px 10px 10px; margin-bottom:15px; background-color:#E1D3C1; color:#A37E4E;">
<pre>
package com.bigohsoftware.common.listener.request;
import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
public abstract class RequestObserverListener implements ServletRequestListener
{
protected abstract void doOnServletRequestInitialized(ObservedServletRequest observedServletRequest);
public void requestInitialized(ServletRequestEvent event)
{
ServletRequest request = event.getServletRequest();
if (request instanceof HttpServletRequest)
{
doOnServletRequestInitialized(new ObservedHttpServletRequest((HttpServletRequest) request));
}
else
{
doOnServletRequestInitialized(new ObservedServletRequest(request));
}
}
public void requestDestroyed(ServletRequestEvent event)
{
// do nothing
}
}
</pre>
</div>
<!--end 'code' block -->
<p>
You'll notice that the listener class cataloged above makes use of two helper classes that adapt the original ServletRequest into an object that is more easily observed, recorded and logged.
</p>
<!--start of 'code' block -->
<div style="height:250px;width:95%; overflow:auto; border-width:2px; border-style:dotted; padding: 10px 10px 10px 10px; margin-bottom:15px; background-color:#E1D3C1; color:#A37E4E;">
<pre>
package com.bigohsoftware.common.listener.request;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import javax.servlet.ServletRequest;
public class ObservedServletRequest
{
protected final String requestorIP;
protected final String requestorHostName;
protected final int requestedPort;
protected final String requestedProtocol;
protected final Date requestReceivedAt;
public ObservedServletRequest(ServletRequest request)
{
requestorIP = request.getRemoteAddr();
String hostName = request.getRemoteHost();
try
{
if (hostName.equals(request.getRemoteAddr()))
{
InetAddress addr = InetAddress.getByName(request.getRemoteAddr());
hostName = addr.getHostName();
}
if (InetAddress.getLocalHost().getHostAddress().equals(request.getRemoteAddr()))
{
hostName = "Local Host";
}
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
requestorHostName = hostName;
requestedPort = request.getServerPort();
requestedProtocol = request.getProtocol();
requestReceivedAt = new Date();
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("Received a " + requestedProtocol + " request");
sb.append(" from " + requestorHostName + " (" + requestorIP + ")");
return sb.toString();
}
}
</pre>
</div>
<!--end 'code' block -->
<!--start of 'code' block -->
<div style="height:250px;width:95%; overflow:auto; border-width:2px; border-style:dotted; padding: 10px 10px 10px 10px; margin-bottom:15px; background-color:#E1D3C1; color:#A37E4E;">
<pre>
package com.bigohsoftware.common.listener.request;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class ObservedHttpServletRequest extends ObservedServletRequest
{
protected final String httpMethod;
protected final String resourceRequested;
protected final String queryString;
protected final String contextName;
protected final String containerUserName;
protected final String jSessionId;
ObservedHttpServletRequest(HttpServletRequest request)
{
super(request);
httpMethod = request.getMethod();
resourceRequested = request.getRequestURL().toString();
queryString = request.getQueryString();
contextName = request.getContextPath();
containerUserName = request.getRemoteUser();
HttpSession session = request.getSession(false);
jSessionId = (session == null) ? null : session.getId();
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append(" for resource " + resourceRequested + " (" + httpMethod + ")");
sb.append(" -- jSessionId: " + ((jSessionId == null || jSessionId.equals("")) ? "undefined" : jSessionId));
sb.append(" -- userName: " + ((containerUserName == null || containerUserName.equals("")) ? "unknown" : containerUserName));
return sb.toString();
}
}
</pre>
</div>
<!--end 'code' block -->
<!-- end 'subContent' block -->
<!-- start of 'subContent' block -->
<h5>An Example: Request Logger Listener<a name="An_Example__Request_Logger_Listener"></a></h5>
<p>
The abstract listener class described above can be extended to provide many different types of behavior when a ServletRequest is observed.
For example, we might define a concrete listener that persists request details to a database or we might define a concrete listener that logs the details of the request to the application container's log file.
The code below implements the latter example.
</p>
<!--start of 'code' block -->
<div style="height:180px;width:95%; overflow:auto; border-width:2px; border-style:dotted; padding: 10px 10px 10px 10px; margin-bottom:15px; background-color:#E1D3C1; color:#A37E4E;">
<pre>
package com.bigohsoftware.common.listener.request;
public class RequestLoggerListener extends RequestObserverListener
{
@Override
protected void doOnServletRequestInitialized(ObservedServletRequest observedServletRequest)
{
System.out.println(observedServletRequest);
}
}
</pre>
</div>
<!--end 'code' block -->
<p>
By applying the above logging listener to a web application, you'll see that new ServletRequests are logged in this format:
<br/>
<i>Received a HTTP/1.1 request from localhost (127.0.0.1) for resource http://127.0.0.1:8080/CommonWeb/ (GET) -- jSessionId: B46435C89F9FABCE571AF701E0BC6820 -- userName: unknown</i>
</p>
<!-- end 'subContent' block -->
<!-- end 'content' block -->
</div>
<!-- end of 'page' div -->
<!-- start of 'sidebar' div -->
<div id="sidebar">
<!-- start of 'content' block -->
<h4>Related Links<a name="Related_Links"></a></h4>
<ul>
<li><a href="http://web.archive.org/web/20111211181624/http://e-docs.bea.com/wls/docs100/webapp/app_events.html">Servlet Listener Overview</a></li>
<li><a href="http://web.archive.org/web/20111211181624/http://en.wikipedia.org/wiki/Observer_pattern">Observer Pattern</a></li>
</ul>
<!-- end 'content' block -->
<!-- start of 'content' block -->
<h4>Shameless Advertising<a name="Shameless_Advertising"></a></h4>
<p>
This site relies on advertising revenue to fund its operating expenses.
</p>
<p>
<script type="text/javascript"><!--
google_ad_client = "pub-0628795409344714";
/* 160x600, text only */
google_ad_slot = "9859786364";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript" src="http://web.archive.org/web/20111211181624js_/http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
<!-- end 'content' block -->
</div>
<!-- end of 'sidebar' div -->
<!-- start of 'footer' div -->
<div id="footer">
<p id="legal">Copyright &copy; 2009 Big Oh Software. All Rights Reserved.</p>
<p id="thank-yous">
Powered by <a href="http://web.archive.org/web/20111211181624/http://tomcat.apache.org/" title="Tomcat Servlet Engine">Tomcat.</a> Design courtesy of <a href="http://web.archive.org/web/20111211181624/http://www.spyka.net" title="Spyka Webmaster">Spyka</a>. Valid <a href="http://web.archive.org/web/20111211181624/http://validator.w3.org/check/referer" title="Valid XHTML Strict?">XHTML</a> and <a href="http://web.archive.org/web/20111211181624/http://jigsaw.w3.org/css-validator/check/referer" title="Valid CSS?">CSS</a>?
</p>
</div>
<!-- end of 'footer' div -->
</div>
<!-- end of 'content' div -->
</div>
<!-- end of 'wrap' div -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment