Professional Documents
Culture Documents
Unit 4: JSP (Java Server Pages)
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.
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.
Servlet class that has been loaded from JSP source is now loaded into the container
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.
_jspinit() method will initiate the servlet instance which was generated from JSP and will be
invoked by the container in this phase.
_jspservice() method is invoked by the container for all the requests raised by the JSP page
during its life cycle
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
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
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