added session details manager.
parent
7212b4a559
commit
86af820114
|
@ -1,154 +1,168 @@
|
||||||
package tomcat.request.session.redis;
|
package tomcat.request.session.redis;
|
||||||
|
|
||||||
import java.io.IOException;
|
import tomcat.request.session.manager.SessionDetailsManager;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.annotation.WebServlet;
|
import javax.servlet.annotation.WebServlet;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
|
||||||
@WebServlet("/")
|
@WebServlet("/")
|
||||||
public class SessionManagerTest extends HttpServlet {
|
public class SessionManagerTest extends HttpServlet {
|
||||||
|
|
||||||
private static final long serialVersionUID = 7464510533820701851L;
|
private static final long serialVersionUID = 7464510533820701851L;
|
||||||
|
|
||||||
/**
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||||
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
|
String action = request.getParameter("action");
|
||||||
* response)
|
action = (action == null) ? "" : action;
|
||||||
*/
|
String responseData;
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
switch (action.toUpperCase()) {
|
||||||
throws ServletException, IOException {
|
case "SET":
|
||||||
|
responseData = setSessionValues(request.getSession());
|
||||||
|
break;
|
||||||
|
case "GET":
|
||||||
|
responseData = getSessionValues(request.getSession(), action);
|
||||||
|
break;
|
||||||
|
case "SESSION_DETAILS":
|
||||||
|
responseData = getSessionDetails();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
responseData = getActions(request);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sendResponse(response, responseData);
|
||||||
|
}
|
||||||
|
|
||||||
String action = request.getParameter("action");
|
/** method to send response */
|
||||||
action = (action == null) ? "" : action;
|
private void sendResponse(HttpServletResponse response, String responseData) throws IOException {
|
||||||
String responseData = null;
|
response.setContentType("text/html");
|
||||||
|
response.setStatus(HttpServletResponse.SC_OK);
|
||||||
|
response.getWriter().println(responseData);
|
||||||
|
}
|
||||||
|
|
||||||
switch (action.toUpperCase()) {
|
/** method to get actions */
|
||||||
case "SET":
|
private String getActions(HttpServletRequest request) {
|
||||||
responseData = setSessionValues(request.getSession());
|
StringBuilder xml = new StringBuilder();
|
||||||
break;
|
|
||||||
case "GET":
|
|
||||||
responseData = getSessionValues(request.getSession(), action);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
responseData = getActions(request);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
sendResponse(response, responseData);
|
xml.append("<!DOCTYPE html>");
|
||||||
}
|
xml.append("<html>");
|
||||||
|
xml.append("<head>");
|
||||||
|
xml.append("<meta charset='UTF-8'>");
|
||||||
|
xml.append("<title>tomcat-cluster-redis-session-manager-test</title>");
|
||||||
|
xml.append("</head>");
|
||||||
|
xml.append("<body>");
|
||||||
|
xml.append("<h2>tomcat-cluster-redis-session-manager-test</h2>");
|
||||||
|
xml.append("<h4>actions</h4>");
|
||||||
|
|
||||||
/**
|
String url = request.getRequestURL().toString();
|
||||||
* method to send response
|
url = (url.contains("?action=") ? (url.substring(0, url.indexOf("?action="))) : url).concat("?action=");
|
||||||
*
|
|
||||||
* @param response
|
|
||||||
* @param responseData
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
private void sendResponse(HttpServletResponse response, String responseData) throws IOException {
|
|
||||||
response.setContentType("text/html");
|
|
||||||
response.setStatus(HttpServletResponse.SC_OK);
|
|
||||||
response.getWriter().println(responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
xml.append("<a href='");
|
||||||
* method to get actions
|
xml.append(url);
|
||||||
*
|
xml.append("SET");
|
||||||
* @param request
|
xml.append("'>SET</a>");
|
||||||
* @return
|
xml.append("<br><br>");
|
||||||
*/
|
xml.append("<a href='");
|
||||||
private String getActions(HttpServletRequest request) {
|
xml.append(url);
|
||||||
StringBuffer xml = new StringBuffer();
|
xml.append("GET");
|
||||||
|
xml.append("'>GET</a>");
|
||||||
|
xml.append("<br><br>");
|
||||||
|
xml.append("<a href='");
|
||||||
|
xml.append(url);
|
||||||
|
xml.append("SESSION_DETAILS");
|
||||||
|
xml.append("'>SESSION_DETAILS</a>");
|
||||||
|
|
||||||
xml.append("<!DOCTYPE html>");
|
xml.append("</body>");
|
||||||
xml.append("<html>");
|
xml.append("</html>");
|
||||||
xml.append("<head>");
|
|
||||||
xml.append("<meta charset='UTF-8'>");
|
|
||||||
xml.append("<title>tomcat-cluster-redis-session-manager-test</title>");
|
|
||||||
xml.append("</head>");
|
|
||||||
xml.append("<body>");
|
|
||||||
xml.append("<h2>tomcat-cluster-redis-session-manager-test</h2>");
|
|
||||||
xml.append("<h4>actions</h4>");
|
|
||||||
|
|
||||||
String url = request.getRequestURL().toString();
|
return xml.toString();
|
||||||
url = (url.contains("?action=") ? (url.substring(0, url.indexOf("?action="))) : url).concat("?action=");
|
}
|
||||||
|
|
||||||
xml.append("<a href='");
|
/** method to set session values */
|
||||||
xml.append(url);
|
private String setSessionValues(HttpSession session) {
|
||||||
xml.append("SET");
|
for (int i = 0; i < 10; i++) {
|
||||||
xml.append("'>SET</a>");
|
session.setAttribute("test-" + i, "test-" + new Date().getTime());
|
||||||
xml.append("<br><br>");
|
}
|
||||||
xml.append("<a href='");
|
return getSessionValues(session, "SET");
|
||||||
xml.append(url);
|
}
|
||||||
xml.append("GET");
|
|
||||||
xml.append("'>GET</a>");
|
|
||||||
|
|
||||||
xml.append("</body>");
|
/** method to get session values */
|
||||||
xml.append("</html>");
|
private String getSessionValues(HttpSession session, String action) {
|
||||||
|
StringBuilder xml = new StringBuilder();
|
||||||
|
xml.append("<!DOCTYPE html>");
|
||||||
|
xml.append("<html>");
|
||||||
|
xml.append("<head>");
|
||||||
|
xml.append("<meta charset='UTF-8'>");
|
||||||
|
xml.append("<title>tomcat-cluster-redis-session-manager-test</title>");
|
||||||
|
xml.append("</head>");
|
||||||
|
xml.append("<body>");
|
||||||
|
xml.append("<h2>tomcat-cluster-redis-session-manager-test-results</h2>");
|
||||||
|
xml.append("<h4>action: ");
|
||||||
|
xml.append(action);
|
||||||
|
xml.append("</h4>");
|
||||||
|
xml.append("<table width='30%' border='1' style='text-align: center;'>");
|
||||||
|
xml.append("<tr>");
|
||||||
|
xml.append("<th width='50%'>Key</th>");
|
||||||
|
xml.append("<th width='50%'>Value</th>");
|
||||||
|
xml.append("</tr>");
|
||||||
|
|
||||||
return xml.toString();
|
Enumeration<String> names = session.getAttributeNames();
|
||||||
}
|
while (names.hasMoreElements()) {
|
||||||
|
String name = names.nextElement();
|
||||||
|
xml.append("<tr>");
|
||||||
|
xml.append("<td>");
|
||||||
|
xml.append(name);
|
||||||
|
xml.append("</td>");
|
||||||
|
xml.append("<td>");
|
||||||
|
xml.append(session.getAttribute(name));
|
||||||
|
xml.append("</td>");
|
||||||
|
xml.append("</tr>");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
xml.append("</table>");
|
||||||
* method to set session values
|
xml.append("</body>");
|
||||||
*
|
xml.append("</html>");
|
||||||
* @param session
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private String setSessionValues(HttpSession session) {
|
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
session.setAttribute("test-" + i, "test-" + new Date().getTime());
|
|
||||||
}
|
|
||||||
return getSessionValues(session, "SET");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return xml.toString();
|
||||||
* method to get session values
|
}
|
||||||
*
|
|
||||||
* @param session
|
|
||||||
* @param action
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private String getSessionValues(HttpSession session, String action) {
|
|
||||||
StringBuffer xml = new StringBuffer();
|
|
||||||
xml.append("<!DOCTYPE html>");
|
|
||||||
xml.append("<html>");
|
|
||||||
xml.append("<head>");
|
|
||||||
xml.append("<meta charset='UTF-8'>");
|
|
||||||
xml.append("<title>tomcat-cluster-redis-session-manager-test</title>");
|
|
||||||
xml.append("</head>");
|
|
||||||
xml.append("<body>");
|
|
||||||
xml.append("<h2>tomcat-cluster-redis-session-manager-test-results</h2>");
|
|
||||||
xml.append("<h4>action: ");
|
|
||||||
xml.append(action);
|
|
||||||
xml.append("</h4>");
|
|
||||||
xml.append("<table width='30%' border='1' style='text-align: center;'>");
|
|
||||||
xml.append("<tr>");
|
|
||||||
xml.append("<th width='50%'>Key</th>");
|
|
||||||
xml.append("<th width='50%'>Value</th>");
|
|
||||||
xml.append("</tr>");
|
|
||||||
|
|
||||||
Enumeration<String> names = session.getAttributeNames();
|
/** method to get session details */
|
||||||
while (names.hasMoreElements()) {
|
private String getSessionDetails() {
|
||||||
String name = names.nextElement();
|
SessionDetailsManager manager = SessionManager.getSessionDetailsManagerInstance();
|
||||||
xml.append("<tr>");
|
|
||||||
xml.append("<td>");
|
|
||||||
xml.append(name);
|
|
||||||
xml.append("</td>");
|
|
||||||
xml.append("<td>");
|
|
||||||
xml.append(session.getAttribute(name));
|
|
||||||
xml.append("</td>");
|
|
||||||
xml.append("</tr>");
|
|
||||||
}
|
|
||||||
|
|
||||||
xml.append("</table>");
|
StringBuilder xml = new StringBuilder();
|
||||||
xml.append("</body>");
|
xml.append("<!DOCTYPE html>");
|
||||||
xml.append("</html>");
|
xml.append("<html>");
|
||||||
|
xml.append("<head>");
|
||||||
|
xml.append("<meta charset='UTF-8'>");
|
||||||
|
xml.append("<title>tomcat-cluster-redis-session-manager-test</title>");
|
||||||
|
xml.append("</head>");
|
||||||
|
xml.append("<body>");
|
||||||
|
xml.append("<h2>tomcat-cluster-redis-session-manager-test-results</h2>");
|
||||||
|
xml.append("<h4>count: ");
|
||||||
|
xml.append(manager.getActiveSessionCount());
|
||||||
|
xml.append("</h4>");
|
||||||
|
// xml.append("<table width='30%' border='1' style='text-align: center;'>");
|
||||||
|
// xml.append("<tr>");
|
||||||
|
// xml.append("<th>Session ID</th>");
|
||||||
|
// xml.append("</tr>");
|
||||||
|
//
|
||||||
|
// for (String sessionId : manager.getActiveSessionIds()) {
|
||||||
|
// xml.append("<tr>");
|
||||||
|
// xml.append("<td>");
|
||||||
|
// xml.append(sessionId);
|
||||||
|
// xml.append("</td>");
|
||||||
|
// xml.append("</tr>");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// xml.append("</table>");
|
||||||
|
xml.append("</body>");
|
||||||
|
xml.append("</html>");
|
||||||
|
|
||||||
return xml.toString();
|
return xml.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue