JSP SetProperty 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 XML tags and can not be used in an XML directive as these have no end tag
  • The JSP setProperty action element sets the values of properties in a bean.
  • There are two basic ways to use the setProperty action:
    1. <:jsp:setProperty> can appear is inside the body of a jsp:useBean element. In this case it executed only if a new object was instantiated, not if an existing one was found.
    2. <:jsp:setProperty> can appear is outside and after a jsp:useBean element. In this case it executed on a new object or any found existing bean created earlier.
  • Here is the notation for the JSP setProperty action element:
    XML Description
    <jsp:setProperty attributes /> The setProperty action element cannot have any body part.
  • The following table lists the attribute(s) for the JSP setProperty action element:
    Attribute Description
    name Designates the bean (id) whose property will be set. The Bean must have been previously defined.
    property Indicates the property you want to set. A value of "*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods.
    value The value that is to be assigned to the given property. If the parameter's value is null, or the parameter does not exist, the setProperty action is ignored.
    param The param attribute is the name of the request parameter whose value the property is to receive. You can't use both value and param, but it is permissible to use neither.
  • The propertyExpression can be any of the following:
    forms Description
    property="*" The * setting tells the tag to iterate through the request parameters for the page, setting any values for properties in the bean whose names match parameter names.
    property="propertyName" Omitting value and param attributes for a property assumes that the bean property and request parameter name match.
    property="propertyName" param="parameterName" The param attribute specifies the parameter name to use in setting this property.
    property="propertyName" value="propertyValue" The value attribute can be any run-time expression as long as it evaluates to a String. The value attribute String can be automatically cast to boolean, byte, char, double, int, float, long, and their class equivalents. Other casts will have to be handled explicitly in the bean's setPropertyName() method.
  • Here is example of two elements set a property from a value:
    <jsp:setProperty name="results" property="col" value="${i mod 4}"/>
    <jsp:setProperty name="results" property="row" value="<%= i/4 %>" />
    As you can see we can use EL-expression or Expression scriplet for the value.

