Servlet Tutorial
Servlet technology is used to create web application (resides at server side and generates dynamic web page).
Servlet technology is robust and scalable because of java language. Before Servlet, CGI (Common Gateway Interface) scripting language was popular as a server-side programming language. But there was many disadvantages of this technology. We have discussed these disadvantages below.
There are many interfaces and classes in the servlet API such as Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse etc.
What is a Servlet?
Servlet can be described in many ways, depending on the context.
- Servlet is a technology i.e. used to create web application.
- Servlet is an API that provides many interfaces and classes including documentations.
- Servlet is an interface that must be implemented for creating any servlet.
- Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.
- Servlet is a web component that is deployed on the server to create dynamic web page.
Steps to Create Servlet Application using tomcat server
To create a Servlet application you need to follow the below mentioned steps. These steps are common for all the Web server. In our example we are using Apache Tomcat server. Apache Tomcat is an open source web server for testing servlets and JSP technology. Download latest version of Tomcat Server and install it on your machine.
After installing Tomcat Server on your machine follow the below mentioned steps :
- Create directory structure for your application.
- Create a Servlet
- Compile the Servlet
- Create Deployement Descriptor for your application
- Start the server and deploy the application
All these 5 steps are explained in details below, lets create our first Servlet Application.
1. Creating the Directory Structure
Sun Microsystem defines a unique directory structure that must be followed to create a servlet application.
Create the above directory structure inside Apache-Tomcat\webapps directory. All HTML, static files(images, css etc) are kept directly under Web application folder. While all the Servlet classes are kept inside
classes
folder.
The
web.xml
(deployement descriptor) file is kept under WEB-INF
folder.Creating a Servlet
There are three different ways to create a servlet.
- By implementing Servlet interface
- By extending GenericServlet class
- By extending HttpServlet class
But mostly a servlet is created by extending HttpServlet abstract class. As discussed earlier HttpServlet gives the definition of
service()
method of the Servlet interface. The servlet class that we will create should not override service()
method. Our servlet class will override only doGet()
or doPost()
method.
When a request comes in for the servlet, the Web Container calls the servlet's
service()
method and depending on the type of request the service()
method calls either the doGet()
or doPost()
method.
NOTE: By default a request is Get request.
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public MyServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResposne response) throws ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>Hello Readers</h1>"); out.println("</body></html>"); } }
Write above code in a notepad file and save it as MyServlet.java anywhere on your PC. Compile it(explained in next step) from there and paste the class file into
WEB-INF/classes/
directory that you have to create inside Tomcat/webapps directory.Compiling a Servlet
To compile a Servlet a JAR file is required. Different servers require different JAR files. In Apache Tomcat server
servlet-api.jar
file is required to compile a servlet class.
Steps to compile a Servlet
- Set the Class Path.
- Download servlet-api.jar file.
- Paste the servlet-api.jar file inside
Java\jdk\jre\lib\ext
directory. - Compile the Servlet class.
NOTE: After compiling your Servlet class you will have to paste the class file into
WEB-INF/classes/
directory.Create Deployment Descriptor
Deployment Descriptor(DD) is an XML document that is used by Web Container to run Servlets and JSP pages. DD is used for several important purposes such as:
- Mapping URL to Servlet class.
- Initializing parameters.
- Defining Error page.
- Security roles.
- Declaring tag libraries.
We will discuss about all these in details later. Now we will see how to create a simple web.xml file for our web application.
Start the Server
Double click on the startup.bat file to start your Apache Tomcat Server.
Or, execute the following command on your windows machine using RUN prompt.
C:\apache-tomcat-7.0.14\bin\startup.bat
Starting Tomcat Server for the first time
If you are starting Tomcat Server for the first time you need to set JAVA_HOME in the Enviroment variable. The following steps will show you how to set it.
- Right Click on My Computer, go to Properites.
- Go to Advanced Tab and Click on Enviroment Variables... button.
- Click on New button, and enter JAVA_HOME inside Variable name text field and path of JDK inside Variable value text field. Click OK to save.
Run Servlet Application
Open Browser and type http:localhost:8080/First/hello
No comments:
Post a Comment