You are on page 1of 5

Java servlets

A Servlet is a Java class in Java EE that conforms to the Java Servlet API, a protocol by which a Java class
may respond to HTTP requests. They are not tied to a specific client-server protocol, but are most often used
with this protocol. The word "Servlet" is often used in the meaning of "HTTP Servlet".[1] Thus, a software
developer may use a servlet to add dynamic content to a Web server using the Java platform. The generated
content is commonly HTML, but may be other data such as XML. Servlets are the Java counterpart to non-
Java dynamic Web content technologies such as CGI and ASP.NET. Servlets can maintain state in session
variables across many server transactions by using HTTP cookies, or URL rewriting.

The servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions
of a Web container and a servlet.[1] A Web container is essentially the component of a Web server that
interacts with the servlets. The Web container is responsible for managing the lifecycle of servlets, mapping
a URL to a particular servlet and ensuring that the URL requester has the correct access rights.

A Servlet is an object that receives a request and generates a response based on that request. The basic
servlet package defines Java objects to represent servlet requests and responses, as well as objects to reflect
the servlet's configuration parameters and execution environment. The package javax.servlet.http
defines HTTP-specific subclasses of the generic servlet elements, including session management objects that
track multiple requests and responses between the Web server and a client. Servlets may be packaged in a
WAR file as a Web application.

Servlets can be generated automatically from JavaServer Pages (JSP) by the JavaServer Pages compiler. The
difference between Servlets and JSP is that Servlets typically embed HTML inside Java code, while JSPs
embed Java code in HTML. While the direct usage of Servlets to generate HTML (as shown in the example
below) is relatively rare nowadays, the higher level MVC web framework in Java EE (JSF) still explicitly
uses the Servlet technology for the low level request/response handling via the FacesServlet. A somewhat
older usage is to use servlets in conjunction with JSPs in a pattern called "Model 2", which is a flavour of
the model-view-controller pattern.

history
The complete servlet specification was created by Sun Microsystems, with version 1.0 finalized in
June 1997. Starting with version 2.3, the servlet specification was developed under the Java Community
Process. JSR 53 defined both the Servlet 2.3 and JavaServer Page 1.2 specifications. JSR 154 specifies the
Servlet 2.4 and 2.5 specifications. As of March 26, 2010, the current version of the servlet specification is
3.0.

In his blog on java.net, Sun veteran and GlassFish lead Jim Driscoll details the history of servlet technology.
James Gosling first thought of servlets in the early days of Java, but the concept did not become a product
until Sun shipped the Java Web Server product. This was before what is now the Java Platform, Enterprise
Edition was made into a specification.

Servlet API history


Servlet API
Released Platform Important Changes
version
December JavaEE 6, Pluggability, Ease of development, Async Servlet,
Servlet 3.0
2009 JavaSE 6 Security, File Uploading
September JavaEE 5,
Servlet 2.5 Requires JavaSE 5, supports annotation
2005 JavaSE 5
November J2EE 1.4, J2SE
Servlet 2.4 web.xml uses XML Schema
2003 1.3
J2EE 1.3, J2SE
Servlet 2.3 August 2001 Addition of Filter
1.2
J2EE 1.2, J2SE Becomes part of J2EE, introduced independent web
Servlet 2.2 August 1999
1.2 applications in .war files
November First official specification, added RequestDispatcher,
Servlet 2.1 Unspecified
1998 ServletContext
Servlet 2.0 JDK 1.1 Part of Java Servlet Development Kit 2.0
Servlet 1.0 June 1997 undefined

ADVANTAGE OVER CGI


The advantages of using servlets is their fast performance and ease of use
combined with more power over traditional CGI (Common Gateway Interface). Traditional CGI scripts
written in Java have a number of disadvantages when it comes to performance:

 When a HTTP request is made, a new process is created for each call of the CGI script. This
overhead of process creation can be very system-intensive, especially when the script does relatively
fast operations. Thus, process creation will take more time than CGI script execution. Java servlets
solve this, as a servlet is not a separate process. Each request to be handled by a servlet is handled by
a separate Java thread within the Web server process, omitting separate process forking by the HTTP
daemon.
 Simultaneous CGI request causes the CGI script to be copied and loaded into memory as many times
as there are requests. However, with servlets, there are the same amount of threads as requests, but
there will only be one copy of the servlet class created in memory that stays there also between
requests.
 Only a single instance answers all requests concurrently. This reduces memory usage and makes the
management of persistent data easy.
 A servlet can be run by a servlet engine in a restrictive environment, called a sandbox. This is similar
