<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">The<strong> jsp useBean</strong> action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn&#8217;t create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.</p>
<h2><b>Syntax of<i> jsp:useBean</i> action tag</b></h2>
<pre class="highlight"><;jsp:useBean id= "instanceName" scope= "page | request | session | application" 
class= "packageName.className" type= "packageName.className" 
beanName="packageName.className | <;%= expression >;" >; 
<;/jsp:useBean>; 
</pre>
<h2><b>Attributes and Usage of <i>jsp:useBean</i> action tag</b></h2>
<ol>
<li><b>id: </b> is used to identify the bean in the specified scope.</li>
<li><b>scope: </b> represents the scope of the bean. It may be page, request, session or application. The default scope is page.
<ul>
<li><b>page: </b> specifies that you can use this bean within the JSP page. The default scope is page.</li>
<li><b>request: </b> specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.</li>
<li><b>session: </b> specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request.</li>
<li><b>application: </b> specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.</li>
</ul>
</li>
<li><b>class: </b>instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and must not be abstract.</li>
<li><b>type: </b> provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If you use it without class or beanName, no bean is instantiated.</li>
<li><b>beanName: </b> instantiates the bean using the java.beans.Beans.instantiate() method.</li>
</ol>
<h2><b>Simple example of <i>jsp:useBean </i>action tag</b></h2>
<p>In this example, we are simply invoking the method of the Bean class.<br />
<b>EmployeeData.java</b></p>
<pre class="highlight">package com.dineshonjava; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmployeeData { 
 
 public String getAge(String name){ 
 return "Age of "+name+" is 27 Years"; 
 } 
 
 public int getSalary(){ 
 return 70000; 
 } 
 
 public int getArea(int height, int width){ 
 return (height*width); 
 } 
} 
</pre>
<p><b>index.jsp</b></p>
<pre class="highlight"><;jsp:useBean id="obj" class="com.dineshonjava.EmployeeData"/>; 
<;% 
String age = obj.getAge("Dinesh Rajput"); 
out.println(age); 
%>; 
<;br/>; 
<;% 
int salary = obj.getSalary(); 
out.println("Salary is Rs. "+salary); 
%>; 
<;br/>; 
<;% 
int area = obj.getArea(200, 100); 
out.println("Area of Home is "+area+" Sq-ft" ); 
%>; 
</pre>
<p>Make a dirctory <i><b>myapp/com/dineshonjava/</b></i> at webapp folder of tomcat server at<br />
<b><i>C:Program Files (x86)Apache Software FoundationTomcat 7.0webapps</i></b><br />
and make class file <i><b>EmployeeData.class</b></i> and create <b><i>index.jsp</i></b> file with in <i><b>myapp </b></i>folder.<br />
Now restart tomcat server and hit the following url.<br />
<i><b>http://localhost:8080/myapp/index.jsp</b></i></p>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><b><img src="https://dineshonjava.com/wp-content/uploads/2013/09/555.png" border="0" /></b></div>
<h3><b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/myapp.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Download Example</a></b></h3>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/jsp-include-tag-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/jsp-tutorial-baby-step-to-server-pages/">Index </a>|| >;>;<a href="https://dineshonjava.com/setproperty-tag-in-jsp/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jsp-include-tag-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/setproperty-tag-in-jsp/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '338'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…