2.java Server Pages
2.java Server Pages
2.java Server Pages
1) Extension to Servlet
• JSP technology is the extension to Servlet technology.
• User can use all the features of the Servlet in JSP.
• In addition to, user can use implicit objects, predefined tags,
expression language and Custom tags in JSP, that makes JSP
development easy.
• JSP pages allow web designers to work with HTML or XML markup,
while Servlets require a deeper understanding of Java.
• JSP doesn't need additional files such as java class files, web.xml, etc.
• JSPs are compiled into servlets by the JSP engine, which improves
performance compared to interpreting servlets at runtime.
• Many IDEs and tools are available for developing JSPs, which can
make development quicker and easier compared to servlets.
• JSPs are more accessible to debug and troubleshoot than servlets
because they have better error reporting and debugging facilities.
JSP Vs Servlets (Advantages of JSP
over Servlet)
2) Easy to maintain
• JSP can be easily managed because user can easily separate our
business logic with presentation logic.
• In Servlet technology, user mix our business logic with the
presentation logic.
3) Fast Development: No need to recompile and redeploy
• If JSP page is modified, don't need to recompile and redeploy the
project. The Servlet code needs to be updated and recompiled if we
have to change the look and feel of the application.
4) Less code than Servlet
• In JSP, can use many tags such as action tags, JSTL, custom tags,
etc. that reduces the code. Moreover, can use EL, implicit objects,
etc..
Life Cycle of a JSP Page
1. Translation Phase:
• When a user navigates to a page ending with a .jsp
extension in their web browser, the web browser sends an
HTTP request to the webserver.
• The webserver checks if a compiled version of the JSP
page already exists.
• If the JSP page's compiled version does not exist, the file
is sent to the JSP Servlet engine, which converts it into
servlet content (with .java extension). This process is
known as translation.
• The web container automatically translates the JSP page
into a servlet.
Life Cycle of a JSP Page
2. Compilation Phase:
• In case the JSP page was not compiled previously or
at any point in time, then the JSP page is compiled
by the JSP engine.
• In this compilation phase, the Translated .java file of
the servlet is compiled into the Java servlet .class
file.
Life Cycle of a JSP Page
3. Initialization Phase:
• In this Initialization phase: Here, the container will:
• Load the equivalent servlet class.
• Create instance.
• Call the jspInit() method for initializing the servlet
instance.
• This initialization is done only once with the
servlet's init method where database connection,
opening files, and creating lookup tables are done.
Life Cycle of a JSP Page
4. Execution Phase:
• This Execution phase is responsible for all JSP
interactions and logic executions for all requests
until the JSP gets destroyed.
• As the requested JSP page is loaded and initiated,
the JSP engine has to invoke the _jspService()
method as per the request.
• This method is responsible for generating
responses for the associated requests, and
responsible for all HTTP methods.
Life Cycle of a JSP Page
5. Destruction Phase:
• This destruction phase is invoked when the JSP has
to be cleaned up from use by the container.
• The jspDestroy() method is invoked.
• User can incorporate and write cleanup codes inside
this method for releasing: resources, database
connections or closing any file.
Life Cycle of a JSP Page
Anatomy of JSP Page
• JSP page is a simple web page which contains the JSP elements
and template text.
• The template text can be scripting code such as Html, Xml or a
simple plain text.
• Template data provides the static aspects of the page.
• Various Jsp elements can be action tags, custom tags, JSTL
library elements.
• These JSP elements are responsible for generating dynamic
contents.
• When JSP request gets processed template text and JSP elements
are merged together and sent to the browser as response.
Anatomy of JSP Page
JSP Processing
<jsp:root
</jsp:declaration>
xmlns:jsp="http://java.sun.com/JSP/Page"
version="2.1"> </jsp:root>
<jsp:directive.page contentType="text/html"/> <jsp:scriptlet>
<html>
<![CDATA[
<head>
out.println(description);
<title> JSP Document using Scripting </title>
out.println("<br/>");
</head>
<body>
int result = sum( 2, 3);
<jsp:declaration> ]]>
String description = "description variable is a </jsp:scriptlet>
instance variable." ; <jsp:expression>
public int sum(int argument1, int argument2)
"Result of Sum :: " +result
{
</jsp:expression>
int result = argument1+ argument2;
</body>
return result;
}
</html>
JSP Documents
Lets write an example (ScriptingExample.jspx) to use scripting tags
(expression, declaration and scriplet ) in JSP document.
Output:
JSP Elements
• JSP Directives
• Scriptlets
• JSP Processing