package int_2.servlets.assignments1;
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.
I do not use Java 1.5 since I do not have been able to download it cause I use a 56kb modem
for Internet access.
So I have done that old way as you can see.
*/
public class EnvironmentVariables_1b extends GeneralServlet
{
public void start(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
out = new PrintWriter(response.getOutputStream());
StringBuffer output = new StringBuffer(getServerSideInfo(request));
output.append("\n\n");
output.append( getClientSideInfo(request) );
printString(output.toString());
flush();
}
public String getServerSideInfo(HttpServletRequest request)
{
ServletContext sc = getServletContext();
StringBuffer info = new StringBuffer("SERVLET INFORMATION:\n");
info.append("\tServlet name :");
info.append(getServletName());
info.append("\n\n");
info.append("SERVLET INIT PARAMETERS:\n");
Enumeration e = getInitParameterNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = getInitParameter(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value);
info.append("\n");
}
info.append("\n\n");
info.append("CONTEXT INIT PARAMETERS:\n");
e = sc.getInitParameterNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = sc.getInitParameter(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value);
info.append("\n");
}
info.append("\n\n");
info.append("SERVER INFORMATION:\n");
info.append("\t");
info.append("Server name: ");
info.append(request.getServerName());
info.append("\n");
info.append("\t");
info.append("Server port: ");
info.append(request.getServerPort());
info.append("\n");
info.append("\t");
info.append("Server info: ");
info.append(sc.getServerInfo());
info.append("\n\n\n");
info.append("SERVER ATTRIBUTES:\n");
e = sc.getAttributeNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
Object value = sc.getAttribute(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value.toString());
info.append("\n");
}
info.append("\n\n");
info.append("RUNTIME VERSIONS:\n");
info.append("\t");
info.append("Major webserver version: ");
info.append(sc.getMajorVersion());
info.append("\n");
info.append("\t");
info.append("Minor webserver version: ");
info.append(sc.getMinorVersion());
info.append("\n");
info.append("\t");
info.append("JDK version: ");
info.append(System.getProperty("java.version"));
info.append("\n\n\n");
return info.toString();
}
public String getClientSideInfo(HttpServletRequest request)
{
StringBuffer info = new StringBuffer("CLIENT-SIDE INFORMATION:\n");
info.append("CLIENT INFORMATION:\n");
info.append("\t");
info.append("Remote address: ");
info.append(request.getRemoteAddr());
info.append("\n");
info.append("\t");
info.append("Remote host: ");
info.append(request.getRemoteHost());
info.append("\n");
info.append("\t");
info.append("Remote user: ");
info.append(request.getRemoteUser());
info.append("\n");
info.append("\t");
info.append("Authentication type: ");
info.append(request.getAuthType());
info.append("\n\n");
info.append("HEADERS:\n");
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = request.getHeader(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value);
info.append("\n");
}
info.append("\n\n");
info.append("REQUEST PARAMETERS:\n");
info.append("\t");
info.append("Query string: ");
info.append(request.getQueryString());
info.append("\n");
info.append("\t");
info.append("Character encoding: ");
info.append(request.getCharacterEncoding()); // Test
info.append("\n");
info.append("\t");
info.append("Content length: ");
info.append(request.getContentLength()); // Test
info.append("\n");
info.append("\t");
info.append("Content type: ");
info.append(request.getContentType()); // Test
info.append("\n\n");
info.append("REQUEST PARAMETERS, ONE BY ONE:\n");
e = request.getParameterNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String[] values = request.getParameterValues(key);
for(int i = 0; i < values.length; i++)
{
info.append("\t");
info.append(key);
info.append(": ");
info.append(values[i]);
info.append("\n");
}
}
info.append("\n\n");
info.append("COOKIES, ONE BY ONE:\n");
Cookie[] cookies = request.getCookies();
if(cookies != null)
{
for (int i = 0; i < cookies.length; i++)
{
Cookie cookie = cookies[i];
info.append("\t");
info.append(cookie.getName());
info.append(": ");
info.append(cookie.getValue());
info.append("\n");
}
}
info.append("\n\n");
info.append("PATH AND CONNECTION INFORMATION:\n");
info.append("\t");
info.append("Path info: ");
info.append(request.getPathInfo());
info.append("\n");
info.append("\t");
info.append("Translated path: ");
info.append(request.getPathTranslated());
info.append("\n");
info.append("\t");
info.append("Request URI: ");
info.append(request.getRequestURI());
info.append("\n");
info.append("\t");
info.append("Request URL: ");
info.append(request.getRequestURL().toString());
info.append("\n");
info.append("\t");
info.append("Request scheme: ");
info.append(request.getScheme());
info.append("\n");
info.append("\t");
info.append("Context path: ");
info.append(request.getContextPath());
info.append("\n");
info.append("\t");
info.append("Servlet path: ");
info.append(request.getServletPath());
info.append("\n");
info.append("\t");
info.append("Request protocol: ");
info.append(request.getProtocol());
info.append("\n");
info.append("\t");
info.append("Request method: ");
info.append(request.getMethod());
info.append("\n\n");
info.append("REQUEST ATTRIBUTES:\n");
e = request.getAttributeNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
Object value = request.getAttribute(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value.toString());
info.append("\n");
}
info.append("\n\n");
info.append("SESSION INFORMATION:\n");
info.append("\t");
info.append("Requested session ID: ");
info.append(request.getRequestedSessionId());
info.append("\n");
info.append("\t");
HttpSession session = request.getSession(true);
info.append("Session creation time: ");
info.append(session.getCreationTime());
info.append("\n");
info.append("\t");
info.append("Session ID: ");
info.append(session.getId());
info.append("\n");
info.append("\t");
info.append("Session last accessed: ");
info.append(session.getLastAccessedTime());
info.append("\n");
info.append("\t");
info.append("Session max inactive interval: ");
info.append(session.getMaxInactiveInterval());
info.append("\n\n");
info.append("SESSION VALUES:\n");
e = session.getAttributeNames();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
Object value = request.getAttribute(key);
info.append("\t");
info.append(key);
info.append(": ");
info.append(value.toString());
info.append("\n");
}
return info.toString();
}
}