0% found this document useful (0 votes)
12 views30 pages

CH-3 Part 2

The document provides an overview of JavaServer Pages (JSP) focusing on directive tags, action tags, and JavaBeans. It explains the syntax and attributes of various JSP directives such as page, include, and taglib, as well as action tags like <jsp:include> and <jsp:forward>. Additionally, it covers the use of JavaBeans in JSP, detailing how to instantiate and manipulate them using <jsp:useBean>, <jsp:setProperty>, and <jsp:getProperty> tags.

Uploaded by

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

CH-3 Part 2

The document provides an overview of JavaServer Pages (JSP) focusing on directive tags, action tags, and JavaBeans. It explains the syntax and attributes of various JSP directives such as page, include, and taglib, as well as action tags like <jsp:include> and <jsp:forward>. Additionally, it covers the use of JavaBeans in JSP, detailing how to instantiate and manipulate them using <jsp:useBean>, <jsp:setProperty>, and <jsp:getProperty> tags.

Uploaded by

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

Java Web Technologies

(MTCA13201 )
Unit 3: JSP and JSP Expression Language,
Java Beans
Directive Tag
 Provide directions that are used by the JSP translator during the translation stage
of the JSP life cycle.
 how to translate the JSP page into its respective servlet.
 Syntax :
<%@ directive attribute = "value"%>
 Directives can have a number of attributes that you can list down as key-value
pairs.
 Three types of Directive tags supported in JSP.
 page
 include
 taglib
Page directive
 JSP page directive is used to define the properties applying the JSP page, such as
the size of the allocated buffer, imported packages, and classes/interfaces,
defining what type of page it is, etc.
 The page directive defines attributes that apply to an entire JSP page.
 The page directive is used to provide instructions to the container.
 By convention, page directives are coded at the top of the JSP page.
 Syntax:
<%@page attribute = "value"%>
Attributes
 import: This tells the container what packages/classes are needed to be
imported into the program.
 The import attribute is used to import class, interface or all the members of a
package. It is similar to import keyword in java class or interface.
 Example:
<%@page import = "java.util.Date"%>

 language: The language attribute specifies the scripting language used in


the JSP page. The default value is "java".
 JSP only supports Java, so this attribute is rarely modified.
 Example:
<%@ page language="java" %>
 extends: The extends attribute defines the parent class that will be inherited by
the generated servlet.
 Specifies a super class that the generated servlet must extend.
 Example:
<%@ page extends="com.example.MyServletBaseClass" %>

 session: Controls whether the session object is available.


 If the value is true, an implicit variable session is available and references current
session.
 If the value is false than implicit variable session is unavailable and any attempt
to use it within the body of the page will result in translation error.
 Example:
<%@ page session="false" %>

 buffer: Defines the size of the buffer that is allocated for handling the JSP page.
The size is defined in Kilo Bytes.
 The values of this attribute are 8 kb, 16 kb, 32 kb and 64 kb. The default value is
8 kb.
 Defines the buffer size for storing output before sending it to the client.
 Example:
<%@ page buffer="16kb" %>
 autoFlush: Indicates whether the buffered output should be flushed
automatically when the buffer is full, or whether an exception should be raised
indicating buffer overflow.
 autoFlush cannot be set to true when buffer is none, otherwise it will result in
translation error.
 Example:
<%@ page autoFlush="false" %>

 isThreadSafe: Indicates the threading model to be used by JSP and generated


servlet class.
 True means the JSP is thread safe and container can send multiple simultaneous
requests to the page.
 Value of false means the JSP page is not thread safe and container must send a
request at a time, in other words container must serialize the requests to JSP
page.
 Specifies if the page can handle multiple requests simultaneously.
 Example:
<%@ page isThreadSafe="false" %>
 info: This attribute simply sets the information of the JSP page which is retrieved
later by using getServletInfo() method of Servlet interface.
 Provides metadata for the JSP page.
 Example:
<%@ page info="This is a demo JSP page" %>

 isErrorPage: It classifies whether the page is an error page or not.


 If value is true than an implicit scripting language variable exception is available
