Servlet Web Server Introduction Computer Science Essay

Published: Last Edited:

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

A web server is that the combination of pc and also the program put in thereon. net server interacts with the consumer through an online browser. It delivers the online pages to the consumer Associate in Nursingd to an application by mistreatment the online browser and he hypertext transfer protocol protocols severally. we will conjointly outline the online server because the package of sizable amount of programs put in on a pc connected to net or computer network for downloading the requested files mistreatment File Transfer Protocol, serving e-mail and building and commercial enterprise web content. an online server works on a consumer server model. A pc connected to the net or computer network should have a server program. whereas talking concerning Java language then an online server could be a server that's wont to support the online element just like the Servlet and JSP. Note that the online server doesn't support to EJB (business logic element) component.

A pc connected to the net for providing the services to alittle company or a chain store could contain the hypertext transfer protocol server (to access and store the online pages and files), SMTP server (to support mail services), FTP server ( for files downloading) and NNTP server (for newsgroup). the pc containing all the higher than servers is named the online server. net service suppliers and huge corporations could have all the servers like hypertext transfer protocol server, SMTP server, FTP server and lots of additional on separate machines. just in case of Java, an online server will be outlined because the server that solely supports to the online element like servlet and jsp. Notice that it doesn't support to the business element like EJB.

Servlet Container

A servlet instrumentality is nothing however a compiled, feasible program. the most perform of the instrumentality is to load, initialize and execute servlets. The servlet instrumentality is that the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications square measure developed by Sun beneath the Java Community method.

A instrumentality handles sizable amount of requests because it will hold several active servlets, listeners etc. it's attention-grabbing to notice here that the instrumentality and therefore the objects in a very instrumentality square measure multithreaded. thus every object should be thread safe in {an exceedingly|in a very} instrumentality because the multiple requests square measure being handled by the instrumentality attributable to the doorway of over one thread to an object at a time.

Note : A Servlet instrumentality might run stand alone i.e. while not an online server or perhaps on another host.

we are able to reason the servlet containers as:

I. easy|an easy|a straightforward} servlet instrumentality isn't absolutely purposeful and thus it will solely run terribly simple servlets and will the subsequent :

Wait for communications protocol request.

Construct a ServletRequest object and a ServletResponse object.

If the request is for a static resource, invoke methodology} method of the StaticResourceProcessor instance, passing the ServletRequest and ServletResponse objects.

If the request is for a servlet, load the servlet category and invoke its service methodology, passing the ServletRequest and ServletResponse objects. Note that during this servlet instrumentality, the servlet category is loaded each time the servlet is requested.

II. a completely purposeful servlet instrumentality in addition will the subsequent for every communications protocol request for a servlet:

When the servlet is termed for the primary time, load the servlet category and decision its init methodology (once only).

For each request, construct associate degree instance of javax.servlet.ServletRequest associate degreed an instance of javax.servlet.ServletResponse.

Invoke the servlet's service methodology, passing the ServletRequest and ServletResponse objects.

When the servlet category is stop working, decision the servlet's destroy methodology and unload the servlet category.

Now lets see what a servlet instrumentality will for every HTTP request for a servlet, normally :

The servlet instrumentality hundreds the servlet category and calls the init methodology of the servlet as shortly because the servlet is termed for the primary time.

Then this instrumentality makes associate degree instance of javax.servlet.ServletRequest and javax.servlet.ServletResponse for every request.

Then it passes the ServletRequest and ServletResponse objects by invoking the servlet's service methodology.

Finally, it calls the destroy methodology and unload the servlet category once the servlet category is to be stop working.

What is Java Servlets?

Servlets area unit server aspect parts that give a robust mechanism for developing server aspect programs. Servlets give component-based, platform-independent ways for building Web-based applications, while not the performance limitations of CGI programs. in contrast to proprietary server extension mechanisms (such because the Netscape Server API or Apache modules), servlets area unit server furthermore as platform-independent. This leaves you liberated to choose a "best of breed" strategy for your servers, platforms, and tools. victimization servlets net developers will produce quick and economical server aspect application which may run on any servlet enabled net server. Servlets run entirely within the Java Virtual Machine. Since the Servlet runs at server aspect thus it doesn't checks the browser for compatibility.

