|
|
Deploy Web Application on Tomcat 3.3a
by Jeff Hunter, Sr. Database Administrator
server.xml file
It is not recommend adding the new context to the server.xml file. Instead, the ContextXmlReader will read all context definitions (you can customize the "base" filename).The default is $TOMCAT_HOME/conf/apps-[name].xmlSee:
$TOMCAT_HOME/conf/apps-examples.xml- and -$TOMCAT_HOME/conf/apps-admin.xml
- cd $TOMCAT_HOME/conf
- cp apps-examples.xml apps-WebDBA.xml
- Edit file apps-WebDBA.xml and add the following:
<?xml version="1.0" encoding="ISO-8859-1"?> <webapps> <!-- You probably want to set "reloadable" to "true" during development, but you should set it to be "false" in production. --> <!-- Setting special properties for /WebDBA ( as an example of overriding the defaults ) --> <Context path="/WebDBA" docBase="webapps/WebDBA" debug="0" reloadable="true"> <SimpleRealm filename="conf/users/WebDBA-users.xml" /> <LogSetter name="WebDBA_tc.log" path="logs/WebDBA.log" /> <LogSetter name="WebDBA_servlet_log" path="logs/servlet_WebDBA.log" servletLogger="true"/> </Context> </webapps>
User Realm File
cd $TOMCAT/conf/users cp example-users.xml WebDBA-users.xmlDO NOT edit the file WebDBA-users.xml
Create Web Application Directories
cd $TOMCAT_HOME/webapps mkdir WebDBA mkdir WebDBA/html mkdir WebDBA/images mkdir WebDBA/lib mkdir WebDBA/WEB-INF mkdir WebDBA/WEB-INF/classes mkdir WebDBA/WEB-INF/lib
Create the Deployment Descriptor 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> |
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
Update the Apache Jserv Configuration File
############################################################################### # Apache JServ Configuration File # ############################################################################### # Note: this file should be appended or included into your httpd.conf # Tell Apache on win32 to load the Apache JServ communication module # LoadModule jserv_module modules/ApacheModuleJServ.dll # Tell Apache on Unix to load the Apache JServ communication module # For shared object builds only!!! # @LOAD_OR_NOT@LoadModule jserv_module @LIBEXECDIR@/mod_jserv.so LoadModule jserv_module libexec/mod_jserv.so <IfModule mod_jserv.c> # Do not edit! # OLD VALUE: ApJServManual on ApJServManual on # OLD VALUE: ApJServProperties <NOT DEFINED> ApJServProperties /u02/app/tomcat/conf/jserv/tomcat_dbaprod.properties ApJServDefaultProtocol ajpv12 ApJServSecretKey DISABLED ApJServMountCopy on ApJServLogLevel notice ### Change if you run tomcat on a different host #ApJServDefaultHost localhost ApJServDefaultPort 8027 #################### All jsp files will go to tomcat #################### ApJServMount default /root AddType text/jsp .jsp AddHandler jserv-servlet .jsp ############################## Context mapping - all requests go to tomcat ApJServMount /examples /root ApJServMount /WebDBA /root ############################## Context mapping - you need to "deploy" # ( copy or ln -s ) the context into htdocs ## # ApJservMount /CONTEXT/servlet /root # <Location /CONTEXT/WEB-INF/ > # AllowOverride None # deny from all # </Location> </IfModule> |
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/Hi
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 --------------------