Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Unit 4: JSP (Java Server Pages)

Introduction to JSP
 Java Server Pages (JSP) is a technology which is used to develop web pages by
inserting Java code into the HTML pages by making special JSP tags. The JSP tags
which allow java code to be included into it are <% —-java code—-%>.
 It can consist of either HTML or XML (combination of both is also possible) with
JSP actions and commands.
 It can be used as HTML page, which can be used in forms and registration pages with
the dynamic content into it.
 Dynamic content includes some fields like dropdown, checkboxes, etc. whose value
will be fetched from the database.
 This can also be used to access JavaBeans objects.
 We can share information across pages using request and response objects.
 JSP can be used for separation of the view layer with the business logic in the web
application.

Advantages of JSP over Servlet

JSP Life Cycle


JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP Page needs to be
converted into servlet first in order to process the service requests. The Life Cycle starts with
the creation of JSP and ends with the disintegration of that.
When the browser asks for a JSP, JSP engine first checks whether it needs to compile the
page. If the JSP is last compiled or the recent modification is done in JSP, then the JSP
engine compiles the page.
Compilation process of JSP page involves three steps:
Parsing of JSP
Turning JSP into servlet
Compiling the servlet
Following steps explain the JSP life cycle:

 Translation of JSP page

A Java servlet file is generated from a JSP source file. This is the first step of JSP life cycle.
In translation phase, container validates the syntactic correctness of JSP page and tag files.

 Compilation of JSP page(Compilation of JSP page into _jsp.java)

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 1


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.

 Classloading (_jsp.java is converted to class file _jsp.class)

Servlet class that has been loaded from JSP source is now loaded into the container

 Instantiation(Object of generated servlet is created)

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.

 Initialisation(_jspinit() method is invoked by container)

_jspinit() method will initiate the servlet instance which was generated from JSP and will be
invoked by the container in this phase.

 Request Processing(_jspservice() method is invoked by the container)

_jspservice() method is invoked by the container for all the requests raised by the JSP page
during its life cycle

 Destroy (_jspDestroy() method invoked by the container)

_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.

JSP Implicit Objects

Implicit Objects
JSP implicit objects are created during the translation phase of JSP to the servlet. These
objects can be directly used in scriplets that goes in the service method. They are created by
the container automatically, and they can be accessed using objects.
1) Out:
 Out is one of the implicit objects to write the data to the buffer and send output to the
client in response
PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 2
 Out object allows us to access the servlet’s output stream
 Out is object of javax.servlet.jsp.jspWriter class
2) Request
The request object is an instance of java.servlet.http.HttpServletRequest and it is one of the
argument of service method
It will be created by container for every request.
It will be used to request the information like parameter, header information , server name,
etc.
It uses getParameter() to access the request parameter.
3) Response
“Response” is an instance of class which implements HttpServletResponse interface
Container generates this object and passes to _jspservice() method as parameter
“Response object” will be created by the container for each request.
It represents the response that can be given to the client
The response implicit object is used to content type, add cookie and redirect to response page
4) Config
“Config” is of the type java.servlet.servletConfig
It is created by the container for each jsp page
It is used to get the initialization parameter in web.xml
5) Application
Application object (code line 10) is an instance of javax.servlet.ServletContext and it is used
to get the context information and attributes in JSP.
Application object is created by container one per application, when the application gets
deployed.
Servletcontext object contains a set of methods which are used to interact with the servlet
container.We can find information about the servlet container
6) Session
The session is holding “httpsession” object(code line 10).
Session object is used to get, set and remove attributes to session scope and also used to get
session information
7) PageContext
This object is of the type of pagecontext.
It is used to get, set and remove the attributes from a particular scope

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 3


Scopes are of 4 types:
 Page
 Request
 Session
 Application

Explicit Objects

JSP Directives
JSP directives are the messages to JSP container. They provide global information about an
entire JSP page. It also gives special instruction to a container for translation of JSP to servlet
code.
Syntax of Directive:
<%@ directive attribute="" %>
There are three types of directives:
 Page directive
<%@ page…%>
It provides attributes that get applied to entire JSP page.
It defines page dependent attributes, such as scripting language, error page, and buffering
requirements.
It is used to provide instructions to a container that pertains to current JSP page.
Following are its list of attributes associated with page directive:
Language, Extends, Import, contentType, info, session, isThreadSafe, autoflush, buffer,
IsErrorPage, pageEncoding, errorPage, isELIgonored
 Include directive
