0% found this document useful (0 votes)
21 views26 pages

ch-4 AJ

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

ch-4 AJ

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

ADVANC JAVA

UNIT 4: JAVA SERVER PAGES

SEMESTER: 6
PREPARED BY: PROF. NITI KHETRA
JSP

• Java Server Pages(JSP) is java server Technology, Which is used


to display the dynamic content on a web page, JSP pages are
saved with “.jsp” extension. The information generated and
displayed on web page at run time is class dynamic content . The
dynamic content included current date, visitor count, and tables
or files that are stored on the server. JSP page uses java
programming language to display the dynamic content on a web
page.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
BENEFITS OF JSP

• JSP helps to access server data efficiently and with a greater


speed.
• Separation of Content Generation from presentation
• Emphasizing reusable Components.
• Simplified Page Development.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP ARCHITECTURE:

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
STEPS

1. User requesting a JSP page through internet via web browser.


2. The JSP request is sent to the Web Server.
3. Web server accepts the requested .jsp file and passes the JSP file to the JSP Servlet Engine.
4. If the JSP file has been called the first time then the JSP file is parsed otherwise servlet is
instantiated.
5. The next step is to generate a servlet from the JSP file. In that servlet file, all the HTML code is
converted in println statements.
6. Now, The servlet source code file is compiled into a class file (bytecode file).
7. The servlet is instantiated by calling the init and service methods of the servlet’s life cycle.
8. Now, the generated servlet output is sent via the Internet form web server to user's web browser.
9. Now in last step, HTML results are displayed on the user's web browser.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP LIFE CYCLE:
• The lifecycle of JSP is controlled by three methods which are automatically called when a JSP is requested
and when the JSP terminates normally.
• These are:
• jspInit () , _jspService() , jspDestroy().
• jspInit() method is identical to the init() method in a Java Servlet and in applet.
• It is called first when the JSP is requested and is used to initialize objects and variables that are used
throughout the life of the JSP.
• _jspService() method is automatically called and retrieves a connection to HTTP.
• It will call doGet or doPost() method of servlet created.
• jspDestroy() method is identical to the destroy() method in Servlet.
• The destroy() method is automatically called when the JSP terminates normally.
• It isn’t called by JSP when it terminates abruptly.
• It is used for cleanup where resources used during the execution of the JSP ate released, such as
disconnecting form database.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP Page

Translation

Compilation

Servlet generated for JSP

Execution

DEPARTMENT OF COMPUTER ENGINEERING,


Output of JSP
*Proprietary material of SILVER OAK UNIVERSITY
JSP SCRIPTING ELEMENTS

• There is more than one type of JSP “tag,” depending on what you
want done with the Java
• <%= expression %>
• The expression is evaluated and the result is inserted into the HTML page
• <% code %>
• The code is inserted into the servlet's service method
• This construction is called a scriptlet
• <%! declarations %>
• The declarations are inserted into the servlet class, not into a method

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
SCRIPTLETS

• Scriptlets are enclosed in <% ... %> tags


• Scriptlets do not produce a value that is inserted directly into the
HTML (as is done with <%= ... %>)
• Scriptlets are Java code that may write into the HTML
• Example:
<% String queryData = request.getQueryString();

out.println("Attached GET data: " + queryData); %>
• Scriptlets are inserted into the servlet exactly as written,
and are not compiled until the entire servlet is compiled
• Example:
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
DECLARATIONS

• Use <%! ... %> for declarations to be added to your servlet class,
not to any particular method
• Caution: Servlets are multithreaded, so nonlocal variables must be handled
with extreme care
• If declared with <% ... %>, variables are local and OK
• Data can also safely be put in the request or session objects
• Example:
<%! private int accessCount = 0; %>
Accesses to page since server reboot:
<%= ++accessCount %>
• You can use <%! ... %> to declare methods as easily as to declare
variables
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
DIRECTIVES

• Directives affect the servlet class itself


