Wednesday, March 21, 2007

Manually Deploying A New Tomcat Web Application ...continue

As promised in my last post, i will describe the process of writing and deploying servlets on Tomcat.

Servlets are java classes which can handle HTTP request and send dynamic response back to the client. Unlike JSP pages, servlets have to be compiled by the developer, wherese JSP pages are first transformed to servlet source files by JSP engine and then compiled to servlet classes by the servlet engine. The advantage of servlet over JSP is they need no compilation time for first time as required by JSP pages.

When deploying a Servlet class we have to put the class file in pre-defined "classes" directory which has to be under the "WEB-INF" directory. We can also put the source file there.

Here is a simple servlet source file, save it as HelloServlet.java under "WEB-INF\classes" directory.

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* The simplest possible servlet.
*
* @author Put Your Name Here
*/

public class HelloServlet extends HttpServlet {


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>Hello World Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Hello World!, I am a servlet");
out.println("</body>");
out.println("</html>");
}
}

Before compiling the servlet we have to set our CLASSPATH correctly. To compile a servlet, the JAR file "servlet-api.jar" containing the required classes has to be in our classpath. It is generally found at "common\lib" directory under Tomcat installation directory. We can either set it in our environment variable or we can declare it in command-line.

Now, we will goto command prompt and compile the servlet using the following command.

D:\tomcat5\webapps\app1\WEB-INF\classes>javac -classpath %CLASSPATH%;D:\tomcat5\common\lib\servlet-api.jar HelloServlet.java

This will generate a class file "HelloServlet.class" in the same directory. But, the work is still not over. We have to inform Tomcat about this servlet through our applications configuration file which is "web.xml".

So, add the following lines in web.xml between the tags <web-app></web-app>.



<servlet>

  <servlet-name> HelloServlet </servlet-name>

  <servlet-class> HelloServlet </servlet-class>

</servlet>



<servlet-mapping>

  <servlet-name>HelloServlet</servlet-name>

  <url-pattern>/Hello</url-pattern>

</servlet-mapping>


The contains of <servlet></servlet> tags define a servlets name ands its corresponding class by using the <servlet-name> and <servlet-class> tag. The contains of <servlet-mapping></servlet-mapping> tags define the URL which is mapped to the servlet class. It is required because when a client request a servlet class, it cannot be directly send to the client. So when a client request an URL which point to a servlet Tomcat server executes the servlet for the client and send the output back to the client. The <servlet-name> is used for identifying the servlet and the <url-pattern> tag is used to inform Tomcat that when any request for this URL arrives, executes this servlet and send its output back to the client.

Now, we are ready to test the servlet. Start Tomcat server and point our browser to the URL http://localhost:8080/app1/Hello.

Tuesday, March 20, 2007

Manually Deploying A New Tomcat Web Application

Tomcat is the first choice for all students,like me, who have started learning JSP or servlet. But again like me, many students finds it hard to get started with it as there is no in-built tool for deploying a new application in Tomcat. So in this post i am sharing my little experience on starting a new Tomcat web-application.

First of all we have to be familier with the directory structure of tomcat. In my computer Tomcat 5.5 is installed at D:\tomcat5. Under it there are 9 directories.

a) bin - all executables and the main starting point of Tomcat server
b) conf - all configuration files
c) log - all Tomcat log files
d) temp - for temporary use by Tomcat
e) common - all common use compiled java classes
f) server - all classes required to run the Tomcat server
g) shared - all classes shared by Tomcat and any other web-applications
f) work - used by Tomcat as its working directory, all applications are served form this directory
g) webapps - default folder to store web applications

among the above said, we are mainly interested in the last directory i.e. webapps.

To start a new application create a new folder under the webapps directory. Lets say we want start a application named "app1". So create a directory "app1" under "webapps". Then put all the JSP and HTML files under it.

Now at least one more directory need to be created under "webapps" and it has to be named "WEB-INF". The "WEB-INF" directory should contain a XML file named web.xml which is used as the configuration file of our appliocation. Here is a minimum web.xml file.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>

</web-app>

Save this as web.xml under the "WEB-INF" folder. Now we will create a JSP test page.

Save the following lines as "index.jsp" under "app1" directory.


<html>
<head>
<title>Testing Tomcat</title>
</head>
<body>
<% out.println("Hello, World! I am a JSP Page"); %>
</body>
</html>


So, after all this our directory structure should be like this

D:\tomcat5
 - webapps
   - app1
    - index.jsp
    - WEB-INF
     - web.xml

Now start Tomcat and locate our browser to the address "http://localhost:8080/app1/"

Wrintng and deploying a servlet requires a little more work. So to keep things simple i will describe that in another post.