JSP “include directive” is used to include one file to the another file
This included file can be HTML, JSP, text files, etc.
It is also useful in creating templates with the user views and break the pages into
header&footer and sidebar actions.
It includes file during translation phase
Syntax of include directive:
<%@ include….%>
 Taglib directive

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 4


JSP taglib directive is used to define the tag library with “taglib” as the prefix, which we can
use in JSP.
JSP taglib directive is used in the JSP pages using the JSP standard tag libraries
It uses a set of custom tags, identifies the location of the library and provides means of
identifying custom tags in JSP page.

Bean class/ Action Tags / Action Elements


JSP actions use the construct in XML syntax to control the behavior of the servlet engine. We
can dynamically insert a file, reuse the beans components, forward user to another page, etc.
through JSP Actions like include and forward. Unlike directives, actions are re-evaluated
each time the page is accessed.
Syntax:
<jsp:action_name attribute="value" />
There are 11 types of Standard Action Tags in JSP. Here is the list of Standard Action tags in
JSP:
jsp:useBean jsp:forward jsp:text
jsp:include jsp:plugin jsp:param
jsp:setProperty jsp:attribute jsp:output
jsp:getProperty jsp:body
1) jsp:useBean
This action name is used when we want to use beans in the JSP page.
With this tag, we can easily invoke a bean.
Syntax of jsp: UseBean:
<jsp:useBean id="" class="" />
Here it specifies the identifier for this bean and class is full path of the bean class
2) jsp:include
It also used to insert a jsp file into another file, just like including Directives.
It is added during request processing phase
Syntax of jsp:include
<jsp:include page="page URL" flush="true/false">
3) jsp:setProperty
This property of standard actions in JSP is used to set the property of the bean.
We need to define a bean before setting the property
Syntax:

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 5


<jsp:setproperty name="" property="" >
Here, the name defines the bean whose property is set and property which we want to set.
4) jsp:getProperty
This property is used to get the property of the bean.
It converts into a string and finally inserts into the output.
Syntax:
<jsp:getAttribute name="" property="" >
Here, the name of the bean from which the property has to be retrieved and bean should be
defined.
5) jsp:forward
It is used to forward the request to another jsp or any static page.
Here the request can be forwarded with no parameters or with parameters.
Syntax:
<jsp:forward page="value">
Here value represents where the request has to be forwarded.
6) jsp:plugin
It is used to introduce Java components into jsp, i.e., the java components can be either an
applet or bean.
It detects the browser and adds <object> or <embed> JSP tags into the file
Syntax:
<jsp:plugin type="applet/bean" code="objectcode" codebase="objectcodebase">
Here the type specifies either an object or a bean
7) jsp:param
This is child object of the plugin object described above
It must contain one or more actions to provide additional parameters.
Syntax:
<jsp:params>
<jsp:param name="val" value="val"/ >
</jsp:params>
8) jsp:body

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 6


This tag is used to define the XML dynamically i.e., the Elements can generate during request
time than compilation time.
It actually defines the XML, which is generated dynamically element body.
Syntax:
<jsp:body></jsp:body>
Here we write XML body tag within this tags
9) jsp:attribute
This tag is used to define the XML dynamically i.e. the elements can be generated during
request time than compilation time
It actually defines the attribute of XML which will be generated dynamically.
Syntax:
<jsp:attribute></jsp:attribute>
Here we write attribute tag of XML.
10) jsp:text
It is used to template text in JSP pages.
Its body does not contain any other elements, and it contains only text and EL expressions.
Syntax:
<jsp:text>template text</jsp:text>
Here template text refers to only template text (which can be any generic text which needs to
be printed on jsp ) or any EL expression.
11) jsp:output
It specifies the XML declaration or the DOCTYPE declaration of jsp
The XML declaration and DOCTYPE are declared by the output
Syntax:
<jsp:output doctype-root-element="" doctype-system="">
Here, doctype-root-element indicates the root element of XML document in DOCTYPE.

Servlet JSP
Servlets run faster than JSP. JSP runs slower than servlet as it takes time
to compile the program and convert into
servlets.
It is hard to write code in servlet. It’s easier to code in JSP compared to

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 7


servlets.
In MVC architecture, servlet works as a In MVC architecture, JSP works as a view
controller. for displaying output.
Servlet is a java code. JSP is a HTML-based code.
It can accept all protocol requests, including It can only accept HTTP requests.
HTTP.
You can override the service() method. In JSP, you can’t override the service()
method.
In Servlet, you have to implement both In JSP, business logic is split from
business logic and presentation logic in the presentation logic using JavaBeans.
single file.

PROF. BHUSHAN L RATHI (GHRIBM,JALGAON) 8

You might also like