0% found this document useful (0 votes)
15 views22 pages

Lecture 3.4

The document provides an overview of Java Server Pages (JSP), including its purpose, advantages, and key components such as JSP declarations, directives, and scriptlets. It explains the use of implicit objects in JSP, how to create JSP pages in Eclipse with Tomcat, and the syntax for various JSP elements. Additionally, it outlines the types of JSP directives and their applications in web development.

Uploaded by

anuragjangid6373
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views22 pages

Lecture 3.4

The document provides an overview of Java Server Pages (JSP), including its purpose, advantages, and key components such as JSP declarations, directives, and scriptlets. It explains the use of implicit objects in JSP, how to create JSP pages in Eclipse with Tomcat, and the syntax for various JSP elements. Additionally, it outlines the types of JSP directives and their applications in web development.

Uploaded by

anuragjangid6373
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science
& Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)

TOPIC OF PRESENTATION:
JSP declaration, JSP directives, JSP
Scriptlets (CO 5)
DISCOVER . LEARN .
EMPOWER
Lecture Objectives

In this lecture, we will


discuss:
•JSP declaration, JSP
directives, JSP Scriptlets

2
What is JSP?

JSP stands for Java Server Pages, a technology invented by Sun


to allow the easy creation of server side HTML pages.

JSP lies in the presentation tier on the web server, with the main
responsibility of generating HTML content that needs to be
served to the browser. It also has the additional responsibility
of pass on the requests to the backend through the JavaBeans,
as and when required.
Why JSP?

1. High performance
2. Convenient to code and maintain as against servlets
3. Separates presentation from content
4. Powered by Java
– Has access to all Java APIs
– Has access to all J2EE APIsa
– Inherent Platform independence
Creating JSP in Eclipse IDE with Tomcat server

• Create a Dynamic web project


• create a jsp
• start tomcat server and deploy the project
JSP Scriptlet tag (Scripting elements)

JSP Scripting elements


The scripting elements provides the ability to insert java code
inside the jsp.
There are three types of scripting elements:
• scriptlet tag
• expression tag
• declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java source code in JSP.

Syntax is as follows:
<% java source code %>
JSP expression tag
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.
Syntax of JSP expression tag
<%= statement %>

JSP Declaration Tag


The JSP declaration tag is used to declare fields and methods.
The code written inside the jsp declaration tag is placed outside the service()
method of auto generated servlet.
So it doesn't get memory at each request.

Syntax of JSP declaration tag


The syntax of the declaration tag is as follows:
<%! field or method declaration %>
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
JSP out implicit object
For writing any data to the buffer, JSP provides an implicit object named out. It is the
object of JspWriter.

JSP request implicit object


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.
It can also be used to set, get and remove attributes from the jsp request scope.

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.
It can be used to add or manipulate response such as redirect response to another
resource, send error etc.
JSP config implicit object
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.

Generally, it is used to get initialization parameter from the web.xml file.

JSP application implicit object


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.


session implicit 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.
Example of session implicit object

Index.html Welcome.jsp
<html> <html>
<body> <body>
<form action="welcome.jsp"> <%
<input type="text" name="uname"> String name=request.getParameter("uname");
<input type="submit" value="go"><br/>
</form> out.print("Welcome "+name);
</body> session.setAttribute("user",name);
</html> <a href="second.jsp">second jsp page</
a> %>
</body>
</html>
second.jsp
<html>
<body>
<%

String name=(String)session.getAttribute("user");
out.print("Hello "+name);

%>
</body>
</html>
exception implicit object
In JSP, exception is an implicit object of type java.lang. Throwable class. This object can be used
to print the exception. But it can only be used in error pages.
It is better to learn it after page directive. Let's see a simple example:

Example of exception implicit object:

error.jsp
<%@ page isErrorPage="true" %>
<html>
<body>

Sorry following exception occured:<%= exception %>

</body>
</html>
Example of exception handling in jsp by the elements of page directive

In this case, you must define and create a page to handle the exceptions, as in
the error.jsp page.

The pages where may occur exception, define the errorPage attribute of page
directive, as in the process.jsp page.

There are 3 files:


index9.jsp for input values
process9.jsp for dividing the two numbers and displaying the result
error9.jsp for handling the exception
JSP directives

The jsp directives are messages that tells the web container how to translate a
JSP page into the corresponding servlet.

Types of directives are as follows:


page directive
include directive
taglib directive

Syntax of JSP Directive


<%@ directive attribute="value" %>
JSP page directive
The page directive defines attributes that apply to an entire JSP page.

Syntax of JSP page directive


<%@ page attribute="value" %>

Attributes of JSP page directive


• import
• contentType
• buffer
• language
• isELIgnored
• pageEncoding
• errorPage
• isErrorPage
Jsp Include Directive

The include directive is used to include the contents of any resource it may be
jsp file, html file or text file.
The include directive includes the original content of the included resource at
page translation time (the jsp page is translated only once so it will be better
to include static resource).

Advantage of Include directive


• Code Reusability

Syntax of include directive


<%@ include file="resourceName" %>
Taglib Directive
This directive basically allows user to use Custom tags in JSP. we shall discuss
about Custom tags in detail in coming JSP tutorials. Taglib directive helps
you to declare custom tags in JSP page.
Syntax of Taglib Directive:
• <%@taglib uri ="taglibURI" prefix="tag prefix"%>Where URI is uniform
resource locator, which is used to identify the location of custom tag and
tag prefix is a string which can identify the custom tag in the location
identified by uri.

Example of Targlib:
• <%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag"
%> <html> <body> <demotag:welcome/> </body> </html>As you can see
that uri is having the location of custom tag library and prefix is identifying
the prefix of custom tag.
Note: In above example – <demotag: welcome> has a prefix demotag.
Summary:

In this session, you were able to :


• JSP declaration, JSP directives, JSP
Scriptlets
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://www.youtube.com/watch?v=NQ7xKNULkTk
Reference Links:
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://docs.oracle.com/javaee/5/tutorial/doc/bnaou.html
https://beginnersbook.com/2013/05/jsp-tutorial-scriptlets/
https://www.javatpoint.com/jsp-scriptlet-tag
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://beginnersbook.com/2013/11/jsp-declaration-tag/

You might also like