ch-4 AJ
ch-4 AJ
SEMESTER: 6
PREPARED BY: PROF. NITI KHETRA
JSP
Translation
Compilation
Execution
• 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
• 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
Implicit Objects
Page Exception
Session
Request
Config
Application
Response
out PageContext
• 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
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.
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.
Core Tag I18N & Formatting Tag SQL Tag XML Tag
Library Library Library Library
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.
SQL Tag Library provides tags that allow direct database access within a JSP page. Tags in SQL
library have a prefix <sql> in it.
• 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
intialize
Filter
Destroy
DEPARTMENT OF COMPUTER ENGINEERING,
*Proprietary material of SILVER OAK UNIVERSITY