You are on page 1of 9

What is servlet?

An Introduction to Java Servlets:


The basic aim of servlet is to develop web applications. Before servlets came into picture there was a specification called CGI. Servlets specification developed by SUN and released to the industry. Lot of server vendors came forward for implementing these specifications of servlets. Servlets are nothing but set of rules given by SUN in the form of Interfaces. The server vendors have developed their own classes by implementing the interfaces developed by SUN. The collection of classes developed by server vendors and Interfaces developed by SUN are known as Servlet-API.
What is servlet
A servlet is a server side platform independent, dynamic and multithread java program, which runs in the context of server for extending the functionality of server. When multiple users make a request to the same servlet then all requests will be processed by the container by creating multiple threads for the same servlet. In order to deal with these servlet programming, we must import javax.servlet.*; javax.servlet.http.*; into our java program.

javax.servlet.Servlet is one of pre-defined top most interface, which is containing life cycle methods[init(), service(), destroy() i will describe about these methods later] for servlet. javax.servlet.GenericServlet is one of the predefined abstract class which is implementing javax.servlet.Servlet interface, this GenericServlet class is used for developing protocol independent applications [ But in real time we will use protocol dependent applications only, so GenericServlet wont be appeared in real time ] Javax.servlet.http.HttpServlet is sub class of GenericServlet used for developing protocol dependent application [ HTTP protocol ]

Consider Myserlet.java is our own class always recommended to extends HttpServlet servlet, as our current internet world is supporting HTTP protocol right. In real time, we will use HttpServlet only not GenericServlet remember.

Steps to Write Java Servlet Program:


Let us see the basic steps to develop java servlet application Servlet program is not like, writing java code and execute through command prompt. We need to follow the following steps in order to develop any servlets program. Even for a simple HelloWorld program also one must follow this standard directory structure which is prescribed by sun.

Create one root directory with your own name, and create another directory within that directory with name src and now write one servlet program and copy into that src folder Now create another folder web-inf in the root directory, this web-inf folder contains web.xml file will see later Create classes folder with in web-inf folder

Compile our servlet program which is in src folder, so we will get one .class file right, just copy this .class file into classes folder in web-inf folder By the way, we are going to run our servlet by using Tomcat server. So we must set the class path for Tomcat related jar file, servlet-api.jar You can find this jar file in your Tomcat directory\common\lib\, Similarly if we are using Weblogic server, we need to set the class path for weblogic related jar files, i.e weblogic.jar which is in c [your drive]:\bea\weblogic\server\lib If our servlet is dealing with any database like MySql or Oracle, it is mandatory for us to create a folder lib with in web-inf folder Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you how in the first application

Directory Structure:

Steps To Develop Servlet Program


Set servlet-api.jar ( Will be available in your tomcat/lib folder ) in your class path Open notepad or any text editor EditPlus or some thing Import javax.servlet.*; Import javax.servlet.http.*; Create one user defined class whose modifier must be public, which must extends javax.servlet.GenericServlet[Not recommended] or javax.servlet.http.HttpServlet[recommended] We know GenericServlet implementing Servlet interface, and HttpServlet is the sub class of GenericServlet. So by default all life cycle methods will be overridden in GenericServlet class, so just override the required [No need all] life cycle methods of servlet in HttpServlet.

Save your servlet program by giving an extension .java You must place your .java files in root folder or in src folder, and .class files in classes files Compile and check the result

Yeah these are the basic and default steps for writing any servlet application, i will tell you the more(web.xml, deployments and all) while we are working with the examples Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you in the first example.

How to Write Deployment Descriptor,web.xml In Servlet:


Deployment Descriptor [ web.xml ] in Servlets Syntax
<web-app> <servlet> <servlet-name> Give Some Dummy Name </servlet-name> <servlet-class>/Flly qualified name of our servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> Give Some Dummy Name </servlet-name> <url-pattern>/UserFriendlyName</url-pattern> </servlet-mapping> </web-app>

We must call our servlet by typing its <url-pattern> only For example http://localhost:8080/OurServletApplicationFolderName/UserFriendlyName Once we call our servlet just like above, then the server will first loads web.xml and verifies whether the url pattern we are calling is same as what in <url-pattern> tag in web.xml [Line number 8] or not If matched, it will checks the <servlet-name> [Line number 7] and will jump to <servlet-name> [Line number 3] in <servlet> tag and, will check whether the value of <servlet-name> in line number 3 & line number 7 are same or not if same it will load our class in <servlet-class> [line number 4] Finally our .class file will be executed, so we can check the output

How to Set Classpath For Servlet,setting Classpath For servlet-api.jar:


As i told you in the previous article, we should set servlet-api.jar in our class path, in order to work with servlet applications. We cannot run servlet applications if you didnt set servlet-api.jar.

Where Can You Download servlet-api.jar:


You can download servlet-api.jar by searching in Google Or just open your Tomcat installation directory and search for lib folder > there you can find servletapi.jar

Setting Classpath:

Open command prompt Type this command, set classpath=C:\Program Files (x86)\apache-tomcat-7.0.33\lib; Thats it

But friends, dont worry. We are going to run the servlet application in Eclipse so things are very very easy, i will show you in the first program.

Servlet Hello World Example in Eclipse IDE with Tomcat Server:


We will see the first servlet application directly in Eclipse IDE and i am using Tomcat 7, friends doing java servlet without eclipse is really tedious we cannot do everything individually and copying related files into the folders and finally copying the application into tomcat webapps directory bla bla.., So we can do it very easily in Eclipse IDE will see how. Open Eclipse IDE [ Download any version from Google ], Goto File > New > Dynamic web Project

Give Project name and click on Finish

So you can check the directory structure like this..

We should write .java files in src folder and .class files will be automatically copied into build folder including the package, so i am going to create .java file OnGeneric.java [ First application i am going to tell you using GenericServlet, from the next application i will take HttpServlet only ]

OnGeneric.java before setting servlet-api.jar:

Directory Screen short will showing some errors see.

So we need to create the classpath for servlet-api.jar > right click on ROOT folder [ Servlet-Hello-World-Java4s ] > Properties > now a window will open in front of you > left side click on Java Build Path > and click on Add External Jars and add the servlet-api.jar from the tomcat folder > and click on ok

Thats it, check now all the errors will be gone