Hi,
Jay SenSharma

Jay SenSharma

Related Post:  httpsession-finding-total-active-users-count-using-jmx/

Here is a Simple demonstration of using “<monitoring-attribute-name>” tag in “weblogic.xml” which helps us in monitoring the Unique users who have logged in to  Application deployed on the WebLogic Server. Many times we (as Administrator) want to know who has actually logged in to the application…?

Description: <monitoring-attribute-name> in “weblogic.xml”
HTTP Sessions are identified with a monitoring ID. By default, the monitoring ID for a given HTTP session is a random string (not the same as a session ID for security reasons). This monitoring ID can be configured by setting the monitoring-attribute-name element in session-descriptor of the weblogic.xml deployment descriptor and then setting a session attribute the defined monitoring-attribute-name. The toString() of the session attribute value will then be used as a monitoring ID.
The monitoring-attribute-name element is useful for tagging session runtime information for different sessions. For example, you can set it to “username”, if you have a “username” attribute that is unique.
The WebAppComponentRuntimeBean.getSessionIds() method returns an array of session attribute values with this name. If it is not set, it returns an array of randomly generated Strings.

Step1).We are going to develop a Simple WebApplication …So please create a Directory somewhere in your filesystem with name “SessionMonitoringWebApp”:

Example: C:SessionMonitoringWebApp

Step2).Now create one “index.jsp” file like following inside “C:SessionMonitoringWebApp” like following:

<html>
<head><title>Login Page</title><head>
<body>
<center> <h1>Please Login</h1>
<table border=20 bgcolor=maroon>
<form action=validated.jsp>
<TR><td><font color=white>Enter UserName:</font> </td><td><input type="text" name="username"></td></TR>
<TR><td><font color=white>Enter Password: </font></td><td><input type="password" name="userpass"></td></TR>
<TR><td><input type="submit" value="Login"></td><td><input type="reset" value="Reset"></td></TR>
</form>
</table>
</body>
</html>
Step3).Now create one “validated.jsp” file like following inside “C:SessionMonitoringWebApp” like following:
<%
String userName=request.getParameter("username");
String userPass=request.getParameter("userpass");
if (userName.equals("weblogic") && userPass.equals("weblogic"))
{
System.out.println("nt User Has Logged in At : "+new java.util.Date());
//  NOTE: We are going to declare "MyMainSessionAttribute" as a Session Monitoring Attribute in "weblogic.xml" //
// You can choose any HttpSession attribute name as a HttpSession Monitoring Attribute Name //
session.setAttribute("MyMainSessionAttribute",userName);
out.println("<h2><center>Welcome Mr. "+userName+"</center></h2>");
out.println("<center><h3>Please check the AdminConsole to Monitor the HttpSession Who has Logged in....?</h3>");
out.println("</h4><a href=logout.jsp>Click Here to Logout</a></h4>");
}
%>
Step4).Now create one “logout.jsp” file like following inside “C:SessionMonitoringWebApp” like following:
<%
String userName=(String)session.getAttribute("MyMainSessionAttribute");
if ( userName!=null)
{
System.out.println("nt Logging Out...Mr. "+userName);
session.invalidate();
System.out.println("nt Logged Out...Mr. "+userName+" Successfully");
out.println("<center><h3>Logged Out...Mr. "+userName+" Successfully </h3>");
out.println("<h3>Do u want to Re-Login <a href=index.jsp>Click Here to Login Again</a></h4></center>");
}
%>
Step5). Now we need to provide “web.xml” inside “C:SessionMonitoringWebAppWEB-INF” like following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step6).Now we need to provide “weblogic.xml” inside “C:SessionMonitoringWebAppWEB-INF” like following:
<?xml version="1.0" encoding="ISO-8859-1"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90">
<session-descriptor>
<timeout-secs>600</timeout-secs>
<monitoring-attribute-name>MyMainSessionAttribute</monitoring-attribute-name>
</session-descriptor>
<container-descriptor>
<session-monitoring-enabled>true</session-monitoring-enabled>
</container-descriptor>
</weblogic-web-app>
Step7).Application is ready to deploy…Please deploy the Application on WebLogic Server.

Step8).Now Login to Admin Console and Click on “Deployments–>SessionMonitoringWebApp–>Monitoring–>Sessions” and then customize the table to display the “Monitoring Id” like following:

Customizing the Session Monitoring Table

Customizing the Session Monitoring Table

Step9).Access your Application like following and Login to the Application:

Logging in to TestApplication using Monitoring Attribute

Logging in to TestApplication using Monitoring Attribute

Successfully Created HttpSession inside Application

Successfully Created HttpSession inside Application

Step10).Now move the AdminConsole again to check if u are able to see the “Monitoring ID”:

Monitoring the HttpSession Monitoring ID in Console

Monitoring the HttpSession Monitoring ID in Console

Step11). Once you logged out from the application u will be able to see that the Monitoring ID is changed…because the User is already Logged Out.

Monitoring HttpSession using <monitoring-attribute-name>

Monitoring HttpSession using

.
.
Thanks
Jay SenSharma
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.