Information Servlet
// Import standard networking I/O packages
import java.net.*;
import java.io.*;
// Enumeration from util
import java.util.Enumeration;
// Import servlet packages
import javax.servlet.*;
import javax.servlet.http.*;
//
//
// InfoServlet
//
//
public class InfoServlet extends HttpServlet
{
// Get method of servlet
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException
{
// Define content type
response.setContentType("text/html");
// Get information about client and server
String clientBrowser = request.getHeader("User-Agent");
String clientReferer = request.getHeader("Referer");
String clientIP = request.getRemoteAddr();
String serverOS = System.getProperty("os.name");
String serverOSVersion = System.getProperty("os.version");
String serverOSArch = System.getProperty("os.arch");
// Get a servlet output stream for the response
ServletOutputStream sout = response.getOutputStream();
// Print header information
sout.println ("
InfoServlet Response");
// Print body information
sout.println ("");
sout.println ("InfoServlet
");
// Print information about client
sout.println ("Client :-
");
// Check for presence of user-agent header field
if (clientBrowser != null)
sout.println ("User-Agent : " + clientBrowser + "
");
// Print IP address
sout.println ("IP Address : " + clientIP + "
");
// Check for presence of referer header field
if (clientReferer != null)
sout.println ("Last page : " + clientReferer + "
");
// Print information about server
sout.println ("Server
");
// Check to see if each property is valid, and if so, output it
if ( (serverOS != null) & (serverOSVersion != null) )
sout.println ("Server O/S : " + serverOS + " v" + serverOSVersion + "
");
if ( serverOSArch != null)
sout.println ("Server CPU : " + serverOSArch + "
");
// Obtain a reference to the server context
ServletContext context = getServletContext();
if (context != null)
{
// Display information about servlets
sout.println (" Servlet information
");
// Table for servlet info
sout.println ("");
sout.println ("Servlet Name |
");
for (Enumeration e = context.getServletNames(); e.hasMoreElements();)
{
// Get name of servlet
String name = (String) e.nextElement();
sout.println (" ");
sout.println (name);
sout.println (" |
");
}
sout.println ("
");
}
else
sout.println ("Could not determine servlet context");
// Write footer information
sout.println ("");
// Flush
sout.flush();
}
// Post method of servlet
public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException
{
// Perform same action as get method
doGet(request, response);
}
public String getServletInfo()
{
return new String (
"InfoServlet - reports information on client connection and server state"
);
}
}