• A directive has the form:
<%@ directive attribute="value" %>
or
<%@ directive attribute1="value1"
attribute2="value2"
...
attributeN="valueN" %>
• The most useful directive is page, which lets you import
packages
• Example: <%@ page import="java.util.*" %>

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
THE INCLUDE DIRECTIVE

• The include directive inserts another file into the file


being parsed
• The included file is treated as just more JSP, hence it can
include static HTML, scripting elements, actions, and directives
• Syntax: <%@ include file="URL " %>
• The URL is treated as relative to the JSP page
• If the URL begins with a slash, it is treated as relative to the
home directory of the Web server
• The include directive is especially useful for inserting
things like navigation bars

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
ACTIONS

• Actions are XML-syntax tags used to control the servlet


engine
• <jsp:include page="URL " flush="true" />
• Inserts the indicated relative URL at execution time (not at compile
time, like the include directive does)
• This is great for rapidly changing data
• <jsp:forward page="URL" />
<jsp:forward page="<%= JavaExpression %>" />
• Jump to the (static) URL or the (dynamically computed)
JavaExpression resulting in a URL

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP IMPLICIT OBJECTS

Implicit Objects

Scope Servlet Object Error


Input and Output
Communication Object Object
Object
Object

Page Exception
Session
Request
Config
Application
Response

out PageContext

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP EXPRESSION LANGUAGE

• Jsp is used to display the dynamic content on a web page. The dynamic
content generated using JSP language can not be formatted
• Ex. JSP scriptlets and expressions can not be used in HTML tags such as
<table>
• Expression Language (EL) is a primary feature of the JSP Technology
• JSP Standard Tag Library expert group and jsp2.0 expert group at the
java community Process have developed the JSP Expression Language.
• Syntax
• $ { EL expression }
• $ indicates beginning of an expression
• { is an opening delimiter
• El expression specifies Expression
• } is a closing delimiter
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
CUSTOM TAGS
• Custom tag allows java developers to embed java codes in HTML document.
Custom tags are similar to JavaBeans. Which are reusable components.
Custom tags are simple to use as they follow the standard HTML tag format.
Custom tags are also similar to XML style tags because they allow developers
to provide a logical name for a complex operation.
• Java developers use Tag Libraries to create tags. Tag Libraries are set of
custom tags grouped according to functionality of implementation.
• Some example of custom Tags are:
• JSP Stadard Tag Library(JSTL)
• Provides two sets of tags. One that uses Expression Langauge(EL) and the
other that does not use EL
• JRun Tag Library
• Deprecated tag library but forms that base of JSTL
• Struts TagLib
• Provides tags that allow developers to impalement (MVC) design pattern.
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
COMPONENTS OT TAG LIBRARY

Defines the behavior of the tag and is


executed when a JSP engine encounters a
TAG LIBRARY custom tag

Tag Handler
Provides the JSP engine with information
regarding the appropriate Tag handle for a
custom tag.
Tag Library Descriptor
Provides information regarding the Tag
Application Deployment Library Descriptor for a particular Tag it also
notifies the Tag library files that are being
Descriptor
used.

JSP Use the custom tag and includes tag libraries


to acquire the desired result.
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
THE BASIC TAG LIFE CYCLE
Instantiate Tag
Handler setBodyContent()

EVAL_BODY_BUFFERED
doStartTag() doInitBody()

doAfterBody()
Return
value
EVAL_BODY_AGAIN
Return
EVAL_BODY_INCLUDE
value
SKIP_BODY
SKIP_BODY
doEndTag()

EVAL_PAGE
SKIP_PAGE
De-reference tag
release()
handler
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
TAG FILES

• One of the advantages of using a custom tag is its reusability and its
ability to modify the JSP content. Custom tags grant use of standard
HTML format tags. Tag files provided a much-managed way to build
a template and reuse it. Custom tag can be created using either the
Tagext package or the tag file.
• JSP specification allows users to add custom tag implementation.
Either using a java class file or using a regular text file with JSP and
XML syntax. This regular text file is termed as a “Tag File”.
• Tag files make creating custom tags very simple
• A Tag file is a text file with a ‘.tag’ extension.
• The tag file has to be stored in the ‘WEB-INF’ directory of the web
application.
• The tag file acts like a tag handler file.
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
JSP STANDARD TAG LIBRARIES