Methods of Servlets

A Generic servlet contains the subsequent 5 methods:


public void init(ServletConfig config) throws ServletException

The init() methodology is termed just the once by the servlet instrumentation throughout the lifetime of a servlet. By this init() methodology the servlet get to grasp that it's been placed into service.

The servlet can't be place into the service if

The init() methodology doesn't come back at intervals a fix time set by the net server.

It throws a ServletException

Parameters - The init() methodology takes a ServletConfig object that contains the data formatting parameters Associate in Nursingd servlet's configuration and throws a ServletException if an exception has occurred.


public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

Once the servlet starts obtaining the requests, the service() methodology is termed by the servlet instrumentation to retort. The servlet services the client's request with the assistance of 2 objects. These 2 objects javax.servlet.ServletRequest and javax.servlet.ServletResponse ar gone along the servlet instrumentation.

The standing code of the response invariably ought to be set for a servlet that throws or sends a slip-up.

Parameters - The service() methodology takes the ServletRequest object that contains the client's request and also the object ServletResponse contains the servlet's response. The service() methodology throws ServletException and IOExceptions exception.


public ServletConfig getServletConfig()

This methodology contains parameters for data formatting and startup of the servlet and returns a ServletConfig object. This object is then passed to the init methodology. once this interface is enforced then it stores the ServletConfig object so as to come back it. it's done by the generic category that implements this inetrface.

Returns - the ServletConfig object


public String getServletInfo()

The information concerning the servlet is came by this methodology like version, author etc. This methodology returns a string that ought to be within the style of plain text and not any quite markup.

Returns - a string that contains the knowledge concerning the servlet


public void destroy()

Life cycle of Servlet

The life cycle of a servlet may be classified into four parts:

Loading and Inatantiation: The servlet instrumentality hundreds the servlet throughout startup or once the primary request is created. The loading of the servlet depends on the attribute of net.xml file. If the attribute incorporates a positive worth then the servlet is load with loading of the instrumentality otherwise it load once the primary request comes for service. once loading of the servlet, the instrumentality creates the instances of the servlet.

Initialization: once making the instances, the servlet instrumentality calls the init() technique and passes the servlet formatting parameters to the init() technique. The init() should be referred to as by the servlet instrumentality before the servlet will service any request. The formatting parameters persist untill the servlet is destroyed. The init() technique is named just one occasion throughout the life cycle of the servlet.

The servlet are going to be obtainable for service if it's loaded with success otherwise the servlet instrumentality unloads the servlet.

Servicing the Request: once with success finishing the formatting method, the servlet are going to be obtainable for service. Servlet creates seperate threads for every request. The sevlet instrumentality calls the service() technique for mating any request. The service() technique determines the sort of request and calls the acceptable technique (doGet() or doPost()) for handling the request and sends response to the shopper victimization the strategies of the response object.

Destroying the Servlet: If the servlet isn't any longer required for mating any request, the servlet instrumentality calls the destroy() technique . just like the init() technique this technique is additionally referred to as just one occasion throughout the life cycle of the servlet. job the destroy() technique indicates to the servlet instrumentality to not sent the any request for service and also the servlet releases all the resources related to it. Java Virtual Machine claims for the memory related to the resources for trash pickup.

Introduction to Server Side Programming

All people (or most of us) would have started programming in Java with the ever celebrated ?Hello World!? program. If you'll be able to recall, we tend to saved this file with a .java extension and later compiled the program exploitation javac and so dead the category file with java. with the exception of introducing you to the language basics, the purpose to be noted concerning this program is that ? ?It may be a consumer aspect program?. this implies that you simply write, compile and additionally execute the program on a consumer machine (e.g. Your PC). No doubt, this can be the simplest and quickest thanks to write, compile and execute programs. But, it's very little sensible significance once it involves universe programming.

Why Server aspect Programming?

Though it's technically possible to implement virtually any business logic exploitation consumer aspect programs, logically or functionally it carries no ground once it involves enterprise applications (e.g. banking, air ticketing, e-shopping etc.). To any justify, going by the consumer s ide programming logic; a bank having ten,000 clients would mean that every customer ought to have a replica of the program(s) in his or her laptop that interprets to ten,000 programs! additionally, there square measure problems like security, resource pooling, synchronal access and manipulations to the info that merely can't be handled by consumer aspect programs. the solution to most of the problems cited on top of is ? ?Server aspect Programming?. Figure-1 illustrates Server aspect design within the simplest terms.

