Unit 4
Unit 4
Introduction to JSP: The Anatomy of a JSP Page, JSP Processing, Declarations, Directives,
Expressions, Code Snippets, implicit objects, Using Beans in JSP Pages, Using Cookies and
session for session tracking, connecting to database in JSP.
Basically, any html file can be converted to JSP file by just changing the file extension from “.html”
to “.jsp”, it would run just fine. What differentiates JSP from HTML is the ability to use java code
inside HTML. In JSP, you can embed Java code in HTML using JSP tags. for e.g. run the code
below, every time you run this, it would display the current time. That is what makes this code
dynamic.
<HTML>
<BODY>
Hello BeginnersBook Readers!
Current time is: <%= new [Link]() %>
</BODY>
</HTML>
1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While adding
comments to a JSP page you can use this tag, we will discuss this in detail in coming posts.
Note: JSP Comments must starts with a tag <%– and ends with –%>
Page 1 of 25
2) Head, Title and Body tags are HTML tags – They are HTML tags, frequently used for static
web pages. Whatever content they have is delivered to client(Web browser) as such.
3) <%[Link](“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet.
Scriptlets can contain Java codes. syntax of scriptlet is: <%Executable java code%>. As the code in
Scriptlets is java statement, they must end with a semicolon(;). [Link](“ Hello, Sample JSP
code ”) is a java statement, which prints“ Hello, Sample JSP code”.
As discussed, JSP is used for creating dynamic webpages. Dynamic webpages are usually a mix of
static & dynamic content.
The static content can have text-based formats such as HTML, XML etc and the dynamic
content is generated by JSP tags using java code inside HTML .
Servlet Vs JSP
Like JSP, Servlets are also used for generating dynamic webpages. Here is the comparison between
them.
The major difference between them is that servlet adds HTML code inside java while JSP adds java
code inside HTML. There are few other noticeable points that are as follows:
Servlets
1. Servlet is a Java program which supports HTML tags too.
2. Generally used for developing business layer(the complex computational code) of an enterprise
application.
3. Servlets are created and maintained by Java developers.
JSP
1. JSP program is a HTML code which supports java statements too. To be more precise, JSP
embed java in html using JSP tags.
2. Used for developing presentation layer of an enterprise application
3. Frequently used for designing websites and used by web developers.
Advantages of JSP
1. JSP has all the advantages of servlet, like: Better performance than CGI Built in session
features, it also inherits the features of java technology like – multithreading, exception
handling, Database connectivity, etc.
2. JSP enables the separation of content generation from content presentation that makes it more
flexible.
3. With the JSP, it is now easy for web designers to show case the information what is needed.
4. Web Application Programmers can concentrate on how to process/build the information.
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.
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.
Page 2 of 25
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.
Before we start developing web application, we should have a basic idea of architectures. Based on
the location where request processing happens (Servlet OR JSP (java server pages)) there are two
architectures for JSP. They are – Model1 Architecture & Model2 Architecture.
1) Model1 Architecture: In this Model, JSP plays a key role and it is responsible for of processing
the request made by client. Client (Web browser) makes a request; JSP then creates a bean object
which then fulfils the request and passes the response to JSP. JSP then sends the response back to
client. Unlike Model2 architecture, in this Model most of the processing is done by JSP itself.
2) Model2 Architecture: In this Model, Servlet plays a major role and it is responsible for
processing the client’s (web browser) request. Presentation part (GUI part) will be handled by JSP
and it is done with the help of bean as shown in image below. The servlet acts as controller and in
charge of request processing. It creates the bean objects if required by the JSP page and calls the
respective JSP page. The JSP handles the presentation part by using the bean object. In this Model,
JSP doesn’t do any processing; Servlet creates the bean Object and calls the JSP program as per the
request made by client.
Page 3 of 25
Java Server Pages (JSP) Life Cycle
JSP pages are saved with “.jsp” extension which lets the server know that this is a JSP page and
needs to go through JSP life cycle stages. When client makes a request to Server, it first goes to
container. Then container checks whether the servlet class is older than jsp page( To ensure that the
JSP file got modified). If this is the case then container does the translation again (converts JSP to
Servlet) otherwise it skips the translation phase (i.e. if JSP webpage is not modified then it doesn’t
do the translation to improve the performance as this phase takes time and to repeat this step every
time is not time feasible)
1. Translation
2. Compilation
3. Loading
4. Instantiation
5. Initialization
6. RequestProcessing
7. Destruction
[code language=”java”]
public void jspInit()
{
//code to intialize Servlet instances
}[/code]
Page 4 of 25
3) A new thread is then gets created, which invokes the_jspService() method, with a request
(HttpServletRequest) and response (HttpServletRespnse) objects as parameters -shown below.
[code language=”java”]
void _jspService( HttpServletRequest req, HttpServletResponse res)
{
//code goes here
}[/code]
4) Invokes the jspDestroy() method to destroy the instance of the servlet class. code will look like
below –
[code language=”java”]
public void jspDestory()
{
//code to remove the instances of servlet class
}[/code]
A JSP page is simply a regular web page with JSP elements for generating the parts of the page that
differ for each request, as shown in the following figure.
Everything in the page that is not a JSP element is called template text . Template text can really be
any text: HTML, WML, XML, or even plain text. Since HTML is by far the most common web page
language in use today, most of the descriptions and examples in this book are HTML-based, but keep
in mind that JSP has no dependency on HTML; it can be used with any markup language. Template
text is always passed straight through to the browser.
When a JSP page request is processed, the template text and the dynamic content generated by the
JSP elements are merged, and the result is sent as the response to the browser.
Page 5 of 25
JSP Elements
There are three types of JSP elements you can use: directive, action, and scripting. A new construct
added in JSP 2.0 is an Expression Language (EL) expression; let’s call this a forth element type,
even though it’s a bit different than the other three.
Directive elements
The directive elements, shown in the following Table, specify information about the page itself that
remains the same between requests—for example, if session tracking is required or not, buffering
requirements, and the name of a page that should be used to report errors, if any.
Element Description
<%@ include ... %> Includes a file during the translation phase
JSP PROCESSING
1. The web server have JSP engine which acts as a container to process JSP pages.
2. All the requests for JSP Pages are intercepted by JSP Container.
3. JSP container along with web server provide the runtime environment to JSP
Please find the below steps that are required to process JSP Page:
1. Web browser sends an HTTP request to the web server requesting JSP page.
Page 6 of 25
2. Web server recognizes that the HTTP request by web browser is for JSP page by checking the
extension of the file (i.e .jsp)
3. Web server forwards HTTP Request to JSP engine.
4. JSP engine loads the JSP page from disk and converts it into a servlet
5. JSP engine then compiles the servlet into an executable class and forward original request to a
servlet engine.
6. Servlet engine loads and executes the Servlet class.
7. Servlet produces an output in HTML format
8. Output produced by servlet engine is then passes to the web server inside an HTTP response.
9. Web server sends the HTTP response to Web browser in the form of static HTML content.
10. Web browser loads the static page into the browser and thus user can view the dynamically
generated page.
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.
As we know that variables can be initialized using scriptlet too but those declaration being placed
inside _jspService() method which doesn’t make them class wide declarations. On the other
side, declaration tag can be used for defining class level variables, methods and classes.
In this example we have declared two variables inside declaration tag and displayed them on client
using expression tag.
<html>
<head>
<title>Declaration tag Example1</title>
</head>
<body>
<%! String name="Chaitanya"; %>
<%! int age=27; %>
<%= "Name is: "+ name %><br>
<%= "AGE: "+ age %>
</body>
</html>
Page 7 of 25
Output:
In this example we have declared a method sum using JSP declaration tag.
<html>
<head>
<title>Methods Declaration</title>
</head>
<body>
<%!
int sum(int num1, int num2, int num3)
{
return num1+num2+num3;
}
%>
Output:
Page 8 of 25
JSP EXPRESSION TAG
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).
Here we are simply passing the expression of values inside expression tag.
<html>
<head>
<title>JSP expression tag example1</title>
</head>
<body>
<%= 2+4*5 %>
</body>
</html>
Output:
In this example we have initialized few variables and passed the expression of variables in the
expression tag for result evaluation.
<html>
<head>
<title>JSP expression tag example2</title>
</head>
<body>
<%
int a=10;
int b=20;
int c=30;
%>
<%= a+b+c %>
Page 9 of 25
</body>
</html>
Output:
In this example we are setting up an attribute using application implicit object and then displaying
that attribute and a simple string on another JSP page using expression tag.
[Link]
<html>
<head>
<title> JSP expression tag example3 </title>
</head>
<body>
<% [Link]("MyName", "Chaitanya"); %>
<a href="[Link]">Click here for display</a>
</body>
</html>
[Link]
<html>
<head>
<title>Display Page</title>
</head>
<body>
<%="This is a String" %><br>
<%= [Link]("MyName") %>
</body>
</html>
Output:
Page 10 of 25
JSP DIRECTIVES – PAGE, INCLUDE AND TAG LIB
Directives control the processing of an entire JSP page. It gives directions to the server regarding
processing of a page.
Syntax of Directives:
1) Page Directive
There are several attributes, which are used along with Page Directives and these are –
1. import
2. session
3. isErrorPage
4. errorPage
5. ContentType
6. isThreadSafe
7. extends
8. info
9. language
10. autoflush
11. buffer
1. import:
This attribute is used to import packages. While doing coding you may need to include more than
one packages, In such scenarios this page directive’s attribute is very useful as it allows you to
mention more than one packages at the same place separated by commas (,). Alternatively you can
have multiple instances of page element each one with different package.
<%@page import="value"%>
Here value is package name.
Example of import- The following is an example of how to import more than one package using
import attribute of page directive.
Page 11 of 25
<%@page import="[Link].*%>
<%@page import="[Link].*%>
<%--Comment: OR Below Statement: Both are Same--%>
<%@page import="[Link].*, [Link].*"%>
2. session:
Generally while building a user interactive JSP application, we make sure to give access to the user
to get hold of his/her personal data till the session is active. Consider an example of logging in into
your bank account, we can access all of your data till we signout (or session expires). In order to
maintain session for a page the session attribute should be true.
This attribute is to handle HTTP sessions for JSP pages. It can have two values: true or false. Default
value for session attribute is true, which means if you do not mention this attribute, server may
assume that HTTP session is required for this page.
Examples of session:
3. isErrorPage:
This attribute is used to specify whether the current JSP page can be used as an error page for
another JSP page. If value of isErrorPage is true it means that the page can be used for exception
handling for another page. Generally these pages has error/warning messages OR exception handling
codes and being called by another JSP page when there is an exception occurred there.
There is another use of isErrorPage attribute – The exception implicit object can only be available to
those pages which has isErrorPage set to true. If the value is false, the page cannot use exception
implicit object.
Page 12 of 25
Example of isErrorPage:
4. errorPage:
When isErrorPage attribute is true for a particular page then it means that the page can be called by
another page in case of an exception. errorPage attribute is used to specify the URL of a JSP page
which has isErrorPage attrbute set to true. It handles the un-handled exceptions in the page.
Example of errorPage:
5. contentType:
Example of contentType:
Lets understand this with an example. Suppose you have created a JSP page and mentioned
isThreadSafe as true, it means that the JSP page supports multithreading (more than one thread can
execute the JSP page simultaneously). On the other hand if it is set to false then JSP engine won’t
allow multithreading which means only single thread will execute the page code.
Page 13 of 25
Syntax of isThreadSafe attribute:
Example of isThreadSafe:
7. buffer:
This attribute is used to specify the buffer size. If you specify this to none during coding then the
output would directly written to Response object by JSPWriter. And, if you specify a buffer size then
the output first written to buffer then it will be available for response object.
Example of buffer:
Like java, here also this attribute is used to extend(inherit) the class.
Example of extends:
The below code will inherit the SampleClass from package: mypackage
It provides a description to a JSP page. The string specified in info will return when we will call
getServletInfo() method.
Page 14 of 25
Syntax of info:
10. language:
It specifies the scripting language( underlying language) being used in the page.
Syntax of language:
11. autoFlush:
If it is true it means the buffer should be flushed whenever it is full. false will throw an exception
when buffer overflows.
Syntax of autoFlush:
12. isScriptingEnabled:
Page 15 of 25
13. isELIgnored:
Syntax of isELIgnored:
2) 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.
Example:
<%@include file="[Link]"%>
You can use the above code in your JSP page to copy the content of [Link] file. However in this
case both the JSP files must be in the same directory. If the [Link] is in the different directory
then instead of just file name you would need to specify the complete path in above code.
3) 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.
Page 16 of 25
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:
JSP Standard Tag Library (JSTL) is a set of useful tags to simplify the JSP development. It provides
tags to control the JSP page behavior, iteration and control statements, internationalization tags, and
SQL tags. JSTL is part of the Java EE API and included in most servlet containers. There are five
groups of JSTL: core tags, sql tags, xml tags, internationalization tags and functions tags.
In the code snippet below, there is a simple loop coded with JSTL. Without any tag library or tags,
we can write the counterpart code with scriptlets that contain Java code in it. But external tag
libraries provide more simple and useful capabilities to us. We can do more with writing less.
<c:out value="${number}"></c:out>
</c:forEach>
These objects are created by JSP Engine during translation phase (while translating JSP to Servlet).
They are being created inside service method so we can directly use them within Scriptlet without
initializing and declaring them. There are total 9 implicit objects available in JSP.
out [Link]
request [Link]
response [Link]
session [Link]
Page 17 of 25
application [Link]
exception [Link]
page [Link]
pageContext [Link]
config [Link]
1. Out: This is used for writing content to the client (browser). It has several methods which can
be used for properly formatting output message to the browser and for dealing with the buffer.
2. Request: The main purpose of request implicit object is to get the data on a JSP page which
has been entered by user on the previous JSP page. While dealing with login and signup forms
in JSP we often prompts user to fill in those details, this object is then used to get those entered
details on an another JSP page (action page) for validation and other purposes.
3. Response: It is basically used for modfying or delaing with the response which is being sent to
the client(browser) after processing the request.
4. Session: It is most frequently used implicit object, which is used for storing the user’s data to
make it available on other JSP pages till the user session is active.
5. Application: This is used for getting application-wide initialization parameters and to
maintain useful data across whole JSP application.
6. Exception: Exception implicit object is used in exception handling for displaying the error
messages. This object is only available to the JSP pages, which has isErrorPage set to true.
7. Page: Page implicit object is a reference to the current Servlet instance (Converted Servlet,
generated during translation phase from a JSP page). We can simply use this in place of it. I’m
not covering it in detail as it is rarely used and not a useful implicit object while building a JSP
application.
8. pageContext: It is used for accessing page, request, application and session attributes.
9. Config: This is a Servlet configuration object and mainly used for accessing getting
configuration information such as servlet context, servlet name, configuration parameters etc.
Syntax of jsp:useBean:
<jsp:setProperty name="unique_name_to_identify_bean"
property="property_name" />
Syntax of jsp:getProperty:
<jsp:getProperty name="unique_name_to_identify_bean"
property="property_name" />
Page 18 of 25
A complete example of useBean, setProperty and getProperty
1) We have a bean class Details where we are having three variables username, age and password. In
order to use the bean class and it’s properties in JSP we have initialized the class like this in the
[Link] page –
2) We have mapped the properties of bean class and JSP using setProperty action tag. We have given
‘*’ in the property field to map the values based on their names because we have used the same
property name in bean class and [Link] JSP page. In the name field we have given the unique
identifier which we have defined in useBean tag.
[Link]
package [Link];
public class Details {
public Details() {
}
private String username;
private int age;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
[Link] = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
[Link] = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
[Link] = password;
}
Page 19 of 25
[Link]
<html>
<head>
<title>
useBean, getProperty and setProperty example
</title>
</head>
<form action="[Link]" method="post">
User Name: <input type="text" name="username"><br>
User Password: <input type="password" name="password"><br>
User Age: <input type="text" name="age"><br>
<input type="submit" value="register">
</form>
</html>
[Link]
Output:
Page 20 of 25
COOKIES IN JSP
Cookies are the text files which are stored on the client machine.
They are used to track the information for various purposes.
It supports HTTP cookies using servlet technology
The cookies are set in the HTTP Header.
If the browser is configured to store cookies, it will keep information until expiry date.
It sets the maximum time which should apply till the cookie expires
Public intgetMaxAge()
Page 21 of 25
It describes the cookie purpose
Example:
In this example, we are creating cookies of username and email and add age to the cookie for 10
hours and trying to get the variable names in the action_cookie.jsp
Action_cookie.jsp
Action_cookie_main.jsp
[Link](60*60*10);
[Link](60*60*10);
Page 22 of 25
// Add both the cookies in the response header.
[Link]( username );
[Link]( email );
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Guru Cookie JSP</title>
</head>
<body>
<b>Username:</b>
<%= [Link]("username")%>
<b>Email:</b>
<%= [Link]("email")%>
</body>
</html>
Action_cookie.jsp
Code Line 10-15: Here we are taking a form which has to be processed in action_cookie_main.jsp.
Also, we are taking two fields "username" and "email" which has to be taken input from the user
with a submit button.
Action_cookie_main.jsp
Code Line 6-9: Creating two cookie objects of "username" and "email" using [Link].
Code Line 12-13: Here we are adding age to both the cookies, which have been created of 10 hours
i.e. cookies will expire in that age.
Code Line 16-17: Adding cookies to the session of username and email and these two cookies can
fetched when requested by getParameter().
Output:
When you execute the above code you get the following output:
Page 23 of 25
When we execute the action_cookie.jsp we get two fields username and email, and it takes user input
and then we click on the submit button.
We get the output from action_cookie_main.jsp where variables are stored in the cookies on the
client side.
We have already seen invalidate() method in session implicit object tutorial. In this post we are going
to discuss it in detail. Here we will see how to validate/invalidate a session.
Example
Let’s understand this with the help of an example: In the below example we have three “jsp” pages.
[Link]: It is having four variables which are being stored in session object.
[Link]: It is fetching the attributes (variables) from session and displaying them.
[Link]: It is first calling [Link]() in order to invalidate (make the session
inactive) the session and then it has a logic to validate the session (checking whether the
session is active or not).
[Link]
<%
String firstname="Chaitanya";
String middlename="Pratap";
String lastname="Singh";
int age= 26;
[Link]( "fname", firstname );
[Link]( "mname", middlename );
[Link]( "lname", lastname );
[Link]( "UAge", age );
%>
<a href="[Link]">See Details</a>
<a href="[Link]">Invalidate Session</a>
[Link]
[Link]
<%[Link]();%>
Page 24 of 25
<% HttpSession nsession = [Link](false);
if(nsession!=null)
{
String data=(String)[Link]( "fname" );
[Link](data);
}
else
[Link]("Session is not active");
%>
Output
while opening display page, all the attributes are getting displayed on client (browser).
Since we have already called invalidate in the first line of [Link], it is displaying the message
“Session is not active” on the screen
Points to Note:
1) This will deactivate the session
<%[Link]();%>
2) This logic will exceute the if body when the session is active else it would run the else part.
<% HttpSession nsession = [Link](false);
if(nsession!=null)
...
else
...
%>
1. [Link]
2. [Link]
3. [Link]
4. [Link]
3rd/0596005636/[Link]#jserverpages3-CHP-3-TABLE-1
5. [Link]
6. [Link]
7. [Link]
8. [Link]
Page 25 of 25
JSP - Database Access
To start with basic concept, let us create a table and create a few records in that table as follows:
Create Table
To create the Employees table in the EMP database, use the following steps:
Step 1
Open a Command Prompt and change to the installation directory as follows:
C:\>
C:\Program Files\MySQL\bin>
Step 2
Login to the database as follows:
C:\Program Files\MySQL\bin>mysql -u root -p
mysql>
Step 3
Create the Employee table in the TEST database as follows:
mysql> use TEST;
);
mysql>
1
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
mysql>
SELECT Operation
Following example shows how we can execute the SQL SELECT statement using JTSL in JSP
programming:
<%@ page import = "[Link].*,[Link].*,[Link].*"%>
<html>
<head>
<title>SELECT Operation</title>
</head>
<body>
url = "jdbc:mysql://localhost/TEST"
</sql:query>
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
2
</tr>
<tr>
</tr>
</c:forEach>
</table>
</body>
</html>
INSERT Operation
Following example shows how we can execute the SQL INSERT statement using JTSL in JSP
programming.
<%@ page import = "[Link].*,[Link].*,[Link].*"%>
<html>
<head>
<title>JINSERT Operation</title>
</head>
3
<body>
url = "jdbc:mysql://localhost/TEST"
</sql:update>
</sql:query>
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
</tr>
</c:forEach>
</table>
</body>
</html>
Access the above JSP, the following result will be displayed as:
4
100 Zara Ali 18
DELETE Operation
Following example shows how we can execute the SQL DELETE statement using JTSL in JSP
programming.
<%@ page import = "[Link].*,[Link].*,[Link].*"%>
<html>
<head>
<title>DELETE Operation</title>
</head>
<body>
url = "jdbc:mysql://localhost/TEST"
</sql:update>
</sql:query>
5
<table border = "1" width = "100%">
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
</tr>
</c:forEach>
</table>
</body>
</html>
UPDATE Operation
Following example shows how we can execute the SQL UPDATE statement using JTSL in JSP
programming.
<%@ page import = "[Link].*,[Link].*,[Link].*"%>
6
<html>
<head>
<title>DELETE Operation</title>
</head>
<body>
url = "jdbc:mysql://localhost/TEST"
</sql:update>
</sql:query>
<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<tr>
7
</tr>
</c:forEach>
</table>
</body>
</html>
Following are the unique characteristics that distinguish a JavaBean from other Java classes:
JavaBeans Properties
A JavaBean property is a named attribute that can be accessed by the user of the object. The
attribute can be of any Java data type, including the classes that you define.
A JavaBean property may be read, write, read only, or write only. JavaBean properties are
accessed through two methods in the JavaBean's implementation class.
getPropertyName()
1 For example, if property name is firstName, your method name would
be getFirstName() to read that property. This method is called accessor.
8
setPropertyName()
2 For example, if property name is firstName, your method name would
be setFirstName() to write that property. This method is called mutator.
A read-only attribute will have only a getPropertyName() method, and a write-only attribute
will have only a setPropertyName() method.
JavaBeans Example
Consider a student class with few properties −
package [Link];
public StudentsBean() {
return firstName;
return lastName;
return age;
[Link] = firstName;
[Link] = lastName;
9
public void setAge(Integer age){
[Link] = age;
Accessing JavaBeans
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a
scripting variable that can be accessed by both scripting elements and other custom tags used in
the JSP. The full syntax for the useBean tag is as follows:
Here values for the scope attribute can be a page, request, session or application based on your
requirement. The value of the id attribute may be any value as a long as it is a unique name
among other useBean declarations in the same JSP.
<html>
<head>
<title>useBean Example</title>
</head>
<body>
</body>
</html>
10
<jsp:useBean id = "id" class = "bean's class" scope = "bean's scope">
value = "value"/>
...........
</jsp:useBean>
The name attribute references the id of a JavaBean previously introduced to the JSP by the
useBean action. The property attribute is the name of the getor the set methods that should be
invoked.
Following example shows how to access the data using the above syntax:
<html>
<head>
</head>
<body>
</jsp:useBean>
</p>
</p>
<p>Student Age:
11
<jsp:getProperty name = "students" property = "age"/>
</p>
</body>
</html>
Let us make the [Link] available in CLASSPATH. Access the above JSP
thefollowing result will be displayed as :
Student Age: 10
12