Professional Documents
Culture Documents
U 5 JSP
U 5 JSP
Web Technologies
Unit 5
Java Server Pages
1) Extension to Servlet
JSP technology is the extension to Servlet technology. We can use all the features of the Servlet in JSP.
In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in
JSP, that makes JSP development easy.
2) Easy to maintain: JSP can be easily managed because we can easily separate our business logic
with presentation logic. In Servlet technology, we mix our business logic with the presentation logic.
3) Fast Development: No need to recompile and redeploy
If JSP page is modified, we don't need to recompile and redeploy the project. The Servlet code needs
to be updated and recompiled if we have to change the look and feel of the application.
4) Less code than Servlet
In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduces the code.
Moreover, we can use EL, implicit objects, etc.
The Lifecycle of a JSP Page
1. In case the JSP page was not compiled previously or at any point in
time, then the JSP page is compiled by the JSP engine.
2. In this compilation phase, the Translated .java file of the servlet is
compiled into the Java servlet .class file.
Loading & Instantiation
Loading a Servlet: The Web container or Servlet Container can load
the Servlet at either of the following two stages : Initializing the context,
on configuring the Servlet with a zero or positive integer value.
• If the Servlet is not preceding stage, it may delay the loading process
until the Web container determines that this Servlet is needed to
service a request.
• The Servlet container performs two operations in this stage :
• Loading : Loads the Servlet class.
• Instantiation : Creates an object of the Servlet. To create a new
instance of the Servlet, the container uses the no-argument constructor.
Initialization Phase
• This Execution phase is responsible for all JSP interactions and logic
executions for all requests until the JSP gets destroyed. As the
requested JSP page is loaded and initiated, the JSP engine has to
invoke the _jspService() method. This method is invoked as per the
requests, responsible for generating responses for the associated
requests, and responsible for all HTTP methods.
Destroy(Cleanup) Phase
index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put
Java code in the JSP page. We will learn scriptlet tag later.
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
It will print 10 on the browser.
JSP Implicit Objects
• There are 9 JSP implicit objects. These objects are created by the web container that are
available to all the jsp pages.
• The available implicit objects are out, request, config, session, application etc.
A list of the 9 implicit objects is given below:
Object Type
Out JspWriter
Request HttpServletRequest
Response HttpServletResponse
Config ServletConfig
Application ServletContext
Session HttpSession
pageContext PageContext
Page Object
Exception Throwable
Java Server Page (JSP) Tags
•JSP Scripting Tag
•JSP Action Tag
•JSP directives Tag
JSP Scripting Tag
• Java Server Page (JSP) is a technology for controlling the content or
appearance of Web pages through the use of servlets.
• Small programs that are specified in the Web page and run on the Web
server to modify the Web page before it is sent to the user who
requested it.
• There are total three Scripting Element in JSP
1. Scriptlet tag
2. Expression tag
3. Declaration tag
Scriptlet tag
• This tag allow user to insert java code in JSP. The statement which is
written will be moved to jspservice() using JSP container while
generating servlet from JSP. When client make a request, JSP service
method is invoked and after that the content which is written inside the
scriptlet tag executes.
Explanation
• The Syntax of JSP Scriptlet tag is begin with <% %>
• We can write our java code inside this tag.
• In java we use System.out.println for printing anything on console.
• In JSP, we use only out.print to write somthing on console because the out we’re
referring to isn’t System.out, it’s a variable in the effective method that wraps our
JSP page.
• System.out writes to the servlet container’s console (usually a log file); out is a
different class entirely which writes to the output stream for the generated
response.
• Here we are creating and HTML file to take username from user.save this file as
index.html.
• JSP Scriptlet tag (Scripting elements)
1. Scripting elements
2. JSP scriptlet tag
3. Simple Example of JSP scriptlet tag
4. Example of JSP scriptlet tag that prints the user name
• In JSP, java code can be written inside the jsp page using the scriptlet
tag. Let's see what are the scripting elements first.
• JSP Scripting elements
• The scripting elements provides the ability to insert java code inside
the jsp. There are three types of scripting elements:
• JSP scriptlet tag
• A scriptlet tag is used to execute java source code in JSP. Syntax is as
follows:
<% java source code %>
• Example of JSP scriptlet tag
• In this example, we are displaying a welcome message.
1. <html>
2. <body>
3. <% out.print("welcome to jsp"); %>
4. </body>
5. </html>
Example of JSP scriptlet tag that prints the user name
• In this example, we have created two files index.html and welcome.jsp. The index.html file gets the
username from the user and the welcome.jsp file prints the username with the welcome message.
• File: index.html
1. <html>
2. <body>
3. <form action="welcome.jsp">
4. <input type="text" name="uname">
5. <input type="submit" value="go"><br/>
6. </form>
7. </body>
8. </html>
1. <html>
2. <body>
3. <%
4. String name=request.getParameter("uname");
5. out.print("welcome "+name);
6. %>
7. </form>
8. </body>
9. </html>
JSP ACTION TAG
• The JSP specification provides a standard tag called Action tag used within
JSP code and is used to remove or eliminate Scriptlet code from your JSP
code as JSP Scriptlets are obsolete and are not considered nowadays.
• There are many JSP action tags or elements, and each of them has its own
uses and characteristics.
• Each JSP action tag is implemented to perform some precise tasks.
• The action tag is also implemented to streamline flow between pages and to
employ a Java Bean. As it coincides with the XML standard, the syntax for
the action element is:\
<jsp:action_name attribute = "attribute_value" />
1. jsp:forward: is used for forwarding the request and response to other resources.
2. jsp:include: is used for including another resource.
3. jsp:body: is used for defining dynamically-defined body of XML element.
4. jsp:useBean: is used for creating or locating bean objects.
5. jsp:setProperty: is used for setting the value of property in the bean object.
6. jsp:getProperty: is used for printing the value of the property of the bean.
7. jsp:element: is used for defining XML elements dynamically.
8. jsp:plugin: is used for embedding other components (applets).
9. jsp:param: is used for setting the parameter for (forward or include) value.
10. jsp:text: is used for writing template text in JSP pages and documents.
11. jsp:fallback: is used for printing the message if plugins are working.
12. jsp:attribute: is used for defining attributes of dynamically-defined XML element.
<!DOCTYPE html>
<html>
<head>
<title>Example for Forward Action</title>
</head>
<body>
<h2>JSP page: Demo forward</h2>
<jsp:forward page="forward_eg.jsp" />
</body>
</html>
It is to be noted that this JSP file and forward_eg.jsp file should have to reside in the same
directory to make it work.
• Example of jsp:forward action tag without parameter
• In this example, we are simply forwarding the request to the printdate.jsp
file.
• index.jsp
1. <html>
2. <body>
3. <h2>this is index page</h2>
4. <jsp:forward page="printdate.jsp" />
5. </body>
6. </html>
JSP directives Tag
The jsp directives are messages that tells the web container how to translate a JSP
page into the corresponding servlet.
There are three types of directives:
1. page directive
2. include directive
3. taglib directive
<html>
<body>
<%@ include file="header.html" %>
Today is: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
3. JSP Taglib directive
• The JSP taglib directive is used to define a tag library that defines many tags. We
use the TLD (Tag Library Descriptor) file to define the tags. In the custom tag
section we will use this tag so it will be better to learn it in custom tag.
Syntax JSP Taglib directive
<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
Example of JSP Taglib directive
• In this example, we are using our tag named currentDate. To use this tag we must
specify the taglib directive so the container may get information about the tag.
<html>
<body>
<%@ taglib uri="http://www.javatpoint.com/tags" prefix="mytag" %>
<mytag:currentDate/>
</body>
</html>
Custom Tags in JSP
• 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.
http://localhost:8888/myapplication/index.jsp
1. The Directory structure of JSP
2. The directory structure of JSP page is same as Servlet. We contain the JSP page outside the
WEB-INF folder or in any directory.
JSP scripting elements
1. The code placed within JSP expression tag is written to the output stream of the response. So you
need not write out.print() to write data. It is mainly used to print the values of variable or
method.
JSP Declaration Tag
There are 9 jsp implicit objects. These objects are created by the web container that are available to
all the jsp pages.
The available implicit objects are:
1. out,
2. request,
3. config, session,
4. application etc.
Example of out implicit object
JSP request implicit object
1. 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
The jsp directives are messages that tells the web container how to
translate a JSP page into the corresponding servlet.
There are three types of directives:
I. page directive
II. include directive
III. taglib directive
JSP page directive
• The JSP taglib directive is used to define a tag library that defines
many tags.
• We use the TLD (Tag Library Descriptor) file to define the tags.
JSP Action Tags
• The action tags are used to control the flow between pages and to use
Java Bean. The Jsp action tags are given below.
jsp:forward action tag