that references the exception.
 If the value is false than implicit object exception is unavailable and any attempt
to use it within the body of the JSP page will result in translation time error.
 Example:
<%@ page isErrorPage="true" %>
 errorPage: The errorPage attribute is used to define the error page, if exception
occurs in the current page, it will be redirected to the error page.
 Specifies an error-handling page.
 Example:
<%@ page errorPage="error.jsp" %>

 contentType: Defines the MIME(Multipurpose Internet Mail Extension) type for


the response of the JSP page and character encoding for the JSP page and
response.
 Default value for MIMETYPE of the generated response is text/html for jsp page
in standard syntax and text/xml for JSP documents in XML syntax.
 Defines the content type of the response.
 Example:
<%@ page contentType="text/html; charset=UTF-8" %>

 pageEncoding: Defines the character encoding for the JSP page.


 Default value is ISO-8859-1.
 Example:
<%@ page pageEncoding="UTF-8" %>
Example
<%@ page
language="java"
extends="package.class“
import="package.class, package.*, ..."
session="true|false“
buffer="none|default|sizekb"
autoFlush="true|false"
isThreadSafe="true|false"
info="Sample JSP to show tags"
isErrorPage="true|false"
errorPage="ErrorPage.jsp"
contentType="TYPE| TYPE; charset=CHARSET| text/html; charset=ISO-8859-1"
pageEncoding="default"
%>
include directive
 include directive is used to include a file during the translation phase.
 This directive tells the container to merge the content of other external files with
the current JSP during the translation phase.
 Both dynamic and static files can be included
 If dynamic file is included, its JSP elements are first translated before being
included in the JSP page.
 These files can be html files, jsp files etc.
 The advantage of using an include directive is that it allows code re-usability.
 Syntax:
<%@include file=“file path” %>

 Example:
<%@ include file=“/Test.jsp %>

 XML Syntax:
<jsp:directive.include file="relative url" />
taglib directive
 Used to declare a custom tag library in a JSP page so that the tags related to that
custom tag library can be used in the same JSP page.
 The taglib directive declares that your JSP page uses a set of custom tags,
identifies the location of the library, and provides means for identifying the
custom tags in your JSP page.
 It allows you to use custom tags defined in a .tld (Tag Library Descriptor) file,
which can encapsulate reusable functionality.
 Custom tags are user-defined JSP elements that execute custom logic when
invoked in a JSP file.
 Syntax:
<%@taglib uri=“URI” prefix=“unique_prefix” %>
 Three components:
 Create the Tag Handler Class
 Create a Tag Library Descriptor (TLD) File
 Use the Custom Tag in a JSP File
Example:
<%@ taglib uri="/WEB-INF/tlds/mytags.tld“ prefix = "mytag" %>
<html>
<body>
<mytag:hello/>
</body>
</html>
Action Tag
 Java Server Pages can provide action tags that can enable the developers to
perform specific tasks including the other files, forwarding the requests, and
manipulating the session data within the JSP documents.
 Action tags are denoted by the <jsp: ..> syntax and it is used to perform the
dynamic actions on the server side of the JSP applications.
<jsp:include> Action tag
 The <jsp:include> action tag in JSP consists of the content of one JSP page into
another JSP page at the request time, not during the translation phase.
 This approach allows developers to reuse common elements across multiple
pages dynamically, enhancing modularity and maintainability.
 The <jsp:include> tag can be used to include static as well as dynamic pages.
 Unlike the include directive, which inserts the file at the time the JSP page is
translated into a servlet, this action inserts the file at the time the page is
requested.
Attributes
 page: Takes a relative URL which specifies the location of the resource to be
included in the JSP page.
Syntax:
<jsp:include page = "date.jsp"/>

 flush: Takes either the true or false value, to indicate whether the buffer needs to
be flushed or not before including a resource.
 If the true value is assigned to this attribute, the buffer is flushed before
including the resource.
 Default value : false
Syntax:
<jsp:include page = "date.jsp" flush = "true" />
Difference between include directive and include tag

JSP include directive JSP include action


