JSP and Servlets in Java: Abstract
JSP and Servlets in Java: Abstract
The following steps explain how the web • A part of the web server called the servlet
server creates the web page using JSP: engine loads the Servlet class and executes
it. During execution, the servlet produces an
• As with a normal page, your browser sends output in HTML format, which the servlet
an HTTP request to the web server. engine passes to the web server inside an
HTTP response.
• The web server recognizes that the HTTP
request is for a JSP page and forwards it to • The web server forwards the HTTP
a JSP engine. This is done by using the URL response to your browser in terms of static
or JSP page which ends with .jsp instead of HTML content.
.html.
• Finally web browser handles the
• The JSP engine loads the JSP page from dynamically generated HTML page inside
disk and converts it into a servlet content. the HTTP response exactly as if it were a
This conversion is very simple in which all static page.
template text is converted to println( )
statements and all JSP elements are 3.2 JSP Life Cycle: JSP lifecycle is
converted to Java code that implements the consists of jspInit(), jspService() and
corresponding dynamic behavior of the jspDestroy(), and these methods are called
page. life cycle methods of the JSP.
• The JSP engine compiles the servlet into an
executable class and forwards the original
request to a servlet engine.
4. Servlet Architecture and lifecycle
Servlets perform the following major tasks: • Send the explicit data (i.e., the document) to
the clients (browsers). This document can
• Read the explicit data sent by the clients be sent in a variety of formats, including
(browsers). This includes an HTML form text (HTML or XML), binary (GIF images),
on a Web page or it could also come from Excel, etc.
an applet or a custom HTTP client program.
• Send the implicit HTTP response to the
• Read the implicit HTTP request data sent by clients (browsers). This includes telling the
the clients (browsers). This includes browsers or other clients what type of
cookies, media types and compression document is being returned (e.g., HTML),
schemes the browser understands, and so setting cookies and caching parameters, and
forth. other such tasks.
• Process the data and generate the results.
4.2 Life Cycle of Servlet: The web
This process may require talking to a
database, executing invoking a Web container maintains the life cycle of a
service, or computing the
response directly.
servlet instance. Let's see the life cycle of 3. init method is invoked. the servlet:
4. service method is invoked.
5. destroy method is invoked.
1. Servlet class is loaded.
2. Servlet instance is created.
JSP Servlets
JSP is a webpage scripting language that can generate Servlets are Java programs that are already compiled
dynamic content. which also creates dynamic web content.
JSP run slower compared to Servlet as it takes Servlets run faster compared to JSP.
compilation time to convert into Java Servlets.
It’s easier to code in JSP than in Java Servlets. Its little much code to write here.
JSP are generally preferred when there is not much servlets are best for use when there is more processing
processing of data required. and manipulation involved.
The advantage of JSP programming over servlets is that There is no such custom tag facility in servlets.
we can build custom tags which can directly call Java
beans.
We can achieve functionality of JSP at client side by There are no such methods for servlets.
running JavaScript at client side.
5.JSP vs Servlet
Conclusion:
In this paper we have taken into
consideration all aspects of java server
pages as well as servlets, discussed about
their architectures and life cycles and their
working. Comparison between the two
technologies has also been made towards
the end of the paper. On comparing and
contrasting the two we find out that main
difference between them is, In servlets both
the presentation and business logic are
place it together. Where as in jsp both are
separated by defining by java beans .In jsp's
the overall code is modulated so the
developer who doesn't know about java can
write jsp pages by simply knowing the
additional tags and class names. One more
important to be considered is servlet take
less time to compile. Jsp is a tool to simplify
the process and make the process automate.
Both are web applications used to produce
web content that mean dynamic web pages.
Both are used to take the requests and
service the clients.
References:
[1] http://www.tutorialspoint.com/jsp/
[2]
http://users.polytech.unice.fr/~buffa/cours/
internet/POLYS/servlets/Servlet-
TutorialOverview.html
[3] http://www.tutorialspoint.com/servlets/
[4]
http://en.wikipedia.org/wiki/Java_Servlet
[5]
http://stackoverflow.com/questions/20953
97/what-is-the-difference-between-
jsfservlet-and-jsp