2.java Server Pages

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Java Server Pages

1. Introduction To Java Server Pages


2. JSP Vs Servlets
3. Life Cycle Of A JSP Page
4. Comments
5. JSP Document
6. JSP Elements
7. JSP GUI Example.
Introduction To Java Server Pages

• JSP stands for Jakarta Server Pages( (JSP; formerly


JavaServer Pages)).
• JSP is used for creating dynamic and platform-independent
web applications. It consists of both HTML tags and JSP
tags.
• It is an advanced version / Extension of Servlet Technology.
• It is a Server-side scripting/ technology means the JSP code
is processed on the web server rather than the client
machine.
• JSP tags are used to insert JAVA code into HTML pages by
making use of special JSP tags, most of which start with <%
and end with %>.
Introduction To Java Server Pages

• JSP tags are used to insert JAVA code into HTML


pages by making use of special JSP tags, most of
which start with <% and end with %>.
• JSP expressions are used to insert dynamic content
into the page and begin with <%= and end with %>.
• JSP tags can be used for a variety of purposes, such as,
• Retrieving information from a database
• Registering user preferences
• Accessing JavaBeans components,
• Passing control between pages, and
• Sharing information between requests, pages etc.
Introduction To Java Server Pages

• JSP can use JavaBeans to store and retrieve data.


• It requires a Java development environment and a
Java Servlet Container such as Apache Tomcat or
GlassFish or Jetty.
JSP Vs Servlets (Advantages of JSP over Servlet)

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

• A JSP page is converted into Servlet in order to service requests.


• The translation of a JSP page to a Servlet is called Lifecycle of
JSP.
• JSP Lifecycle is exactly same as the Servlet Lifecycle, with one
additional first step, which is, translation of JSP code to Servlet
code.
• The life cycle of a JSP page can be divided into the following
phase:
• Translation Phase
• Compilation Phase
• Initialization Phase
• Execution Phase
• Destruction(Cleanup) Phase
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 pages can be processed using JSP Container


only.
Steps that need to be followed while processing the
request for JSP page:
• Client makes a request for required JSP page to server.
• The server must have JSP container so that JSP request
can be processed.
JSP Processing

Steps that need to be followed while processing the


request for JSP page:
• For instance: let the client makes request for xyz.jsp page.
• On receiving this request the JSP container searches and
then reads the desired JSP page.
• Then this JSP page is converted to corresponding
servlet.
• Basically any JSP page is a combination of template text
and JSP elements.
• Every template text is converted to corresponding
println statement.
JSP Processing

Steps that need to be followed while processing the


request for JSP page:
• Every JSP element is converted into corresponding Java
code.
• This phase is called Translation phase, output of it is a
servlet. For instance:xyz.jsp is converted to xyzservlet.java
• This servlet is compiled to generate servlet class file.
Using this class, response is generated.
• This phase is called request processing phase.
• The Jsp container thus executes servlet class file.
• A requested page is then returned to client as response.
JSP Processing
Figure: JSP Processing
JSP Documents

• JSP can be written in 2 formats:


• JSP Page - A JSP page written in an HTML format
with JSP elements expressed as <% ... %> tags.
• JSP Document - A JSP written in an XML format
with JSP elements expressed as XML elements.
• A JSP document is JSP written in XML format and
therefore must comply with the XML standard.
• JSP elements included in the JSP document must comply
with the XML syntax.
JSP Documents

• JSP document must be well formed which means that each


start tag must have a corresponding end tag and that the
document must have only one root element.
• A JSP Document should be saved with the .jspx extension.
• A JSP Document must have a root element called "root"
with a "version" attribute like: <jsp:root version="2.1"
xmlns:jsp="http://java.sun.com/JSP/Page">
• By default content type of JSP document is text/xml so to
display the document as html, user need to change the
content type to text/html.
JSP Documents

JSP Document Syntax:


Declaration:
• In standard JSP page , declaration tag is used as <
%! . . %>.
• But in JSP document , declaration tag is expressed
as <jsp:declaration> . . . </jsp:declaration>
•k
JSP Documents

JSP Document Syntax:


Scriptlets:
• In standard JSP page , scriptlet tag is used as <% . .
%>
• In JSP document , scriptlets tag is expressed as
<jsp:scriptlet> . . . </jsp:scriptlet>
JSP Documents

JSP Document Syntax:


Expressions:
• In standard JSP page , expression tag is used as <
%= . . %>
• But in JSP document , expressions tag is expressed
as <jsp:expression> . . . </jsp:expression>
JSP Documents

JSP Document Syntax:


Comments:
• In standard JSP page , comments are defined as <
%-- . . --%>.
• But in JSP document , expressions tag is expressed
as <!-- . . . -->
JSP Documents

JSP Document Syntax:


Directives :
• Include directive will be expressed as
<jsp:directive.include .. /> instead of <%@include . . .
%>
• Page Directive will be expressed as
<jsp:directive.page .. /> instead of <%@page ... %>
• Taglib directive will be expressed as
<jsp:directive.taglib .. /> instead of <%@taglib ... %>
JSP Documents

JSP Document Syntax:


EL Operators:
• Operators need to be used as below –
• Ø < will be written as lt
• Ø >will be written as gt
• Ø <= will be written as Le
• Ø >= will be written as Ge
• Ø != will be written as Ne
JSP Documents
Lets create a HelloWorld.jspx JSP document which will print “hello world” on
screen:
• <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
• <html>
• <head>
• <title> Hello World JSP Document </title>
• </head>
• <body>
• <h4>
• Hello World JSP Document
• </h4>
• </body>
• </html>
• </jsp:root>
JSP Documents

Access HelloWorld.jspx document using


http://localhost:8080/jsp-tutorial/HelloWorld.jspx:
JSP Documents
Access HelloWorld.jspx document using
http://localhost:8080/jsp-tutorial/HelloWorld.jspx:
Since we did not change the content type of JSP document , the whole
content is displayed as xml. Now change the content type (highlighted
below )
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<html>
<head>
<title> Hello World JSP Document </title>
</head>
<jsp:directive.page contentType="text/html"/>
<body>
<h4>
Hello World JSP Document
</h4>
</body>
</html>
</jsp:root>
JSP Documents
JSP Documents
Lets write an example (ScriptingExample.jspx) to use scripting tags (expression, declaration and scriplet )
in JSP document.

<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

You might also like