Advantages of Server Side Programs

The list below highlights a number of the necessary benefits of Server facet programs.

All programs reside in one machine referred to as the Server. Any variety of remote machines (called clients) will access the server programs.

New functionalities to existing programs may be extra at the server facet that the clients? will advantage while not having to alter something from their facet.

Migrating to newer versions, architectures, style patterns, adding patches, switch to new databases may be done at the server facet while not having to hassle regarding clients? hardware or software system capabilities.

Issues about enterprise applications like resource management, concurrency, session management, security and performance ar managed by service facet applications.

They are moveable and possess the aptitude to come up with dynamic and user-based content (e.g. displaying dealing data of mastercard or revolving credit reckoning on user?s choice).

Types of Server facet Programs

Active Server Pages (ASP)

Java Servlets

Java Server Pages (JSPs)

Enterprise Java Beans (EJBs)


To summarize, the target of server facet programs is to centrally manage all programs about a specific application (e.g. Banking, Insurance, e-shopping, etc). shoppers with vacant minimum demand (e.g. Pentium II, Windows XP skilled, MS web soul and an online connection) will expertise the facility and performance of a Server (e.g. IBM Mainframe, UNIX Server, etc) from a distant location while not having to compromise on security or speed. a lot of significantly, server programs aren't solely moveable however additionally possess the aptitude to come up with dynamic responses supported user’s request.

Introduction to Java Servlets

Basic Servlet Structure

As seen earlier, Java servlets square measure server facet programs or to be a lot of specific; internet applications that run on servers that abide by communications protocol protocol. The javax.servlet and javax.servlet.http packages offer the required interfaces and categories to figure with servlets. Servlets typically extend the HttpServlet category and override the doGet or the doPost strategies. additionally, different strategies like init, service and destroy conjointly referred to as as life cycle strategies may well be used which can be mentioned within the following section. The skeleton of a servlet is given in Figure

Installation, Configuration and running Servlets

Installation of Tomcat Server and JDK

As mentioned earlier, Apache?s domestic cat Server is free software system out there for transfer @ the present version of domestic cat Server is vi.0 (as of Nov 2007). This Server supports Java Servlets two.5 and Java Server Pages (JSPs) two.1 specifications. just in case of doubt or confusion, you'll be able to seek advice from the rife documentation repository out there on this web site.

Important software system needed for running this server is Sun?s JDK (Java Development Kit) and JRE (Java Runtime Environment). the present version of JDK is vi.0. Like domestic cat, JDK is additionally free and is on the market for transfer at

Configuring domestic cat Server

Set JAVA_HOME variable - you have got to line this variable that points to the bottom installation directory of JDK installation. (e.g. c:\program file\java\jdk1.6.0). you'll be able to either set this from the electronic communication or from My laptop -> Properties -> Advanced -> atmosphere Variables.

Specify the Server Port ? you'll be able to amendment the server port from eighty to 80 (if you would like to) by written material the server.xml move into the conf folder. the trail would be one thing like this ? c:\program files\apache software system foundation\tomcat6\conf\server.xml

Run domestic cat Server

Once the on top of pre-requisites ar taken care, you'll be able to take a look at as whether or not the server is with success put in as follows:

Step 1

Go to C:\Program Files\Apache software system Foundation\Tomcat vi.0\bin and double click on tomcat6


attend Start->Programs->Apache domestic cat vi.0 -> Monitor domestic cat. you'll notice Associate in Nursing icon seem on the correct aspect of your standing Bar. Right click on this icon and click on on begin service.

Step 2

Open your Browser (e.g. MS web Explorer) and sort the subsequent computer address :

http://localhost/ (If you have got modified to port # to 80)


Open your Browser (e.g. MS web Explorer) and sort the subsequent computer address :

http://localhost:8080/ (If you have got NOT modified the default port #)

In either case, you must get a page the same as the one in Figure-8 that signifies that the domestic cat Server is with success running on your machine.