to an applet that runs in the sandbox of the Web browser. This makes a restrictive use of potentially
harmful servlets possible.[1]

1. The container calls the no-arg constructor.


2. The Web container calls the init() method. This method initializes the servlet and must be called
before life of a servlet, the init() method is called only once.
3. After initialization, the servlet can service client requests. Each request is serviced in its own
separate thread. The Web container calls the service() method of the servlet for every request. The
service() method determines the kind of request being made and dispatches it to an appropriate
method to handle the request. The developer of the servlet must provide an implementation for these
methods. If a request for a method that is not implemented by the servlet is made, the method of the
parent class is called, typically resulting in an error being returned to the requester.
4. Finally, the Web container calls the destroy() method that takes the servlet out of service. The
destroy() method, like init(), is called only once in the lifecycle of a servlet.
[edit] Example

Here is a simple servlet that just generates HTML. Note that HttpServlet is a subclass of GenericServlet, an
implementation of the Servlet interface. The service() method dispatches requests to methods doGet(),
doPost(), doPut(), doDelete(), etc., according to the HTTP request.

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n" +
"<html>\n" +
"<head><title>Hello World</title></head>\n" +
"<body>\n" +
"<h1>Hello, world!</h1>\n" +
"</body></html>");
}
}

Usage

Servlets are most often used to

 process or store data that was submitted from an HTML form


 provide dynamic content such as the results of a database query
 manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into
the shopping cart of the appropriate customer. [1]

A JavaServer Pages compiler, or JSP compiler, is a program that parses JavaServer Pages (JSPs), and
transforms them into executable Java Servlets. A program of this type is usually embedded into an
application server and run automatically the first time a JSP is accessed, but pages may also be precompiled
for better performance, or compiled as a part of the build process to test for errors.

Most JSP containers support configuring how often the container checks JSP file timestamps to see if the
page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during
software development, and a longer interval (perhaps minutes, or even never) for a deployed Web
application.

Jetty is a pure Java-based HTTP server and servlet container (Application server) developed as a free and
open source project as part of the Eclipse Foundation. It is currently used in products such as Google App
Engine, HP OpenView, IBM Tivoli NetView, Zimbra, ActiveMQ, Alfresco, Eclipse, FUSE[disambiguation needed],
Apache Geronimo, JBoss, Liferay, Maven and Ubuntu[citation needed]. Jetty is also used as a standard Java
application server by many open source projects such as Eucalyptus and Hadoop[citation needed].

Apache Tomcat (or Jakarta Tomcat or simply Tomcat) is an open source servlet container
developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the
JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server
environment for Java code to run.

Tomcat should not be confused with the Apache web server, which is a C implementation of an HTTP web
server; these two web servers are not bundled together. Apache Tomcat includes tools for configuration and
management, but can also be configured by editing XML configuration files.

JBoss" redirects here. For other uses, see JBoss (disambiguation).

JBoss Application Server

Developer(s) Red Hat

Stable release 6.0.0 / December 28, 2010; 41 days ago

Preview 7.0.0.Alpha1 / November 1, 2010; 3 months


release ago

Written in Java

Operating
Cross-platform
system

Type Application server

License GNU Lesser General Public License

http://www.jboss.com/products/platforms/a
Website
pplication/

JBoss Application Server (or JBoss AS) is a free software/open-source Java EE-based application server.
An important distinction for this class of software is that it not only implements a server that runs on Java,
but it actually implements the Java EE part of Java. Because it is Java-based, the JBoss application server
operates cross-platform: usable on any operating system that supports Java. JBoss AS was developed by
JBoss, now a division of Red Hat.

GlassFish is an open source application server project led by Sun Microsystems for the Java EE platform.
The proprietary version is called Sun GlassFish Enterprise Server. GlassFish is free software, dual-licensed
under two free software licences: the Common Development and Distribution License (CDDL) and the
GNU General Public License (GPL) with the classpath exception.

GlassFish is based on source code released by Sun and Oracle Corporation's TopLink persistence system. It
uses a derivative of Apache Tomcat as the servlet container for serving Web content, with an added
component called Grizzly which uses Java New I/O (NIO) for scalability and speed.

In computing, IBM WebSphere refers to a brand of software products in the genre of enterprise software
known as "application and integration middleware". These software products are used by end-users to create
applications and integrate applications with other applications. IBM WebSphere has been available to the
general market for over a decade.

Occasionally, the term "IBM WebSphere" also refers in popular usage to one specific product: IBM
WebSphere Application Server (WAS

You might also like