includes resource at translation time. includes resource at request time.
better for static pages. better for dynamic pages.
Actual content of file is included Only output of page in included
<jsp:param> action tag
 The <jsp:param> action tag is used to pass parameters from one JSP page to
another, typically inside <jsp:include> or <jsp:forward>.
 It allows dynamic content to be passed between JSP pages.

Syntax:
<jsp:param name="paramName" value="paramValue" />
 name: The name of the parameter.
 value: The value assigned to the parameter.
Example
<jsp:include page="action_tag.jsp">
<jsp:param name="username" value="Rohan" />
</jsp:include>
<jsp:forward> action tag
 The <jsp:forward> action tag in JSP is used to forward the request to another
resource (such as another JSP page, servlet, or static file).
 It is useful when you want to transfer the control of the request to another page
without requiring user intervention.
 The forward action terminates the action of the current page and forwards the
request to another resource such as a static page, another JSP page, or a Java
Servlet.
Example:
<jsp:forward page = "Relative URL" />
 page: Should consist of a relative URL of another resource such as a static
page, another JSP page, or a Java Servlet.
Example

<jsp:forward page=“index.jsp">
<jsp:param name="user" value=“Rohan"/>
</jsp:forward>
JSP Bean
 JavaServer Pages provides the mechanism to interact with the Java objects
called JavaBeans.
 JavaBeans are reusable software components for Java.
 They are classes written in the Java programming language conforming to a
particular convention.
 Used to encapsulate many objects into a single object (the bean), so that they
can be passed around as a single bean object instead of as multiple individual
objects.
 Often used as the container for the dynamic content to be displayed by a web
page.
 Separates business logic from presentation logic.
 It typically consists of a class with private instance variables (fields), public getters
and setters, a default constructor,
<jsp:usebean>
 Encapsulate the business logic in a Java Object (a Java Bean), and then instantiate
and use this object within a JSP page.
 It is used to instantiate a JavaBean or locate an existing JavaBean instance and
assign it a variable name or id.
 <jsp:useBean> tag in JSP can be used to create or retrieve a JavaBean instance
and provides a way to use Java objects in JSP pages without the need for explicit
Java code.

Syntax:
<jsp:useBean id="beanName" class="fullyQualifiedClassName" scope="scope"/>

 id: The reference name for the bean in the JSP.


 scope: The scope in which the bean will be stored.
 class: Takes class name to create a JavaBean instance
Scope of JSP Bean
 page scope: JavaBean is only available for the current page. The default scope is
page.
 request scope: JavaBean is available for the current request.
 session scope: The JavaBean is available for the entire session.
 application scope: JavaBean is available for the entire application.
<jsp:setProperty>
 Sets a property value in the JavaBean.
 While using the action tag, the name attribute of the action tag must be the
same as the id attribute of the action tag.
 It can either set a specific value or retrieve the value from the request
parameters.

 Example:
<jsp:setProperty name="person" property="name" value=“Rohan" />
<jsp:setProperty name="person" property="age" value="25" />

 name: The name of the bean (id used in <jsp:useBean>).


 property: The property of the bean you want to set.
 value: The value you want to assign to the property.
Examples to use <jsp:setProperty>
 Code to display param property
<jsp:setProperty name=“reference variable name” property=“property-name”
param=“request parameter name” />
 Code to set all properties in a JB
<jsp:setProperty name=“mybean” property=“*” />
 Code to set a specific property in a JB
<jsp:setProperty name=“mybean” property =“uname” param=“uname” />
 Code to set property at runtime with value
<jsp:setProperty name=“mybean” property=“uname” value=“<%=uname %> “/>
<jsp:getProperty>
 The <jsp:getProperty> tag is used to retrieve the value of a property from the
JavaBean and display it in the JSP page.

 Example:
<p>Name: <jsp:getProperty name="person" property="name" /></p>
<p>Age: <jsp:getProperty name="person" property="age" /></p>

 name: name of the reference variable name on which you want to invoke the
getter method. The name of the bean.
 property: takes the name of a JB property and invokes the getter method of
the property. The property of the bean you want to display.
Sharing Bean

You might also like