[an error occurred while processing this directive]

Working with Tomcat Server

                         

This section explains how to work with Tomcat Server

Apache Tomcat is a famous Servlet container developed at  Apache Software  Foundation. This software is released under under the Apache Software License. Anyone can use it for the development as well as deployment of the applications. Tomcat is the official reference of implementation of java  Servlets and java  Server Pages. Tomcat is very easy to install and configure.  Anyone can learn it very fast and start using the Tomcat server for the development and deployment of the web applications. 

These days many web hosting companies are providing Tomcat support on their server. So, if you develop the application in Java technology you can get any host and then deploy it on the internet. Earlier it was a difficult task to get a good host for hosting.

So, let's get started with the Tomcat server.

1) Installation and Configuration- Apache Tomcat  Server is free software available for download at www.apache.org and www.jakarta.apache.org.The current version of tomcat server is 6.0. This server supports Java Servlets  2.5  and Java server Pages  2.2. JDK is also free software available for download at www.java.sun.com. Its current version is 6.0. 

Learn Tomcat Installation in detail. You will also find the description on how to run example with tomcat.

Deploying Servlets On  Tomcat  Server

To  deploy servlets on  Tomcat  Server, following steps are to be taken for example given  below.

1. Create web application

To develop an application using  servlets or jsp, a directory structure  is  to be maintained for the example given  below.

Step1: Create a web application folder (servlet-examples) under tomcat webapps directory.  The  path will be C:\apache tomcat\webapps\servlets-examples.

Step2:  Create a WEB-INF folder which should be created under servlets-examples.

Step3:  Create  web.xml file and classes folder under the WEB_INF folder.

2.  Compile the servlet Program- Create a servlet program and compile it on the command Prompt .The procedure is not different from any java  program. The set of classes required for writing servlets is available in  servlet-api.jar which is put into CLASSPATH.

3.  Copy the Servlet class(Hello) into classes folder, which is under WEB-INF folder.

4.  Edit web.xml to include  servlet's name  and url pattern.

<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>

5.  Run Tomcat Server and execute  your servlet-   To run the server Go to C:\apache tomcat\bin\startup.bat and double  click on it , the server will start up. After assuring that  the server is running successfully, you can run your servlet. To  execute your servlet,  open  your web browser and  type the url which you have mentioned in your web.xml. The url will be like this:

http://localhost:8080/servlets-examples/Hello

Let's see an example Servlet "Hello". Here is the code of "Hello.java".

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello 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("<body>");
out.println("hello"); 
out.println("</body>");
out.println("</html>");
}
}

Output of program:

Download Source Code

                         

[an error occurred while processing this directive]