CH-3 Part 2
CH-3 Part 2
(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"%>
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" %>
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
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"/>
Example:
<jsp:setProperty name="person" property="name" value=“Rohan" />
<jsp:setProperty name="person" property="age" value="25" />
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