Unit 3 JSP
Unit 3 JSP
Unit 3 JSP
• In translation phase, container validates the syntactic correctness of JSP page and tag files.
• The JSP container interprets the standard directives and actions, and the custom actions
referencing tag libraries used in this JSP page.
1. Translation of the JSP Page:
1. Translation of the JSP Page:
JSP Life Cycle
2. Compilation of the JSP Page
• The generated java servlet file is compiled into java servlet class
• The translation of java source page to its implementation class can happen at any time
between the deployment of JSP page into the container and processing of the JSP page.
• In the above pictorial description demo_jsp.java is compiled to a class file demo_jsp.class
3.Classloading
• Servlet class that has been loaded from JSP source is now loaded into the container
JSP Life Cycle
4. Instantiation
• In this step the object i.e. the instance of the class is generated.
• The container manages one or more instances of this class in the response to
requests and other events. Typically, a JSP container is built using a servlet
container. A JSP container is an extension of servlet container as both the
container support JSP and servlet.
• A JSPPage interface which is provided by container provides init() and destroy ()
methods.
• There is an interface HttpJSPPage which serves HTTP requests, and it also
contains the service method.
JSP Life Cycle
5. Initialization
• _jspinit() method will initiate the servlet instance which was generated from JSP
and will be invoked by the container in this phase.
• Once the instance gets created, init method will be invoked immediately after
that
• It is only called once during a JSP life cycle, the method for initialization is
declared as shown above
JSP Life Cycle
6. Request processing
• _jspservice() method is invoked by the container for all the requests raised by
the JSP page during its life cycle
• For this phase, it has to go through all the above phases and then only service
method can be invoked.
• It passes request and response objects
• This method cannot be overridden
• The method is shown above: It is responsible for generating of all HTTP methods
i.eGET, POST, etc.
JSP Life Cycle
7. Destroy
• _jspdestroy() method is also invoked by the container
• This method is called when container decides it no longer needs the servlet
instance to service requests.
• When the call to destroy method is made then, the servlet is ready for a garbage
collection
• This is the end of the life cycle.
• We can override jspdestroy() method when we perform any cleanup such as
releasing database connections or closing open files.
Servlets vs JSP Life Cycle
JSP scripting elements
• Java Server Page (JSP) is a technology for controlling the content or appearance of Web pages
through the use of servlets.
• There are five different types of scripting elements
Scripting Element Example
Comment <%-- comment --%>
Directive <%@ directive %>
Declaration <%! declarations %>
Scriptlet <% scriplets %>
Expression <%= expression %>
• The code inside a scriptlet tag goes to the _jspService() method of the generated servlet
for processing the request.
• For each request sent, the _jsp service() method will invoke.
• If multiple scriptlets are present in the code, then they are appended to this service
method in an ordered fashion.
Scriptlets in JSP
• Example 1:-
Scriptlets in JSP
• Example 2:-
Scriptlets in JSP
• Example 3:-
Scriptlets in JSP
• Example 4:-
JSP Expression
• JSP Expression tags, as the name suggests, evaluates expressions.
• They incorporate arithmetic and logical expressions so that they can be evaluated.
• They form an easy means to access the value of a Java variable.
• The Expressions in JSP can be an access point for other expressions and merge that value
in an HTML file.
• They can easily access data stored in applications.
• Expression Tag is used to print out java language expression that is put between the tags.
• An expression tag can hold any java language expression that can be used as an argument
to the out.print() method.
• Syntax of Expression Tag
• <%= Java Expression %>
JSP Expression
JSP Declaration
• We know that at the end a JSP page is translated into Servlet class.
• So when we declare a variable or method in JSP inside Declaration Tag, it means the
declaration is made inside the Servlet class but outside the service(or any other) method.
• You can declare static member, instance variable and methods inside Declarations in JSP
declare java language statements. <%! %> tags contain declarations.
• They can declare classes/instances/inner classes/variables/methods.
• Unlike the other two, this code doesn’t go to _jspService() method.
• This code rather goes to the source file that gets generated outside the _jspService
method.
• Syntax of Declaration Tag :
• <%! declaration %>
JSP Declaration
Difference Between JSP Scriptlet tag and Declaration tag
• This jsp:include action is different from jsp directive. Include directive includes resources at the time of translation,
whereas include action includes resources dynamically at request time.
• Action directives work well for static pages, whereas later works better for dynamic pages. There are two attributes
under include:
www.youtube.com/c/powerupwithpowerpoint
JSP Standard Tag
• Classification of The JSTL Tags
• The JSTL tags can be classified, according to their functions, into the following JSTL tag library
groups that can be used when creating a JSP page −
• Core Tags
• Formatting tags
• SQL tags
• XML tags
• JSTL Functions
www.youtube.com/c/powerupwithpowerpoint
JSP Custom Tag
• Custom tags are user-defined tags. They eliminates the possibility of scriptlet tag and
separates the business logic from the JSP page.
• The same business logic can be used many times by the use of custom tag.
• A tag handler is associated with each tag to implement the operations.
• Therefore, it separates the business logic from JSP and helps avoid the use of scriptlet tags.
• The scriptlet tag embeds java code inside the JSP page itself rendering the page difficult to
understand therefore, it is better to avoid the use of scriptlet.
• Advantages of Custom Tags
• The key advantages of Custom tags are as follows:
1.Eliminates the need of scriptlet tag The custom tags eliminates the need of scriptlet tag
which is considered bad programming approach in JSP.
2.Separation of business logic from JSP The custom tags separate the the business logic
from the JSP page so that it may be easy to maintain.
3.Re-usability The custom tags makes the possibility to reuse the same business logic again
and again.
www.youtube.com/c/powerupwithpowerpoint
JSP Custom Tag
• The javax.servlet.jsp.tagext is the package that
contains classes and interfaces for JSP custom tag
API.
• Here the JspTag is the root interface in the
Custom Tag hierarchy.
• Further tags and Iteration tags extend it. Body
tag extends the above hierarchy and tag support
implements it.
• Further, the body tag supports extends as well as
implements it
• Syntax:-
Thank You
CSE Department
rakeshranjan@cgu-Odisha.co.in
7 0 7 0 2 5 4 4 8 6
www.cgu-odisha.edu.in