0% found this document useful (0 votes)
35 views

31-Module 7 - JSP - JSP Tags and Expressions-01-Apr-2019Reference Material I - JSP

JSP is a server-side technology used for developing dynamic web pages. JSP code combines HTML tags and Java code to generate dynamic web content. JSP pages are compiled into Java servlets that generate responses. The document discusses the differences between JSP and servlets, the JSP page lifecycle including compilation, execution and cleanup, as well as JSP elements like directives, scripting tags, implicit objects and actions.

Uploaded by

Tanmay sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

31-Module 7 - JSP - JSP Tags and Expressions-01-Apr-2019Reference Material I - JSP

JSP is a server-side technology used for developing dynamic web pages. JSP code combines HTML tags and Java code to generate dynamic web content. JSP pages are compiled into Java servlets that generate responses. The document discusses the differences between JSP and servlets, the JSP page lifecycle including compilation, execution and cleanup, as well as JSP elements like directives, scripting tags, implicit objects and actions.

Uploaded by

Tanmay sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

JSP (java Server

Pages)
Servlet vs JSP
Like JSP, Servlets are also used for generating dynamic webpages.
• Servlets 
• Servlets are Java programs which supports HTML tags too.
• Generally used for developing business layer of an enterprise
application.
• Servlets are created and maintained by Java developers.

• JSP –
• JSP program is a HTML code which supports java statements too.
• Used for developing  presentation layer of an enterprise application
• Frequently used for designing websites and used for web
designers.
Overview
• Java Server Pages (JSP) is a server side technology for
developing dynamic web pages.
• This is mainly used for implementing presentation layer (GUI
Part) of an application.
• A complete JSP code is more like a HTML with bits of java
code in it.
• JSP is an extension of servlets and every JSP page first gets
converted into servlet by JSP container before processing the
client’s request.
Dynamic web pages
• Dynamic webpages can have two types of contents – static &
dynamic content.

• The static contents can have text-based formats such as


HTML,  XML etc and the dynamic contents are generated by
JSP elements.
An Example

