Tomcat Installation and Create the Servlet using Tomcat server

What are tomcat server?
  • Apache Tomcat version 6.0 implements the Servlet 2.5 and JavaServer Pages 2.1 specifications from the Java Community Process, and includes many additional features that make it a useful platform for developing and deploying web applications and web services.
Tomcat server Installation step by step:
  • First, download tomcat6.0 server and run the exe file.
By default the tomcat server installed into C:\Program Files\Apache SoftwareFoundation\tomcat6.0 . But we can customise it path desired location. In this installation, it is installed into D: drive.
During tomcat installation, java jar file is required. Therefore java installation is must necessary before tomcat installation.
Create Servlet Directory Structure
Directory structure defines the way to put the different types of files within web container. tomcat
Create Servlet Application
There are three ways to create the servlet.
  1. By implementing the Servlet interface
  2. By extends the GenericServlet class
  3. By extends the HttpServlet class
Servlet Example
import; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("Welcome Java Servlet Example"); } }
Compile the Servlet file
1.) Open cmd prompt and change directory according the java source file location. In this example the servlet file is saved into D:\Tomcat 6.0\webapps\ServletExample\classes .
2.) compile the servlet file : javac

compile time exception is occured because servlet API is not a part of java. So set the classpath is required for compilation the servlet file.

Set the classpath
Set the classpath is same as the process of set the path.

1.) Click on Advance System setting

2.) Click on Advanced Tab

3.) Click on New Tab


4.) Copy complite address (D:\Tomcat 6.0\lib\servlet-api.jar)


5.) Variable Name : classpath

Variable Value : D:\Tomcat 6.0\lib\servlet-api.jar;


6. ) Close the cmd prompt.

7.) Again open cmd prompt

8.) Change Directory and compile source code

Create Deployment Descriptor
The deployment descriptor (web.xml) is an xml file. This file includs the configuration information for our web application. The first line in the deployment descriptor is an XML declaraton specifying the version of the XML and the encoding used:
<?xml version="1.0" encoding="ISO-8859-1"?>

The actual definition of servlets is enclosed within <web-app> and </web-app>tags.

<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app>
Start the Tomcat server
Step to Start Tomcat server
  • First go to Tomcat6.0
  • And then click on bin folder
  • In last click on tomcat6 or double click on startup.bat file.
Run the Web Application
First open any browser and type http://localhost:8080/ServletExample/FirstServlet and press enter.
Download this example
1) Deployed using Tomcat 6.0
Change the port number
We can change the default port number (8080). Open Server.xml file and edit the prot number.
Now edited servet.xml file is :
Turn on Servlet Reloading
If, the class files of our servlet programs get modified it will be seen by Tomcat. And after that some time the server will be shut down and restart and thus running the web appliction will be tedious. So we have to set it by doing the following:
<Context reloadable="true">, in C:\Tomcat6.0conf\context.xml


Latest Tutorial

Most Visited Tutorial