|
|
Deploy Web Application on Tomcat 4.1.12
by Jeff Hunter, Sr. Database Administrator
Create Web Application Directories
cd $CATALINA_HOME/webapps mkdir webdba mkdir webdba/images mkdir webdba/WEB-INF mkdir webdba/WEB-INF/classes mkdir webdba/WEB-INF/lib
Add Minimal Deployment Descriptor File
Create a default deployment descriptor (web.xml) file that contains only the DTD, describing the web.xml file, and an emptyelement. You can simply copy the deployment descriptor file from the /examples application and take out the webapp details.
$CATALINA_HOME/webapps/webdba/WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <!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> </web-app>Place the above web.xml file in the directory:
$CATALINA_HOME/webapps/webdba/WEB-INF
- cp $CATALINA_HOME/webapps/examples/WEB-INF/web.xml $CATALINA_HOME/webapps/WebDBA/WEB-INF/web.xml
- Make the following changes to the new web.xml file or simply replace it with this file:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <display-name>WebDBA</display-name> <description>Web Monitoring Tool - Netscape and JNDI</description> <!-- +===========================================+ | CONTEXT SECTION | +===========================================+ --> <context-param> <param-name>Jeff Hunter</param-name> <param-value>jhunter@iDevelopment.info</param-value> <description> The EMAIL address of the administrator to whom questions and comments about this application should be addressed. </description> </context-param> <!-- +===========================================+ | SERVLET: HelloServlet | +===========================================+ --> <servlet> <servlet-name> Hi </servlet-name> <servlet-class> HelloServlet </servlet-class> <init-param> <param-name>Application Name</param-name> <param-value>Hello Application</param-value> </init-param> <description> Testing Configuration and Parameter Passing </description> <load-on-startup/> </servlet> <servlet-mapping> <servlet-name> Hi </servlet-name> <url-pattern> /Hi </url-pattern> </servlet-mapping> <!-- +===========================================+ | LOGIN SECTION | +===========================================+ --> <!-- Default login configuration uses BASIC authentication --> <!-- <login-config> <auth-method>BASIC</auth-method> <realm-name>Example Basic Authentication Area</realm-name> </login-config> --> <!-- Form-based login is enabled by default. If you wish to try Basic authentication, comment out the <login-config> section below and uncomment the one above. --> <login-config> <auth-method>FORM</auth-method> <realm-name>Example Form-Based Authentication Area</realm-name> <form-login-config> <form-login-page>/jsp/security/login/login.jsp</form-login-page> <form-error-page>/jsp/security/login/error.jsp</form-error-page> </form-login-config> </login-config> </web-app>
- In the above example, we took the servlet named HelloServlet and registered it as Hi. In Tomcat speak; the servlet's registered name is Hi.
- Also notice, that we made a Servlet Mapping for the registered servlet Hi. Now the servlet can be accessed using the following URLs:
http://<machine_name>/WebDBA/Hi- OR -http://<machine_name>/WebDBA/servlet/HiNOTE: Parameter passing will ONLY work using the registered name. In the example above, both URLs will work since they use the Hi registered name. The following URL though will not work:http://<machine_name>/WebDBA/servlet/HelloServlet
Enabling the Invoker Servlet
The invoker servlet lets you run servlets without first making changes to the WEB-INF/web.xml file in your Web Application. Instead, you just drop your servlet into WEB-INF/classes and use the URL http://localhost/servlet/ServletName. The invoker servlet is extremely convenient when learning and even during initial development. But, as discussed at length in many books, you do not want it on at deployment time.Up until Apache Tomcat 4.1.12, the invoker was enabled by default. However, a security flaw was recently uncovered whereby the invoker servlet could be used to see the source code of servlets that were generated from JSP pages. Although this may not matter in most cases, it might reveal proprietary code to outsiders, so, as of Tomcat 4.1.12, the invoker was disabled by default in the global deployment descriptor: $CATALINA_HOME/conf/web.xml. I suspect that the Jakarta project will fix the problem soon and re-enable the invoker servlet in upcoming Tomcat releases. In the meantime, however, you almost certainly want to enable it when learning or while developing your application. Just be sure that you do so only on a desktop development machine that is not accessible to the outside world.
To enable the invoker servlet, uncomment the following servlet-mapping element:
<servlet-mapping> <servlet-name>invoker</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping>Uncomment the above element in $CATALINA_HOME/conf/web.xml to enable this feature for all Web Applications or in $CATALINA_HOME/webapps/webdba/web.xml for only a particular Web Applications.
Create a Web Application ServletContext
After you have created the Web Application directory structure, (and decided if you are going to turn on the Invoker Servlet feature), you must add a new ServletContext. The ServletContext defines a set of methods and components that a Web Application uses to communicate with the servlet container. The ServletContext acts as a container for the Web Application, and there is only one ServletContext per Web Application.To build a new ServletContext to Tomcat, you need to add the following entry to the $CATALINA_HOME/conf/server.xml file, setting the values for the path and docBase equal to the name of your new Web Application. This entry should be added inside the
element, with the name localhost (or the name you used as the ServerName in Apache's httpd.conf file).
... <Context path="/webdba" docBase="webdba" debug="0" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="webdba_log." suffix=".txt" timestamp="true"/> </Context> ...In the above definition, note that path="/webdba" tells the servlet container that all requests with /webdba appended to the server's URL belong to the webdba Web Application. The second element, docBase="webdba", tells the servlet container that the Web Application exists in the Web Application directory webdba.
Check the Apache JK configuration File (only if connecting with Apache)
$CATALINA_HOME/bin/shutdown.sh $CATALINA_HOME/bin/startup.sh
cd $CATALINA_HOME/conf/auto
######## Auto generated on Wed Nov 27 16:29:17 EST 2002##########
<IfModule !mod_jk.c>
LoadModule jk_module /u02/app/apache/modules/mod_jk.so
</IfModule>
JkWorkersFile "/u02/app/tomcat/conf/jk/workers.properties"
JkLogFile "/u02/app/tomcat/logs/mod_jk.log"
JkLogLevel info
<VirtualHost cartman.ads.com>
ServerName cartman.ads.com
JkMount /admin ajp13
JkMount /admin/* ajp13
JkMount /webdav ajp13
JkMount /webdav/* ajp13
JkMount /metaview ajp13
JkMount /metaview/* ajp13
JkMount /examples/*.jsp ajp13
JkMount /examples/servlet/* ajp13
JkMount /webdba/*.jsp ajp13
JkMount /webdba/servlet/* ajp13
JkMount /tomcat-docs ajp13
JkMount /tomcat-docs/* ajp13
JkMount /manager ajp13
JkMount /manager/* ajp13
</VirtualHost> |
Testing the New Web Application Setup
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* My test servlet
*
* @author Jeff Hunter
*/
public class HelloServlet extends HttpServlet {
String ApplicationName = "Null Value";
public void init() throws ServletException {
ApplicationName = getInitParameter("Application Name");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>" + ApplicationName + "</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<h1> Hi </h1><P>");
out.println("This is a sample message from the Application: <P>");
out.println("<CENTER>");
out.println("<B><U>" + ApplicationName + "</U></B>");
out.println("</CENTER>");
out.println("</body>");
out.println("</html>");
}
} |
javac HelloServlet.java
http://<machine_name>/WebDBA/HiIf you used the Invoker servlet (not a good idea in a production scenario), you can access the testing servlet without needing to update the deployment descriptor (web.xml):
http://<machine_name>/WebDBA/servlet/HelloServlet
Passing Parameters to Servlets
If you are passing parameters to your servlet through the web.xml file and the getInitParameter() method, you will need to keep the following in mind:
- Try to use the init() method to capture your parameters while using the getInitParameter() method.
- The getInitParameter() method takes the name of the parameter as a String and returns the value as a String.
- If you need to return the parameter value as an Integer, use the following:
int count; public void init() throws ServletException { String initial = getInitParameter("Parameter Name"); try { count = Integer.parseInt(initial); } catch (NumberFormatException e) { count = 0; } }
- After making changes to the web.xml file, you will need to restart Tomcat and Apache.
- IMPORTANT: You will need to refer to the servlet using the registered name. If not, the parameter will not be passed and the getInitParameter() method will return null.
Understanding URLs with Web Applications
In the previous example, we created an example servlet called "HelloServlet", registered it as "Hi" and mapped it to the root of the web application. In this case, we can access the "Hi" application using either of the URLs:http://<machine_name>/WebDBA/Hi- OR -http://<machine_name>/WebDBA/servlet/HiNOTE: Parameter passing will ONLY work using the registered name. In the example above, both URLs will work since they use the Hi registered name. The following URL though will not work:http://<machine_name>/WebDBA/servlet/HelloServlet
Setting the CLASSPATH Variable for webapps (JAR Files)
The CLASSPATH variable set in your environment variable is being used only for compiling and running non-servlet applications. The Tomcat server uses a separate runtime environment that DOES NOT inherent your CLASSPATH variable setting.Classes that are used in one particular web application should be placed in that web application's WEB-INF/classes or in a jar in the WEB-INF/lib as defined by the Servlet 2.2 specification. If you want to give the classes wider scope by putting them in the Common Classloader or Apps Classloader, or want them to be part of the Server Classloader, use one of the following two methods to have those classes included in that classloader:
- If the classes are in a jar file, place the jar file in the directory that corresponds to the chosen classloader. If the classes exist as class files, create a classes under the corresponding directory if it doesn't already exist. Then add the class files into the appropriate package directory under the classes directory.
- If the chosen classloader is the Common Classloader or Apps classloader you include a directory or jar these classloaders by listing them a System property. For the Common Classloader, include the directory or jar file in a System property named org.apache.tomcat.common.classpath. For the Apps classloader, use a System property named org.apache.tomcat.apps.classpath.
You also have the option of setting the CLASSPATH variable for the Tomcat runtime environment in the $TOMCAT_HOME/bin/tomcat.sh script. There is a section called:
## -------------------- Prepare CLASSPATH --------------------