/**Servlet generating it's environment variables and sends them to browsers making requests as plain text. * Internet Programming 2 - Course * @author Martin Carlsson */ import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class EnvironmentVariablesText extends HttpServlet { private final static String CONTENT_TYPE = "text/plain"; public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { doGet(req, res); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletContext sc = getServletContext(); Enumeration e = null; res.setContentType(CONTENT_TYPE); PrintWriter pw = res.getWriter(); print("*********SERVER-SIDE INFORMATION:**********\n", pw); print("Servlet Information:", pw); print("Servlet name", getServletName(), pw); print("Servlet Init Parameters:", pw); e = getInitParameterNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); String value = getInitParameter(key); print(key, value, pw); } print("Context Init Parameters:", pw); e = sc.getInitParameterNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); String value = sc.getInitParameter(key); print(key, value, pw); } print("Server Information:", pw); print("Server name", req.getServerName(), pw); print("Server port", Integer.toString(req.getServerPort()), pw); print("Server info", sc.getServerInfo(), pw); print("Server Attributes:", pw); e = sc.getAttributeNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); Object value = sc.getAttribute(key); print(key, value.toString(), pw); } print("Server and Java Versions:", pw); print("Major webserver version", Integer.toString(sc.getMajorVersion()), pw); print("Minor webserver version", Integer.toString(sc.getMinorVersion()), pw); print("JDK version", System.getProperty("java.version"), pw); print("*********CLIENT-SIDE INFORMATION:*************\n", pw); print("Client Information:", pw); print("Remote address", req.getRemoteAddr(), pw); print("Remote host", req.getRemoteHost(), pw); print("Remote user", req.getRemoteUser(), pw); print("Authentication type", req.getAuthType(), pw); print("Headers:", pw); e = req.getHeaderNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); String value = req.getHeader(key); print(key, value, pw); } print("Request Information:", pw); print("Query string", req.getQueryString(), pw); print("Character encoding", req.getCharacterEncoding(), pw); // Test print("Content length", Integer.toString(req.getContentLength()), pw); // Test print("Content type", req.getContentType(), pw); // Test print("Request Parameters:", pw); e = req.getParameterNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); String[] values = req.getParameterValues(key); for(int i = 0; i < values.length; i++) { print(key, values[i], pw); } } print("Cookies:", pw); Cookie[] cookies = req.getCookies(); if(cookies != null) { for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; print(cookie.getName(), cookie.getValue(), pw); } } print("Path and connection information:", pw); print("Path info", req.getPathInfo(), pw); print("Translated path", req.getPathTranslated(), pw); print("Request URI", req.getRequestURI(), pw); print("Request URL", req.getRequestURL().toString(), pw); print("Request scheme", req.getScheme(), pw); print("Context path", req.getContextPath(), pw); print("Servlet path", req.getServletPath(), pw); print("Request protocol", req.getProtocol(), pw); print("Request method", req.getMethod(), pw); print("Request Attributes:", pw); e = req.getAttributeNames(); while (e.hasMoreElements()) { String key = (String)e.nextElement(); Object value = req.getAttribute(key); print(key, value.toString(), pw); } print("Session Information:", pw); print("Requested session id", req.getRequestedSessionId(), pw); HttpSession session = req.getSession(true); print("Session is new", Boolean.toString(session.isNew()), pw); print("Session creation time", new Date(session.getCreationTime()).toString(), pw); print("Session id", session.getId(), pw); print("Session last accessed", new Date(session.getLastAccessedTime()).toString(), pw); print("Session max inactive interval", Integer.toString(session.getMaxInactiveInterval()), pw); print("Session Values:", pw); e = session.getAttributeNames(); if(!e.hasMoreElements()) print("key", null, pw); else while (e.hasMoreElements()) { String key = (String)e.nextElement(); Object value = session.getAttribute(key); print(key, value.toString(), pw); } } private void print(String string, PrintWriter pw) { pw.println(); pw.println(string); } private void print(String name, String value, PrintWriter pw) { if(value == null) value = "null"; pw.println(" " + name + ": " + value); } } //End class EnvironmentVariablesText