2. Java Server Pages(JSP) is a technology for developing portable java web applications
that runs on a java web server.
Main motivation behind developing JSP was to develop a technology so that a non-java
programmer or a person with little knowledge of java could write web applications for
java platform.
Initially JSP was developed to replace servlet but now common practice is to use servlet
and JSP together using MVC(model-view-controller) pattern.
JSP allows developer to write html (static content),JSP action tags and java (dynamic
content) code in the same page.
JSPS are compiled into java servlets by a JSP compiler, thus internally JSP page is
converted into servlet, it is must to understand java servlet technology thoroughly for
becoming a good JSP programmer.
3. Java Server Pages (JSP) supports dynamic content which helps developers insert
java code in HTML pages by making use of special JSP tags, most of which start
with <% and end with %>.
JSP enables you to separate the dynamic content of a web page from its
presentation.
It caters to different types of developers:
Web designers, who are responsible for the graphical design of the page.
Java developers, who write the code to generate dynamic content.
4. The JSP pages follows these phases:
Translation of JSP page
Compilation of JSP page
Class loading (class file is loaded by the class loader)
Instantiation (object of the generated servlet is created).
Initialization ( jspinit() method is invoked by the container).
Request processing ( _jspservice() method is invoked by the container).
Destroy ( jspdestroy() method is invoked by the container).
5.
6. 1. Translation of JSPto Servlet code
WebContainertranslates JSPcodeinto aservletsource(.java)file.
Thisisthe firststepin its tediousmultiple phaselife cycle.
Inthetranslation phase,thecontainervalidatesthe syntacticcorrectnessof theJSPpages and tagfiles.
The container interprets the standard directives and actions, and the custom actions referencing tag
libraries usedin thepage.
2. Compilation ofServlet to bytecode
The JSPengine compiles the servlet into an executable class and forwards the original request to a servlet
engine.
The translation of a JSPsource page into its implementation class can happen at any time between initial
deployment of the JSPpage into the JSPcontainer and the receipt and processing of a client request for
the targetJSPpage.
7. 3. LoadingServlet class
• Thejavaservlet classthat wascompiledfrom the JSPsourceisloaded into thecontainer.
4. Creatingservletinstance
• In this executionphasethe container managesoneor more instances of this classin responseto requests
and otherevents.
5. Initialization bycallingjspInit() method
WhenacontainerloadsaJSPit invokesthejspInit()methodbefore servicinganyrequests. If youneedto
performJSP-specificinitialization,override the jspInit().
Typically, initialization is performed only once and aswith the servlet init method, you generally initialize
databaseconnections,openfiles,andcreatelookuptablesinthejspInit method.
public void jspInit()
{
//initializing the code
}
8. 6.RequestProcessingbycalling_jspService() method
Thisphaseof the JSPlife cyclerepresentsall interactions with requests until the JSPis destroyed.
Whenever abrowser requests aJSPandthe pagehasbeen loadedandinitialized, the JSPengine
invokesthe _jspService()method in theJSP.
The_jspService()method takesanHttpServletRequest andanHttpServletResponseasits
parameters .
• void _jspService(HttpServletRequest request,
• HttpServletResponse response) {
// Service handling code...
}
The_jspService()method of aJSPisinvoked onrequest basis.Thisisresponsiblefor generating the
responsefor thatrequest.
9. 7. DestroyingbycallingjspDestroy() method
Thedestruction phaseof the JSPlife cyclerepresentswhen aJSPisbeingremoved from usebya
container.
ThejspDestroy()method isthe JSPequivalentof the destroy method forservlets.
Override jspDestroy(),whenyouneedto perform anycleanup,suchasreleasingdatabase
connectionsor closingopenfiles.
public void jspDestroy()
{
//Your cleanup code goes here.
}
Whenthecallto destroymethod ismadethen,theservletisreadyfor agarbagecollection
Thisisthe endof the JSPlifecycle.
10. The following steps explain how the web server creates the web page using JSP:
As with a normal page, browser sends an HTTP request to the web server.
The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine.This
is done by using the URL or JSP page which ends with .jsp instead of .html.
The JSP engine loads the JSP page from disk and converts it into a servlet content.This conversion is
very simple in which all template text is converted to println( ) statements and all JSP elements are
converted to Java code that implements the corresponding dynamic behavior of the page.
The JSP engine compiles the servlet into an executable class and forwards the original request to a
servlet engine.
A part of the web server called the servlet engine loads the Servlet class and executes it. During
execution, the servlet produces an output in HTML format, which the servlet engine passes to the web
server inside an HTTP response.
The web server forwards the HTTP response to your browser in terms of static HTML content.
Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly
as if it were a static page.
11. Servlets provide a way to generate dynamic documents that is
both easier to write and faster to run.
provide all the powerfull features of JAVA, such as Exception
handling and garbage collection.
Servlet enables easy portability across Web Servers.
Servlet can communicate with different servlet and servers.
Since all web applications are stateless protocol, servlet uses
its own API to maintain session
12. Designing in servlet is difficult and slows down the
application.
Writing complex business logic makes the application difficult
to understand.
You need a Java Runtime Environment on the server to run
servlets. CGI is a completely language independent protocol,
so you can write CGIs in whatever languages you have
available (including Java if you want to).
13. Allows tag based programming. So extensive java knowledge is not
required.
Use nine implicit objects, which we can use directly in our JSP program
Modification done in JSP program will be recognized by underlying server
automatically without reloading of web application.
Allows us to use separate presentation logic (html code) from Java
code(business logic).
Increases readability of code because of tags.
Gives built-in JSP tags and also allows to develop custom JSP tags and to
use third party supplied tags
Takes care of exception handling
Suitable for both java and non java programmer
14. Debugging JSP programs is still a major challenge.You have to
remember that a jsp page will be first converted to a .java file and
then compiled.Thus, any errors will be pointing to line numbers in
the converted .java file and not the .jsp page.For example an error
in the first line of .jsp page may be shown on the 20th line of the
.java file.Tracing back can be somewhat tricky.
Database connectivity is not as easy as it should be. Most of the
servlet engine vendors do not support connection pooling
natively, as of this day. Consequently, one has to write a lot of
custom code to do the job.
It is not an easy task to choose the appropriate servlet engine.
There are numerous syntax related issues with JSP programming.
15. Servlet JSP
Servlet is faster than jsp JSP is slower than Servlet because it first
translate into java code then compile.
In Servlet, if we modify the code then we need
recompilation, reloading, restarting the server.
It means it is time consuming process.
In JSP, if we do any modifications then just we
need to click on refresh button and
recompilation, reloading, restart the server is
not required.
Servlet is a java code. JSP is tag based approach.
In Servlet, there is no such method for running
JavaScript at client side.
In JSP, we can use the client side validations
using running the JavaScript at client side.
To run a Servlet you have to make an entry of
Servlet mapping into the deployment
descriptor file i.e. web.xml file externally.
For running a JSP there is no need to make an
entry of Servlet mapping into the web.xml file
externally, you may or not make an entry for
JSP file as welcome file list.
Coding of Servlet is harden than jsp. Coding of jsp is easier than Servlet because it is
tag based.
16. Servlet JSP
In MVC pattern, Servlet plays a controller role. In MVC pattern, JSP is used for showing output
data i.e. in MVC it is a view.
Servlet accept all protocol request. JSP will accept only http protocol request.
In Servlet, service() method need to override. In JSP no need to override service() method.
In Servlet, by default session management is
not enabled we need to enable explicitly.
In JSP, session management is automatically
enabled.
In Servlet we do not have implicit object. It
means if we want to use an object then we
need to get object explicitly from the servlet.
In JSP, we have implicit object support.
In Servlet, we need to implement business
logic, presentation logic combined.
In JSP, we can separate the business logic from
the presentation logic by uses java Bean
technology.
In Servlet, all package must be imported on top
of the servlet.
In JSP, package imported anywhere top, middle
and bottom.