• JSP Standard Tag Library (JSTL has standard set of tag to write and
develop JSP pages. JSTL provides the user with a script-free environment .
The user can write code using tags instead of java code and scriptlets.
JSTL has tags such as iterators and conditional tags, internationalization
tags, and SQL tags. Iterators and conditional tag are used to handle flow
controls.
• Internationalization tags provide users with the required language and
formatting. SQL tags are used in JSTL for accessing the database and
updating tables.
• Jstl provides four main tag libraries: Core Tag Library,
Internationalization(I18N) and formatting Tag Library, SQL Tag library and
XML Tag Library.

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
JSP Standard Tag Library (JSTL)

Core Tag I18N & Formatting Tag SQL Tag XML Tag
Library Library Library Library

JSTL Tag libraries are available in two versions.


1) uses an Expression Language (EL)
An EL allows the user to access application data JavaBeans components
2) does not use any Expression Language
Provide the users with flexibility of standard tabs without having element EL
like HTML tags
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
Core Tag Library
General-Purpose
Tags set
Core tag library has
general-purpose tags that are remove
used to manipulate the scoped
variables created with a JSP
page. out

Decision-Making
Tags
Decision-making tags in Core tag library if
are used to do conditional processing of
code in a JSP page.
choose
Iteration
Tags
Iteration tags in core tag library are used forEach
to iterate over a collection of object
multiple times.
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
forTokens
UNIVERSITY
I18N & Formatting
Tag Library
Internationalization (I18N) and formatting Tag Library helps in creating internationalized web applications that
standardize formatted numeric and data-time output. It is support multiple Languages.

setLocale bundle SetBundle message

<fmt:setLocale> <fmt:setBundle> <fmt:message>


<fmt:bundle> Used to give output
Used to override the Creates an I18N Create an I18N
client-specified settings localization context and values from resource
localization context and bundle.
for a page. loads its resource bundle store it in the scoped
setLocale is an empty variables . It is empty tag. <fmt:param> is sub
into the context. tag .
tag. Resource bundle is It can be an empty as
specified in the basename well as a container tag.
attribute.
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
JSTL SQL Tag Library

SQL Tag Library provides tags that allow direct database access within a JSP page. Tags in SQL
library have a prefix <sql> in it.

setDataSource query update transaction param


<sql:setDataSource> <sql:query> <sql:update> <sql:transaction> <sql:param>
use to set a datasource Searches the Executes the User to establish a Sets values for the
for the database. This database and INSERT, UPDATE Transaction context parameters in SQL
tag is empty tag. It is returns the and DELETE statements.
allows user to set data result set statement. It act as a sub tag
source information for containing rows Zero is returned if for <sql:query> and
the database. of data no rows are <sql:update>
affected by insert,
update or delete

DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK


UNIVERSITY
FILTERS

• Filters were introduced as a component type in java servlet specification version 2.3
• Filter are web service components that add functionality to the request and response
processing of web application. Filters can access incoming request from the user before the
servlet processes the request. Filters can also access the outgoing response from the web
resource before sending it to the user.
• Filter can configured on user’s request and on the response of the web resource. There can be
multiple filters between user and the servlet. They are chained in series of filters.
• Authorize request
• Request headers and modify data
• Modify response headers and data

request Web resource


USER Filters (servlet or jsp)
responce
DEPARTMENT OF COMPUTER ENGINEERING, *Proprietary material of SILVER OAK
UNIVERSITY
instantiate

intialize

Filter

Destroy
DEPARTMENT OF COMPUTER ENGINEERING,
*Proprietary material of SILVER OAK UNIVERSITY

You might also like