Java Tutorial - Java Script : Testing Tomcat

Java Tutorial - Java Script :

Testing Tomcat

out.println(“<head>”);
out.println(“<title>SysProps Servlet</title>”);
out.println(“</head>”);
out.println(“<body>”);
out.println( today() );
out.println( showHeaders(request) );
out.println( showRequestInfo(request) );
out.println( showRequestParams(request) );
out.println( showSessionAttributes(request) );
out.println( showServletContextInfo() );
out.println( showContextAttributes() );
out.println( showInitParams() );
out.println( showSysProps() );
out.println(“</table>”);
out.println(“</body>”);
out.println(“</html>”);
out.close();
}
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
public String getServletInfo()
{
return “Prints Java System Properties”;
}
}
SuperSnoop shows information about the
·         HttpRequest that invoked it
·         Current session
·         ServletContext
·         Java environment
This code is not complex, but being able to see the information it presentscan be very useful when you are trying to solve difficult problems with servlet and container configurations. You can include a copy of SuperSnoop in my
classes directory for any new Web application that you’re developing. Use the following web.xml file for deployment:
<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
“http://java.sun.com/dtd/web-app_2_3.dtd”>
<web-app>
<servlet>
<servlet-name>Snoop</servlet-name>
<servlet-class>SuperSnoop</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Snoop</servlet-name>
<url-pattern>/servlet/Snoop</url-pattern>
</servlet-mapping>
</web-app>

The following command line can be used to compile the program on Windows: javac –classpath %CATALINA_HOME%\common\lib\servlet.jar SuperSnoop.java The command for compiling under Linux is:javac -classpath $CATALINA_HOME/common/lib/servlet.jar SuperSnoop.java CATALINA_HOME represents the base installation directory for Tomcat. This can normally be found set in the CATALINA_HOME environment variable. After the code has been compiled, follow these steps to install the code:
·         Create a directory below the Tomcat webapps directory, and name it snoopy.
·         Below the snoopy directory create another directory, and name it WEBINF.
Note that the name WEB-INF must be entered as uppercase even on Windows.
·         Copy the web.xml file into this WEB-INF directory.
·         Create a new directory named classes below the WEB-INF directory.
·         Copy the SuperSnoop.class file into the classes directory.
Restart Tomcat, and it should now recognize the new Web application. The application can be executed by entering the following URL into your browser: http://localhost:8080/snoopy/servlet/Snoop
A sample of the output screen is shown in Figure