JSP UseBean Action

JSP UseBean Action Element

  • All the JSP action elements are used during the request processing phase, and not as the directive elements and script elements, which are used during the translation phase.
  • All JSP action elements are specified in the form of XML tags.
  • The JSP usebean action makes a JavaBeans component available in a page
  • You can not use a JSP action element in an XML directive as these have no end tag.
  • Here is the notation for the JSP useBean action element:
    XML Description
    <jsp:usebean attributes > content </jsp:useBean> The content can bee anything.
  • The following table lists the attribute(s) for the JSP useBean action element:
    Attribute Valid values Default Description
    id Unique name n/a Unique name used to access the bean in the rest of the page. This is a mandatory attribute.
    scope page, request, session, or application page The scope of the bean (see table below).
    class Class name n/a The class name must include package specification. This is an optional attribute, but either the class or type attribute (or both) must be specified.
    beanName Class name n/a The name of a bean, as expected by the instantiate() method of the java.beans.Beans class. Most often you will use the class attribute, rather than beanName.
    type Interface name or Class name n/a The type to be used for the variable that references the bean. This follows Java rules, so it can be the class of the bean, any parent class of the bean, or any interface implemented by the bean or by a parent class
    Comments on scope:
    Scope Comments
    page Most restrictive. The object is only accessible within the page in which it is defined. Saved inside the Page context Object.
    request Available for the life of the specific request (within pages to which the request is forwarded or included). These objects are thread-safe. Saved in Servlet Request Object.
    session Session scope is saved in HTTP Session Object and it can be available or accessible through the entire session. These objects are NOT thread-safe
    application Application scope is stored in Servlet context object and it can be accessible across the entire application. These objects are NOT thread-safe

Example of using JSP useBean action element.

In the example we use Netbeans IDE and Glassfish Server.

You can download this example here (needed tools can be found in the right menu on this page).

If you like to participate in the review of this example you must first create a Web project in Netbeans (the project name is UseBeanAction).

In this example, we will add two files, header.jsp and footer.jsp.

  • It is customary to place all include files, which are used several times in the WEB-INF folder. Some would also like the include files should have the extension .jspf, but this is not a requirement.
    Here is the file we want to include at the top of all pages:
    <h2 class="siteblue">Short presidential list</h2>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Header.jsp and folder should be WEB-INF).

    Here is the file we want to include at the bottom of all pages:
    <hr >
    <span class="siteblue">Listing date <%= (new java.util.Date()).toString() %></span>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is Footer.jsp and folder should be WEB-INF).

