package int_2.servlets.assignments3; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.io.*; import int_2.servlets.*; import int_2.tools.*; /** Please respect the copyright of this code. It is not allowed to copy this code and use at any circumstances. @author © Fredrik Andersson
This servlet uses code from my home made toolbox that you can find in the folder 8/tools and 8/beans.The toolbox is not ready yet, I have been working for it for a couple of years when I have time. The goal is to get a toolbox of classes so it will be easier to map tasks to use-case-diagram. This servlet prints out all the environment variables from assignment 1b with HTML instead of txt. */ public class EnvironmentVariables_3b extends GeneralServlet { HashMap singelVariables; HashMap multiVariables; public void start(HttpServletRequest request, HttpServletResponse response) throws IOException { singelVariables = new HashMap(); multiVariables = new HashMap(); collectEnviromentVariables(request); String output = createPage("applications/int_2/int_2/templates/code_separation_3b.html", "code_separation_3b"); //String output = output = FileManager.getStringFor("applications/int_2/int_2/templates/code_separation_3b.html"); output = replaceInOutput(output, request); printString(output); flush(); singelVariables.clear(); multiVariables.clear(); } public void collectEnviromentVariables(HttpServletRequest request) { ServletContext sc = getServletContext(); multiVariables.put("SERVLET INFORMATION:", "SINGEL,Servlet name: "); multiVariables.put("SERVLET INIT PARAMETERS:", getInitParameterNames()); multiVariables.put("CONTEXT INIT PARAMETERS:", sc.getInitParameterNames()); multiVariables.put("SERVER INFORMATION:", "SINGEL,Server name: ,Server port: ,Server info: "); multiVariables.put("SERVER ATTRIBUTES:", sc.getAttributeNames()); multiVariables.put("RUNTIME VERSIONS:", "SINGEL,Major webserver version: ,Minor webserver version: ,JDK version: "); multiVariables.put("CLIENT INFORMATION:", "SINGEL,Remote address: ,Remote host: ,Remote user: ,Authentication type: "); multiVariables.put("HEADERS:", request.getHeaderNames()); multiVariables.put("REQUEST PARAMETERS:", "SINGEL,Query string: ,Character encoding: ,Content length:,Content type: "); multiVariables.put("REQUEST PARAMETERS, ONE BY ONE:", request.getParameterNames()); multiVariables.put("COOKIES, ONE BY ONE:", request.getCookies()); multiVariables.put("PATH AND CONNECTION INFORMATION:", "SINGEL,Path info: ,Translated path: , Request URI: ,Request URL: ,Request scheme: ,Context path: ,Servlet path: ,Request protocol: ,Request method: "); multiVariables.put("REQUEST ATTRIBUTES:", request.getAttributeNames()); multiVariables.put("SESSION INFORMATION:","SINGEL,Requested session ID: ,Session creation time: ,Session ID: ,Session last accessed: ,Session max inactive interval: "); multiVariables.put("SESSION VALUES:", session.getAttributeNames()); singelVariables.put("Servlet name: ", getServletName()); singelVariables.put("Server name: ", request.getServerName()); singelVariables.put("Server port: ", ""+request.getServerPort()); singelVariables.put("Server info: ", sc.getServerInfo()); singelVariables.put("Major webserver version: ", ""+sc.getMajorVersion()); singelVariables.put("Minor webserver version: ", ""+sc.getMinorVersion()); singelVariables.put("JDK version: ", System.getProperty("java.version")); singelVariables.put("Remote address: ", request.getRemoteAddr()); singelVariables.put("Remote host: ", request.getRemoteHost()); singelVariables.put("Remote user: ", request.getRemoteUser()); singelVariables.put("Authentication type: ", request.getAuthType()); singelVariables.put("Query string: ", request.getQueryString()); singelVariables.put("Character encoding: ", request.getCharacterEncoding()); singelVariables.put("Content type: ", request.getContentType()); singelVariables.put("Path info: ", request.getPathInfo()); singelVariables.put("Translated path: ", request.getPathTranslated()); singelVariables.put("Request URI: ", request.getRequestURI()); singelVariables.put("Request URL: ", request.getRequestURL().toString()); singelVariables.put("Request scheme: ", request.getScheme()); singelVariables.put("Context path: ", request.getContextPath()); singelVariables.put("Servlet path: ", request.getServletPath()); singelVariables.put("Request protocol: ", request.getProtocol()); singelVariables.put("Request method: ", request.getMethod()); singelVariables.put("Requested session ID: ", request.getRequestedSessionId()); HttpSession session = request.getSession(true); singelVariables.put("Session creation time: ", ""+session.getCreationTime()); singelVariables.put("Session ID: ", session.getId()); singelVariables.put("Session last accessed: ", ""+session.getLastAccessedTime()); singelVariables.put("Session max inactive interval: ", ""+session.getMaxInactiveInterval()); } public String replaceInOutput(String output, HttpServletRequest request) { Iterator multiKeyIterator = multiVariables.keySet().iterator(); while(multiKeyIterator.hasNext()) { String key = (String)multiKeyIterator.next(); Object object = multiVariables.get(key); if(object instanceof String) { StringBuffer stringBuffer = new StringBuffer(""); String value = (String)object; if(value.startsWith("SINGEL")) { StringTokenizer stringTokenizer = new StringTokenizer(value, ","); int count = 0; while(stringTokenizer.hasMoreTokens()) { if(count > 0) { String token = stringTokenizer.nextToken(); value = (String)singelVariables.get(token); stringBuffer.append("
  • "); stringBuffer.append(token); stringBuffer.append(value); stringBuffer.append("
  • "); } else { stringTokenizer.nextToken(); } count++; } } else { value = (String)multiVariables.get(key); stringBuffer.append("
  • "); stringBuffer.append(key); stringBuffer.append(value); stringBuffer.append("
  • "); } output = TextManager.replaceInString(output, "
  • "+key+"
  • ", stringBuffer.toString()); } else if(object instanceof Cookie[]) { Cookie[] cookies = (Cookie[])object; StringBuffer stringBuffer = new StringBuffer(""); if(cookies != null) { for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; stringBuffer.append("
  • "); stringBuffer.append(cookie.getName()); stringBuffer.append(": "); stringBuffer.append(cookie.getValue()); stringBuffer.append("
  • "); } } output = TextManager.replaceInString(output, "
  • "+key+"
  • ", stringBuffer.toString()); } else if(object instanceof Enumeration) { ServletContext sc = getServletContext(); HttpSession session = request.getSession(true); Enumeration e = (Enumeration)object; StringBuffer stringBuffer = new StringBuffer(""); if(key.equals("SERVLET INIT PARAMETERS:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); String value = getInitParameter(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value); stringBuffer.append("
  • "); } } else if(key.equals("CONTEXT INIT PARAMETERS:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); String value = sc.getInitParameter(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value); stringBuffer.append("
  • "); } } else if(key.equals("SERVER ATTRIBUTES:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); Object value = sc.getAttribute(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value.toString()); stringBuffer.append("
  • "); } } else if(key.equals("HEADERS:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); String value = request.getHeader(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value); stringBuffer.append("
  • "); } } else if(key.equals("REQUEST PARAMETERS, ONE BY ONE:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); String[] values = request.getParameterValues(key2); for(int i = 0; i < values.length; i++) { stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(values[i]); stringBuffer.append("
  • "); } } } else if(key.equals("REQUEST ATTRIBUTES:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); Object value = request.getAttribute(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value.toString()); stringBuffer.append("
  • "); } } else if(key.equals("SESSION VALUES:")) { while (e.hasMoreElements()) { String key2 = (String)e.nextElement(); Object value = session.getAttribute(key2); stringBuffer.append("
  • "); stringBuffer.append(key2); stringBuffer.append(": "); stringBuffer.append(value.toString()); stringBuffer.append("
  • "); } } output = TextManager.replaceInString(output, "
  • "+key+"
  • ", stringBuffer.toString()); } } return output; } }