Created by Mohammad Hossein Rohban
Student of Sharif University of technology
Computer Engineering Department
In
the demanding world of information technology, it is most important to develop
enterprise applications in short time and leverage the speed, security and
reliability of the server-side technologies.
To reduce cost and time of development, JavaTM 2 Platform Enterprise Edition provides a component-based approach that enables the developer to reuse the components in different parts of development.
A component in this approach is a self-contained functional software unit which is assembled into the J2EE application with its related classes and files and that communicate with other components.
J2EETM
uses a multi-tiered distributed application model, which extends the ordinary
two-tiered client/server model by a multi-threaded application server.
In this platform each application consists of
different components that would be installed on different machines depending on
the tier they belong to: client machine, J2EE server machine, Enterprise
information system (EISTM) machine.
In this approach not only you can separate the
logic solution and application programming interfaces (APIs), but also you can
take advantage of off-loading complex business rules to the J2EE server rather
than the client.
There are thus, four types of tiers in J2EE
specifications:
·
Client-tier components run
on the client machine.
·
Web-tier components run on
the J2EE server.
·
Business-tier components
run on the J2EE server.
·
Enterprise information
system (EIS)-tier software runs on the EIS server.
and also three types of components related to
these tiers :
·
Application clients and
applets are client components.
·
Java Servlets and
JavaServerTM Pages (JSPTM) are web components.
·
Enterprise JavaBeansTM
(EJBTM) are business components.
In this article we are
going to introduce Java Servlets and see when and why we may use them in J2EE
applications.
What is a
JavaServlet?
As I mention before,
JavaServlets are web components in J2EE environment and they run on J2EE
server. They may be used to generate either static or dynamic HTML pages (in
response to a HTTP request from a client), which are said to be on-the-fly HTML
generation to have a thin client.
A
thin client consists of a web browser and dynamic web pages, in contrast with
thick clients, which consist of application clients. In thin client model
usually a JavaServlet on J2EE server generates dynamic web pages. Why may we
use thin client model in our applications?
If
we use this model, we may off-load functionality to the server and this makes
the management and deployment of the application much more easier. However
keeping more functionality close to the client can make for a better-perceived
user experience.
Although
JavaServlets are considered to be one of the components of a J2EE application,
we may use HTTP JavaServlets separately in a web server in situation, which the
logic solution is too simple and there is no need to use EJBTM (In this way the simple client/server model
is being used and a JavaServlet acts as a middle tier). For example if we want
to create a web site to simply gather the votes of visitors we may use them
separately.
Why
do we prefer using JavaServlets but not Common Gateway Interface (CGI)? It has
many reasons:
·
Efficiency: In traditional CGI a new process
starts for each HTTP request. If the
CGI program does a relatively fast operation, the overhead of starting the
process can dominate the execution time. With servlets, the Java Virtual
Machine stays up, and each request is handled by a lightweight Java thread, not
a heavyweight operating system process. Similarly, in traditional CGI, if there
are N simultaneous requests to the same CGI program, then the code for
the CGI program is loaded into memory N times. With servlets, however, there
are N threads but only a single copy of the servlet class.
·
Easy to use: If you know Java, why wasting
your time and learning Perl to develop CGI programs? So you can take advantage
of what you have learned before.
·
Portable: According to Java platform
independence, you may develop a servlet in one web server and use it without
change in other servers.
Servlets
work within a request/response model. If a request is sent by a client to the
server, the server processes that request and reflect the state of both client
and server at the time of the request by sending a response to the client.
Request can come in form of HTTP, FTP or custom protocol. Both POST and GET
methods can be used to send request to a servlet.
All
you have to do to have a HTTP servlet is to create a class which extends
javax.servlet.http.HttpServlet and override the "void
doGet(HttpServletRequst, HttpServletResponse) throws ServletException,
IOException" to manage a GET request or doPost() method with the same
signature to handle a POST request . You may use J2EETM API to get
more information.
Servlets run on the web
server platform as part of the same process as the web server itself. The web
server is responsible for initializing, invoking, and destroying each servlet
instance.
A web server communicates
with a servlet through a simple interface, javax.servlet.Servlet.
This interface
consists of three main methods:
·
init()
·
service()
·
destroy()
and two ancillary
methods:
·
getServletConfig()
·
getServletInfo()
You may notice a similarity
between this interface and that of Java applets. This is by design! Servlets
are to web servers what applets are to web browsers. An applet runs in a web browser,
performing actions it requests through a specific interface. A servlet does the
same, running in the web server.
The
class HttpServlet provides an implementation of the service() method that
dispatches the HTTP messages to one of several special methods. These methods
are:
·
doGet()
·
doHead()
·
doDelete()
·
doOptions()
·
doPost()
·
doTrace()
and correspond directly with the HTTP protocol
methods.
As shown in the following
diagram, the service() method interprets each HTTP method and determines if it
is an HTTP GET, HTTP POST, HTTP HEAD, or other HTTP protocol method:

HTTP Methods
handling
It is very common to have
servlets connect to databases through JDBC. This allows you to better control
access to the database by only permitting the middle-tier to communicate with
the database.
If your database server
includes sufficient
simultaneous connection licenses, you can even setup database
connections once, when the servlet is initialized, and pool the connections
between all the different service requests.
In my opinion it is better
to develop a J2EE application even if we have a simple logic solution rather
than creating a JavaServlet separately, this makes the later developments and
managements much more easier.
·
www.apl.jhu.edu/~hall/java/servlet-tutorial
Home Page
for Marty Hall, Senior Computer
Scientist, JHU/APL Research and
Technology Development Center
·
http://java.sun.com/j2ee/tutorial/1_3-fcs/
J2EE tutorial , A
beginner's guide to developing enterprise applications
on the JavaTM 2 Platform, Enterprise Edition SDK
version 1.3
·
http://developer.java.sun.com/developer/onlineTraining/Servlets/Fundamentals/