In this example, we will add two java files, President.java and PresidentControl.java.

  • We need a java class with information about each President that we want to list through a PresidentControl class.
    Here is the President.java:
    package app;
    public class President {
      private int presidentNr;
      private String presinentImage;
      private int bornYear;
      private String name;
      private int fromYear;
      private int toYear;
      private String party;
      public President(int presidentNr, String presinentImage, 
              int bornYear, String name, int fromYear, int toYear, String party) {
        this.presidentNr = presidentNr;
        this.presinentImage = presinentImage;
        this.bornYear = bornYear;
        this.name = name;
        this.fromYear = fromYear;
        this.toYear = toYear;
        this.party = party;
      public int getPresidentNr() {
        return presidentNr;
      public String getPresinentImage() {
        return presinentImage;
      public int getBornYear() {
        return bornYear;
      public String getName() {
        return name;
      public int getFromYear() {
        return fromYear;
      public int getToYear() {
        return toYear;
      public String getParty() {
        return party;

    For those who participate in the review: create a java class in Netbeans and replace generated code for the java file with that shown above (the java file name is President and package should be app).

  • We want to list some related informations of each president with help of the PresidentControl class.
  • We need a Java class, PresidentControl.java, that handles the existence of each President in a list and can give us the list when we need it in our JSP.
    Here is the PresidentControl.java:
    package app;
    import java.util.ArrayList;
    import java.util.List;
    public class PresidentControl {
        private List<President>  presidents= new ArrayList<President>();
      public PresidentControl() {
        presidents.add(new President(40,"images/RReagen.png",1911,
                "Ronald Reagan",1981,1989,"Republican"));
        presidents.add(new President(41,"images/GHWBush.png",1924,
                "George H. W. Bush",1989,1993,"Republican"));
        presidents.add(new President(42,"images/BClinton.png",1946,
                "Bill Clinton",1993,2001,"Democratic"));
        presidents.add(new President(43,"images/GWBush.png",1946,
                "George W. Bush",2001,2009,"Republican"));
        presidents.add(new President(44,"images/BObama.png",1961,
                "Barack Obama",2009,2017,"Democratic"));    
      public List<President> getPresidents() {
        return presidents;

    For those who participate in the review: create a java class in Netbeans and replace generated code for the java file with that shown above (the java file name is PresidentControl and package should be app).

  • In this class we have a member variable, presidents, which shall contain all the registered presidents at current time. This is instantiated when an instance of the class is created. We have also added some presidents in the default constructor.
  • We have created a method that returns a list of all the registered presidents at current time.
  • All the presidential photos we need so far must be placed under the web/images directory. You can download the pictures of all the presidents here and extract them to the web directory of the project.

Then we need a JSP file to demonstrate how we use the JSP useBean action element.

  • Here is the main JSP file:
    <%@page contentType="text/html" pageEncoding="UTF-8" import="app.*"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Country Liste</title>
          .gradientdown {
            background:  #e0e6ff;
            background: -webkit-linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* Chrome10+,Safari5.1+ */
            background: -o-linear-gradient(#e0e6ff, #eeeeee); /* For Opera 11.1 to 12.0 */
            background: -moz-linear-gradient(top, #e0e6ff, #eeeeee); /* For Firefox 3.6 to 15 */
            background: linear-gradient(top, #e0e6ff 0%,#eeeeee 100%); /* W3C Standard syntax (must be last) */
          table tr.data:nth-of-type(even) { 
            background: #eeeeee; 
          table { 
            border-color: #aad5ff;
            border-style: solid;
            border-width: 0 0 1px 1px;
            border-spacing: 0;
            border-collapse: collapse;
          tr,th,td {
            border-color: #aad5ff;
            border-style: solid;
            padding: 2px;
            border-width: 1px 1px 0 0;
            vertical-align: central;  
          table tr th {
            color: blue;
          hr { background-color: #3366ff; height: 1px; border: 0; }
          .siteblue {
            color: #3366ff;
        <div style="max-width:530px; ">
          <%@ include file="WEB-INF/header.jsp" %>
          <table >
            <tr class="gradientdown">
              <th style="width:60px;"><br /></th>
              <th style="width:50px;">Pres.No.</th>
              <th style="width:160px;">Name</th>
              <th style="width:60px;">Born</th>
              <th style="width:100px;">Presidency</th>
              <th style="width:100px;">Party</th>
            <jsp:useBean id="presidentControl" scope="page" class="app.PresidentControl"/>
            <c:forEach items="${presidentControl.presidents}" var="president" >
              <tr class="data">
                <td style="text-align: center;">
                  <img src="${president.presinentImage}" width="60" height="75"
                       alt="${president.name}" /></td>
                <td style="text-align: center;">${president.presidentNr}</td>
                <td style="text-align: center;">${president.bornYear}</td>
                <td style="text-align: center;">${president.fromYear}-${president.toYear}</td>
          <%@ include file="WEB-INF/footer.jsp" %>

    For those who participate in the review: create a JSP file in Netbeans and replace generated code for the JSP with that shown above (the JSP file name is index).

How does this work?

  • We have created two JSP files, Header.jsp and Footer,jsp, which we have included in the JSP document, index.jsp.
  • We create an instance of President Control class with the help of a JSP useBean, which have a page scope.
  • Because we want to use the PresidentControl class and the President class, we need to import these classes with the page directive.
  • We use the standard core taglibrary from JSLT to loop through the presidents that exists in the presidentControl presidents property.
  • With help of EL we can print the informations of each president.

Creating Deployment descriptor.

  • To run this JSP you have to deploy it to a web-server or a Application server. To deploy means to install the JSP with some instruction to a such server.
  • The instructions are mainly defined to be deployment descriptors. The standard part of the deployment descriptor should be in an XML-file with the name web.xml.

    You may need to create a Deployment descriptor file, web.xml in Netbeans.

  • The contents of the web.xml file should look like this:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  • This file starts with the normal xml tag for a XML file and the root tag for the deployment descriptor is web-app. Every ting inside the last tag is to tell the server about our application, which in this case is a JSP file.
  • With a servlet tag we give the JSP file a servlet name, which is used in the servlet-mapping tag to specify a url for the JSP file.
  • In this way we can have many urls for the same JSP file.
  • If no session-timeout (the server ends the service of the application after this time) is given a standard timeout for the server is used as timeout for the application.
  • The welcome-file tag specifies the startup for our application, which in this case and our application is the welcome file with url presidentList. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /UseBeanAction) for the application will in most cases be specified by a server vendor deployment descriptor.

    For those who participate in the review: create a Glassfish deployment descriptor in Netbeans.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" 
    <sun-web-app error-url="">
      <class-loader delegate="true"/>
        <property name="keepgenerated" value="true">
            Keep a copy of the generated servlet class' java code.

Run the application.

  • For those who participate in the review: right click the Web-project and select Run.
  • This results in that the application will be deployed to the server and started.
  • The browser will display:
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.