Example of using JSP setProperty 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 SetPropertyAction).

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:
    <h1>Book inventory control</h1>

    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:
    Status on <%= (new java.util.Date()).toString() %>

    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, Book.java and Library.java.

  • We need a java class with information about each book in a library.
    Here is the Book.java:
    package doc;
    public class Book {
      private String isbn_no;
      private String short_desc;
      private double price;
      public Book() {
            this.isbn_no = "";
        this.short_desc = "";
        this.price =0;
      public Book(String isbn_no, String short_desc, double price) {
        this.isbn_no = isbn_no;
        this.short_desc = short_desc;
        this.price = price;
      public String getIsbn_no() {
        return isbn_no;
      public String getShort_desc() {
        return short_desc;
      public double getPrice() {
        return price;
      public void setIsbn_no(String isbn_no) {
        this.isbn_no = isbn_no;
      public void setShort_desc(String short_desc) {
        this.short_desc = short_desc;
      public void setPrice(double price) {
        this.price = price;

    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 Book and package should be doc).

  • We want to register isbn number, description and price of each book we have in our library (Perhaps not the most suitable properties for a book in a library, but anyway ...).
  • We need a java class to control what books we have in our library.
    Here is the Library.java:
    package doc;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.regex.Pattern;
    public final class Library {
      private List<Book> books;
      public Library() {
        books = new ArrayList<Book>();
        setAddBook(new Book("0201703092", "The Practical SQL", 39));
        setAddBook(new Book("0471777781", "Professional Ajax", 32));
        setAddBook(new Book("0764557599", "Professional C#", 42));
      public List<Book> getBooks() {
        return books;
      public void setAddBook(Book book) {
      public void setDelBook(String[] isbns) {
        if (isbns != null) {
          for (String isbn : isbns) {
            for (Book book : books) {
              if (book.getIsbn_no().equals(isbn)) {
      public boolean Validate(String test){
        boolean result=true;
        String decimalPattern = "[0-9]+(\\.[0-9][0-9]*)*";
        result=Pattern.matches(decimalPattern, test);
        return result;

    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 Library and package should be doc).

  • In this class we have a member variable, books, which shall contain all the books in the library. This is instantiated in the default constructor. We've also added some books in this constructor.
  • Moreover, we have a method to add new books and a method of removing books from the library.
  • Finally, we have created a method that returns a list of all the books in the library.

At last we need a JSP file(s) to demonstrate how we use the JSP setProperty action Element.

  • Here is the main JSP file:
    <%@page import="java.util.regex.Pattern"%>
    <%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.*, doc.*" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <!DOCTYPE html >
        <title>Library Book Control</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(odd) { 
            background: #eeeeee; 
          input[type=text] {
            width: 150px;
            border:2px solid #ccc; 
            -webkit-border-radius: 5px;
            border-radius: 5px;
            background-color: lemonchiffon;
          input[type=text]:focus {
          input[type=submit] {
            width: 166px;
            -webkit-border-radius: 5px;
            border-radius: 5px; 
            border: outset #333 2px;
            margin-top: 10px;
        <div style="max-width:400px; ">
          <%@ include file="/WEB-INF/header.jsp" %>
          <jsp:useBean id="library" class="doc.Library" scope="session"/>
          <jsp:useBean id="newBook" class="doc.Book" scope="page"/>
          <!-- adding a book to the Library -->
          <c:if test="${param.action == 'add'}">
            <jsp:setProperty name="newBook" property="isbn_no" />
    <%-- OR 
           <jsp:setProperty name="newBook" property="isbn_no" 
            <jsp:setProperty name="newBook" property="short_desc"/>
            <c:if test="${library.Validate(param.price)}">
              <jsp:setProperty name="newBook" property="price"/>
            <jsp:setProperty name="library" property="addBook" 
                             value="${newBook}" />
    <%-- OR        
            <jsp:setProperty name="library" property="addBook"
                             value="<%= newBook%>" />
          <!-- deleting a book from the Library -->
          <c:if test="${param.action == 'del'}">
            <jsp:setProperty name="library" property="delBook" 
                             value="${paramValues.isbns}" />
    <%-- OR
          <jsp:setProperty name="library" property="delBook" 
                           value="<%= request.getParameterValues("isbns")%>" />  
          <!-- List all books in the Library -->
          <h3>Library book list</h3>
          <form action='index.jsp' method="POST">
            <table  >
              <tr class="gradientdown">
                <th style="width:100px;">Isbn_no</th> 
                <th style="width:150px;">Short desc</th> 
                <th style="width:90px;">Price</th> 
                <th style="width:60px;">Del.</th>
              <c:forEach items="${library.books}" var="listBook" >
                <tr class="data" >
                  <td><jsp:getProperty name="listBook" property="isbn_no"/></td>
                  <td><jsp:getProperty name="listBook" property="short_desc"/></td>
                  <td><jsp:getProperty name="listBook" property="price"/></td>
                  <td style="text-align: center;"><input type="checkbox" 
                      name="isbns" value="" /></td>
            <input type="hidden" name="action" value="del"/>
            <input type="submit"  value="Delete book(s)" />
          <!-- Input form for a new book -->
          <h3>Add a book to Library</h3>
          <form action='index.jsp' method="POST">
                <td><input type="text" name="isbn_no" placeholder="Isbn number" /></td> 
                <td><input type="text" name="short_desc" placeholder="Short desc."/></td> 
                <td><input type="text" name="price" placeholder="Price" /></td> 
            <input type="hidden" name="action" value="add"/>
            <input type="submit"  value="Add book" />
          <%@ 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 index.jsp file with JSP include action element.
  • With the useBean action element we have created an instance of our library and an instance for a new book to be added. The library is made session related while the new book is only related to the life of the page.
  • With use of the JSTL core and EL syntax we test for one of two action, delete on or more books from the library or add a new book to the library. We use the setProperty action to handle both of those two action.
  • Because we want to use the library class and the book class, we need to import these classes with the page directive (doc.*).
  • When any delete or add is finish we uses JSTL core and EL syntax to list all the books that exists in the library with the possibility to delete any of them.
  • At last we have created a form to add a new book to the library.

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 BookControl. Reorganize the welcome-file-list to what is shown above.

Creating Web-server Deployment descriptor.

  • The context-root (in example /SetPropertyAction) 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:

Another JSP attribute action example, you'll find under JSP Element action.

© 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.