• All JSP programs are stored as a .jsp files. Above is a simple JSP
code, MyJSP.jsp which prints Hello, Sample JSP code. As
discussed JSP is used for creating dynamic webpages.
Life Cycle of JSP page
• Compilation
• Initialization
• Execution
• Cleanup
JSP Compilation:
• When a browser asks for a JSP, the JSP engine first checks to
see whether it needs to compile the page.
• If the page has never been compiled, or if the JSP has been
modified since it was last compiled, the JSP engine compiles
the page.
• The compilation process involves three steps:
• Parsing the JSP.(.jsp to .java)
• Turning the JSP into a servlet.
• Compiling the servlet. Page Translation (.java to .class)
JSP Initialization:
• When a container loads a JSP it invokes the jspInit() method
before servicing any requests.
• If you need to perform JSP-specific initialization, override the
jspInit() method:
public void jspInit()
{
// Initialization code...
}
• Typically initialization is performed only once and as with the
servlet init method, you generally initialize database
connections, open files, and create lookup tables in the
jspInit method.
JSP Execution
• This phase of the JSP life cycle represents all interactions with
requests until the JSP is destroyed.
• Whenever a browser requests a JSP and the page has been loaded
and initialized, the JSP engine invokes the _jspService() method in
the JSP.
• The _jspService() method takes an HttpServletRequest and an
HttpServletResponse as its parameters as follows:
void _jspService(HttpServletRequest request,
HttpServletResponse response)
{ // Service handling code... }
• The _jspService() method of a JSP is invoked once per a request
and is responsible for generating the response for that request and
this method is also responsible for generating responses to all
seven of the HTTP methods ie. GET, POST, DELETE etc.
JSP Cleanup:
• The destruction phase of the JSP life cycle represents when a
JSP is being removed from use by a container.
• The jspDestroy() method is the JSP equivalent of the destroy
method for servlets.
• Override jspDestroy when you need to perform any cleanup,
such as releasing database connections or closing open files.
• The jspDestroy() method has the following form:
public void jspDestroy()
{
// Your cleanup code goes here.
}
JSP elements
• Directives of the form <%@ ... %>
• Scripting elements
• Expressions of the form <%= expr %>
• Scriptlets of the form <% code %>
• Declarations of the form <%! code %>
• JSP Comments <%-- ... --%>
• Implicit objects.
• Standard actions
• Example: <jsp:useBean> ... </jsp:useBean>
1. JSP Directives
• Directives control the processing of an entire JSP page. It gives
directions to the server regarding processing of a page.

• Syntax of Directives:

<%@ directive name [attribute name=“value” attribute name=“value” ...]%>

There are three types of Directives in JSP:


1) Page Directive
2) Include Directive
3) TagLib Directive
1)Page Directive
There are several attributes, which are used along with Page
Directives and these are :
• import
• session
• isErrorPage
• errorPage
• ContentType
• isThreadSafe
• extends
• info
• language
• autoflush
• buffer
Page Directive: import
• This attribute is used to import packages. While doing coding
you may need to include more than one packages, In such
scenarios this page directive’s attribute is very useful as it
allows you to mention more than one packages at the same
place separated by commas (,).
Page Directive: session
• Generally while building a user interactive JSP application, we
make sure to give access to the user to get hold of his/her
personal data till the session is active.
• Consider an example of logging in into your bank account, we
can access all of your data till we signout (or session expires).
In order to maintain session for a page the session attribute
should be true.
• This attribute is to handle HTTP sessions for JSP pages.
• It can have two values:
true or false.
Page Directive: contentType
• This attribute is used to set the content type of a JSP page.
Page Directive: extends
2) Include Directive
• Include directive is used to copy the content of one JSP page
to another. It’s like including the code of one file into another.
3) Taglib Directive
• This directive basically allows user to use Custom tags in JSP.
• Taglib directive helps you to declare custom tags in JSP page.
• Syntax of Taglib Directive:
<%@taglib uri ="taglibURI" prefix="tag prefix"%>
• Where URI is uniform resource locator, which is used to
identify the location of custom tag and tag prefix is a string
which can identify the custom tag in the location identified by
uri.
2. JSP Scripting Elements : Expression
• For an expression scripting element like <%= expr %>, expr is
evaluated and the result is converted to a string and placed
into the JSP's servlet output stream.
• In a Java servlet this would be equivalent to
PrintWriter out = response.getWriter();
...
out.print(expr);
Example
Example
JSP Scriptlet
• A scriptlet can contain any number of JAVA language
statements, variable or method declarations, or expressions
that are valid in the page scripting language.
• Syntax of Scriptlet:
<% code fragment %>
Example of JSP scriptlet tag that prints the user
name
JSP Declaration tag
• Declaration tag is a block of java code for declaring class wide
variables, methods and classes.
• Whatever placed inside these tags gets initialized during JSP
initialization phase and has class scope.
• JSP container keeps this code outside of the service method
(_jspService()) to make them class level variables and
methods.
• Declaration tag can be used for defining class level variables,
methods
• Syntax of declaration tag:
<%!  Declaration %>
Example 1: Variables declaration
Example 2: Methods declaration
JSP Comment tag
• JSP comment marks text or statements that the JSP container
should ignore. A JSP comment is useful when you want to hide
or "comment out" part of your JSP page.
3. Implicit objects
JSP Client Request
When a browser
requests for a web
page, it sends lot of
information to the
web server which
can not be read
directly because this
information travel
as a part of header
of HTTP request.
JSP Server Response
• A HTTP server response header looks as follows:
HTTP Status codes
HTTP Status Code
JSP Actions
• JSP actions use constructs in XML syntax to control the
behavior of the servlet engine.
• we can dynamically insert a file, reuse JavaBeans components,
forward the user to another page, or generate HTML for the
Java plugin.
• There is only one syntax for the Action element, as it conforms
to the XML standard:

<jsp:action_name attribute="value" />


JSP Actions
• Action elements are basically predefined functions and there
are following JSP actions available:
Control Flow
Statements
Decision-Making Statements
• The if...else block starts out like an ordinary Scriptlet, but the
Scriptlet is closed at each line with HTML text included
between Scriptlet tags.
Advantages of JSP
• vs. Active Server Pages (ASP): The advantages of JSP are twofold. First,
the dynamic part is written in Java, not Visual Basic or other MS
specific language, so it is more powerful and easier to use. Second, it is
portable to other operating systems and non-Microsoft Web servers.
• vs. Pure Servlets: It is more convenient to write (and to modify!)
regular HTML than to have plenty of println statements that generate
the HTML.
• vs. Server-Side Includes (SSI): SSI is really only intended for simple
inclusions, not for "real" programs that use form data, make database
connections, and the like.
• vs. JavaScript: JavaScript can generate HTML dynamically on the client
but can hardly interact with the web server to perform complex tasks
like database access and image processing etc.
• vs. Static HTML: Regular HTML, of course, cannot contain dynamic
information

You might also like