Introduction to JSP
JSP technology is used to create web application
just like Servlet technology.
It can be thought of as an extension to Servlet
because it provides more functionality than
servlet such as expression language, JSTL, etc.
A JSP page consists of HTML tags and JSP tags.
The JSP pages are easier to maintain than
Servlet because we can separate designing and
development.
It provides some additional features such as
Expression Language, Custom Tags, etc.
Advantages of JSP over Servlet
Extension to Servlet
Easy to maintain
Fast Development: No need to recompile and
redeploy
Less code than Servlet
Translation of JSP Page
Compilation of JSP Page
Classloading (the classloader loads class file)
Instantiation (Object of the Generated Servlet is
created).
Initialization ( the container invokes jspInit() method).
Request processing ( the container invokes
_jspService() method).
Destroy ( the container invokes jspDestroy()
method).
jspInit(), _jspService() and jspDestroy() are the life
cycle methods of JSP.
Implicit Objects
Out
For writing any data to the buffer, JSP provides an
implicit object named out. It is the object of JspWriter.
Request
The JSP request is an implicit object of type
HttpServletRequest i.e. created for each jsp request
by the web container. It can be used to get request
information such as parameter, header information,
remote address, server name, server port, content
type, character encoding etc.
JSP response implicit object
In JSP, response is an implicit object of type
HttpServletResponse. The instance of
HttpServletResponse is created by the web container
for each jsp request.
Config
In JSP, config is an implicit object of type ServletConfig. This object
can be used to get
initialization parameter for a particular JSP page. The config object
is created by the web container for each jsp page.
<web-app>
<servlet>
<servlet-name>servlet</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
application
In JSP, application is an implicit object of
type ServletContext.
The instance of ServletContext is created only
once by the web container when application or
project is deployed on the server.
This object can be used to get initialization
parameter from configuaration file (web.xml). It
can also be used to get, set or remove attribute
from the application scope.
This initialization parameter can be used by all jsp
pages.
pageContext
Using this object you can find attribute, get
attribute, set attribute and remove attribute at any
of the below levels –
JSP Page – Scope: PAGE_CONTEXT
HTTP Request – Scope: REQUEST_CONTEXT
HTTP Session – Scope: SESSION_CONTEXT
Application Level – Scope:
APPLICATION_CONTEXT
Methods of pageContext
Object findAttribute (String AttributeName)
Object getAttribute (String AttributeName, int Scope)
Object obj = pageContext.getAttribute("BeginnersBook",
PageContext.SESSION_CONTEXT)
void removeAttribute(String AttributeName, int Scope)
pageContext.removeAttribute(“MyAttr”, PageContext.
PAGE_CONTEXT);
void setAttribute(String AttributeName, Object
AttributeValue, int Scope):
pageContext.setAttribute(“mydata”, “This is my data”,
PageContext. APPLICATION_CONTEXT);
Session object
In JSP, session is an implicit object of type
HttpSession.The Java developer can use this
object to set,get or remove attribute or to get
session information.
Page object
Page implicit variable holds the currently
executed servlet object for the corresponding jsp.
Acts as this object for current jsp page.
Exception object
Exception is the implicit object of the throwable
class.
It is used for exception handling in JSP.
The exception object can be only used in error
pages
Scripting Elements
The scripting elements provides the ability to
insert java code inside the jsp. There are three
types of scripting elements:
scriptlet
expression
Declaration
comments
Scriplets
Scriptlets are nothing but java code enclosed
within <% and %> tags.
JSP container moves the statements enclosed in
it to _jspService() method while generating
servlet from JSP.
Declaration
Declaration tag is a block of java code for
declaring class wide variables, methods and
classes.
Whatever placed inside these tags gets initialized
during JSP initialization phase and has class
scope.
JSP container keeps this code outside of the
service method (_jspService()) to make them
class level variables and methods.
Syntax of declaration tag:
<%! Declaration %>
Expression
Expression tag evaluates the expression placed in it,
converts the result into String and send the result
back to the client through response object.
Basically it writes the result to the client(browser).
Syntax
<%= expression %>
Example
<html>
<head>
<title>JSP expression tag example1</title>
</head>
<body> <%= 2+4*5 %>
</body>
</html>
JSP Directives
Directives control the processing of an entire JSP
page. It gives directions to the server regarding
processing of a page.
There are three types of Directives in JSP:
1) Page Directive
2) Include Directive
3) TagLib Directive
Page Directive
There are several attributes, which are used along
with Page Directives and these are –
Import
<%@page import="value"%>
Example
<%@page import="java.io.*%>
<%@page import="java.lang.*%>
Or
<%@page import="java.io.*, java.lang.*"%>
Session
<%@ page session="false"%>
isErrorPage
<%@ page isErrorPage="value"%>
errorPage
<%@ page errorPage="value"%>
ContentType
This attribute is used to set the content type of a JSP
page.
isThreadSafe
It is used to allow multithreading which means only
single thread will execute the page code.
extends
Info
It provides a description to a JSP page. The string
specified in info will return when we will
call getServletInfo() method.
Language
It specifies the scripting language( underlying
language) being used in the page.
Autoflush
Buffer
This attribute is used to specify the buffer size.
Include Directive
Include directive is used to copy the content of
one JSP page to another. It’s like including the
code of one file into another.
<%@include file ="value"%>
Action tag
Each JSP action tag is used to perform some
specific tasks.
The action tags are used to control the flow
between pages and to use Java Bean.
JSP Actions - jsp:forward , jsp:include,
jsp:useBean, jsp:setProperty and jsp:getProperty
jsp:forward action tag
The jsp:forward action tag is used to forward the
request to another resource it may be jsp, html or
another resource.
Syntax
<jsp:forward page="relativeURL | <%= expression
%>" />
jsp:include action tag
The jsp:include action tag is used to include the
content of another resource it may be jsp, html or
servlet.
Difference between include directive and include
action tag
include directive Include action tag
includes resource at
translation time.
includes resource at
request time.
better for static pages. better for dynamic pages.
Workshop on Spring framework
Date :18/06/2022
Time : 8:30-10:30
Mode : online
Platform : MS-Teams