Wednesday 30 March 2016

JSP:USEBEAN ACTION TAG

The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.

Syntax of jsp:useBean action tag


  1. <jsp:useBean id= "instanceName" scope= "page | request | session | application"   
  2. class"packageName.className" type= "packageName.className"  
  3. beanName="packageName.className | <%= expression >" >  
  4. </jsp:useBean>  

Attributes and Usage of jsp:useBean action tag

  1. id: is used to identify the bean in the specified scope.
  2. scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page.
    • page: specifies that you can use this bean within the JSP page. The default scope is page.
    • request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
    • session: 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.
    • application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.
  3. class: 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.
  4. type: 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.
  5. beanName: instantiates the bean using the java.beans.Beans.instantiate() method.

jsp:setProperty and jsp:getProperty action tags


The setProperty and getProperty action tags are used for developing web application with Java Bean. In web devlopment, bean class is mostly used because it is a reusable software component that represents data.
The jsp:setProperty action tag sets a property value or values in a bean using the setter method.

Displaying applet in JSP (jsp:plugin action tag)

The jsp:plugin action tag is used to embed applet in the jsp file. The jsp:plugin action tag downloads plugin at client side to execute an applet or bean.

Syntax of jsp:plugin action tag






  1. <jsp:plugin type"applet | bean" code"nameOfClassFile"   
  2. codebase"directoryNameOfClassFile"  
  3. </jsp:plugin>  


Example of displaying applet in JSP

In this example, we are simply displaying applet in jsp using the jsp:plugin tag. You must have MouseDrag.class file (an applet class file) in the current folder where jsp file resides. You may simply download this program that contains index.jsp, MouseDrag.java and MouseDrag.class files to run this application.

index.jsp






  1. <html>  
  2.     <head>  
  3.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  4.         <title>Mouse Drag</title>  
  5.     </head>  
  6.     <body bgcolor="khaki">  
  7. <h1>Mouse Drag Example</h1>  
  8.   
  9.  <jsp:plugin align="middle" height="500" width="500"  
  10.      type="applet"  code="MouseDrag.class" name="clock" codebase="."/>  
  11.   
  12.     </body>  
  13. </html>  

No comments:

Post a Comment