Compile and Execute your Servlet

This section through a step by step (and illustration) approach explains as the way to compile so run a servlet victimisation domestic cat Server. tho' this clarification is particular to domestic cat, the procedure explained holds true for alternative net servers too (e.g. JRun, Caucho?s Resin).

Step 1 Compile your servlet program

The first step is to compile your servlet program. The procedure is not any completely different from that of writing and aggregation a java program. But, the purpose to be noted is that neither the javax.servlet.* nor the javax.servlet.http.* is an element of the quality JDK. it's to be completely additional within the CLASSPATH. The set of categories needed for writing servlets is on the market during a jar file referred to as servlet-api.jar. This jar file may be downloaded from many sources. However, the best one is to use this jar file on the market with the Felis catus server (C:\Program Files\Apache computer code Foundation\Tomcat half-dozen.0\lib\servlet-api.jar). you would like to incorporate this path in CLASSPATH. Once you've got done this, you may be ready to with success compile your servlet program. make sure that the category file is made with success.

Step 2 Create your Web application folder

The next step is to make your net application folder. The name of the folder are often any valid and logical name that represents your application (e.g. bank_apps, airline_tickets_booking, handcart,etc). however the foremost vital criterion is that this folder ought to be created beneath webapps folder. the trail would be similar or near this - C:\Program Files\Apache software package Foundation\Tomcat six.0\webapps. For demo purpose, allow us to produce a folder known as demo-examples beneath the webapps folder

Figure- depicts the same.

Step 3 Create the WEB-INF folder

The third step is to form the WEB-INF folder. This folder ought to be created underneath your internet application folder that you simply created within the previous step. Figure-10 shows the WEB-INF folder being placed underneath the demo-examples folder.

Figure WEB-INF folder inside web application folder

Step 4 Create the web.xml file and the classes folder

The fourth step is to form the net.xml file and therefore the categories folder. make sure that the net.xml and categories folder area unit created below the WEB-INF folder. Figure-11 shows this file and folder being placed below the WEB-INF folder.

Figure web.xml file and the classes folder

Note rather than making the online.xml file a simple means would be to repeat associate existing net.xml file (e.g. C:\Program Files\Apache software package Foundation\Tomcat vi.0\webapps\examples\WEB-INF) and paste it into this folder. you'll later edit this file and add relevant data to your net application.

Step 5 Copy the servlet class to the classes folder

We need to copy the servlet class file to the classes folder in order to run the servlet that we created. All you need to do is copy the servlet class file (the file we obtained from Step 1) to this folder. Figure-12 shows the servlet_lifecycle (refer section 1.2.3.) class being placed in the classes folder.

Figure servlet class file placed under classes folder

Step 6 Edit web.xml to include servlet?s name and url pattern

This step involves two actions viz. including the servlet?s name and then mentioning the url pattern. Let us first see as how to include the servlet?s name in the web.xml file. Open the web.xml file and include the servlet?s name as shown in Figure-13.

Figure Include servlet?s name using the <servlet> </servlet> tag

Note ? The servlet-name need not be the same as that of the class name. You can give a different name (or alias) to the actual servlet. This is one of the main reasons as why this tag is used for.

Next, include the url pattern using the <servlet-mapping> </servlet-mapping> tag. The url pattern defines as how a user can access the servlet from the browser. Figure-14 shows the url pattern entry for our current servlet.

Note Please bear in mind that the trail given within the url-pattern could be a relative path. this suggests that this path is w.r.t. your internet applications folder (demo-examples during this case).

Step seven Run house cat server and so execute your Servlet

This step once more involves 2 actions viz. running the net Server and so death penalty the servlet. To run the server, follow the steps explained in Section one.3.3.

After guaranteeing that the net server is running with success, you'll be able to run your servlet. To do this, open your applications programme and enter the universal resource locator as per the net.xml file. the entire universal resource locator that has to be entered within the browser is:


Eureka! Here?s the output of our first servlet. After a long and pain staking effort, we finally got an output! As mentioned in Section 1.2.3. you can keep refreshing the browser window and see for yourself as how i value is incremented (a proof that the doGet is called every time you re-invoke a servlet).