Getting Started with Servlets and JSP using Tomcat

What is a Servlet?

  • A servlet is a server-side program which executes inside a Web server, such as Apache.
  • It Receives HTTP requests from the client and provides HTTP responses

The Servlet API

Servlet API is used to create servlets.

Servlets use classes and interfaces from two packages: javax.servlet and javax.servlet.http. The javax.servlet package contains classes to support generic, protocol-independent servlets.

These classes are extended by the classes in the javax.servlet.http package to add HTTP-specific functionality.

The top-level package name is javax instead of the familiar java, to indicate that the Servlet API is a standard extension.

Every servlet must implement the javax.servlet.Servlet interface. Most servlets implement it by extending one of two special classes: javax.servlet.GenericServlet or javax.servlet.http.HttpServlet.

What is Tomcat?

Tomcat is a Servlet Container.

  • Manages servlet loading/unloading
  • Works with the Web server (e.g. Apache) to direct requests to servlets and responses back to clients

How to Create and Run Servlets

1. Download and install Java SE 6 (including JRE)

You can download Java from the link below

http://www.java.com/en/download/index.jsp

Install it in your system

2. Download Tomcat

You can download Tomcat from the link below. The latest version of Tomcat is Tomcat 7.0

http://tomcat.apache.org/

Install Tomcat.

3. Configure the server

4. Test set up

The Tomcat Home Page gets displayed if it’s successfully installed

TomcatHomePage

TomcatHomePage

Tomcat Directory/File Structure

TomcatDirectoryStructure

Tomcat Directory Structure

This is the Tomcat Directory structure once it is installed in your system.

Tomcat Directories/Files Description

Directory Description
bin The binary executables and scripts
conf  Configuration files
lib JAR files that contain classes that are available to all Web applications
logs Log files
webapps Web applications
work  Temporary files and directories for Tomcat

Deploy a Web Application in Tomcat

  • The application should be under the C:\Tomcat 7.0\webapps directory
  • A root directory for the application can be created under the C:\Tomcat 7.0\webapps directory (e.g. C:\Tomcat 7.0\webapps\ContactApp)
  • The root directory contains sub-directories, the index file, HTML and JSP files for the application.
  • All directories and files relating to our applications should be under this directory
  • Applications that use servlets must have the WEB-INF and WEB-INF\classes directories inside their root directories
  • Put the Deployment descriptor for the application inside the WEB-INF directory.

Deployment Descriptor

  • Deployment descriptor is an XML file with the name web.xml
  • It should be in the WEB-INF directory

A Sample Deployment Descriptor

<web-app>
	<servlet>
		<!--Name for referring to it elsewhere in the DD -->
		<servlet-name>HelloServlet</servlet-name>
		<!-- Full name, including any package details-->
		<servlet-class>servlets.HelloWorldServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<!--Name of the servlet again -->
		<servlet-name>HelloServlet</servlet-name>
		<!--Client’s URL will have this -->
		<url-pattern>/HelloServlet</url-pattern>
	</servlet-mapping>

</web-app>

Tomcat: Important Directories for an application in webapps directory

Directory Description
doument root Contains sub-directories, the index file,  HTML and JSP files for the application.
\WEB-INF Contains a file named web.xml. It can be used to configure servlets and other components that make up the application.
\WEB-INF\classes Contains servlets and other Java classes that are not compressed into a JAR file. If Java packages are used, each package must be stored in a subdirectory that has the same name as the package.
\WEB-INF\lib Contains any JAR files that contain Java classes that are needed by this application, but not by other Web

Writing your First Servlet

Open any Text Editor and Type the Program below.

HelloWorldServlet.java


package servlets;

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

public class HelloWorldServlet extends HttpServlet
{
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
	{
		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		out.println("<html>");
		out.println("<head><title>Hello World Servlet</title></head>");
		out.println("<body>");
		out.println("<h1>Hello World!!!</h1>");
		out.println("<p>Welcome to the world of Servlets and JSP!!!</p>");
		out.println("</body></html>");
	}
}

Compile it like any other Java Program using javac command. The class file should get generated successfully.

Deploying and Running your Servlet

  • Create a root directory for the application under the C:\Tomcat 7.0\webapps directory (e.g. C:\Tomcat 7.0\webapps\Servlet1)
  • Create a WEB-INF and WEB-INF\classes directories inside the Servlet1 Directory.
  • Make a directory called servlets inside WEB-INF\classes directory as our HelloWorld Servlet is inside this package.
  • Put the class file of the servlet inside WEB-INF\classes\servlets directory.
  • Write the Deployment Descriptor web.xml and put it under the WEB-INF directory.

Deployment Descriptor for Hello World Servlet


<web-app>

	<servlet>
		<servlet-name>HelloServlet</servlet-name>
		<servlet-class>servlets.HelloWorldServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HelloServlet</servlet-name>
		<url-pattern>/HelloServlet</url-pattern>
	</servlet-mapping>

</web-app>

Running Hello World Servlet

  1. Start tomcat
  2. Open browser and type http://localhost:8080. The Home Page of Tomcat should open.
  3. Type http://localhost:8080/Servlet1/HelloServlet in the address bar of your browser.
  4. The Servlet should run and display the famous greetings in programming

Snapshot of the o/p

Hello World Servlet

Servlets are extremely powerful and can do a lots of work very easily as compared to CGI. They are good in performance and secure. :D

Java Servlet – Key Points

Java Servlet

  • A servlet is a server-side program which executes inside a Web server, such as Apache.
  • It Receives HTTP requests from the client and provides HTTP responses
  • The javax.servleand javax.servlet.http packages provide interfaces and classes for writing servlets.
  • All servlets must implement the Servlet interface, which defines life-cycle methods.
  • When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. TheHttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.