Using c:set taglib to set property of object stored in scope variable

Previous example, we say how to use <c:set/> to set primitive value in scope variables and attributes that can be used in this taglib. In this example, we will see the way to set property of object stored in scope variable.

Tools used:

  • jstl 1.2

We have an pojo class to store name of a person named Person. It contains two fields “firstName” and “lastName” and its setters and getter. Following is the code of the pojo class:


package com.raistudies.domain;

public class Person {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return firstName + " " +lastName;
    }

}

We will store instance of Person class in request scope in a servlet and then will forward the request to jsp page where we will use <c:set/> taglib to set value of properties “firstName” and “lastName”.

Code of Servlet that stores instance of Person class in request scope and forward to jsp:


package com.raistudies.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.raistudies.domain.Person;

/**
* Servlet implementation class HomeServlet
*/
public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        Person p = new Person();
        request.setAttribute("owner", p);
        getServletContext().getRequestDispatcher("/Home.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
    }

}

Jsp code of the example:


<%@page import="com.raistudies.domain.Person"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Example of c:set and c:remove taglibs</title>
    </head>
    <body>
    Setting value of first name property in owner<c:set property="lastName" value="Mondal" target="${owner}"/><br>
    Setting value of last name property in owner<c:set property="firstName" value="Rahul" target="${owner}"/><br>

    Now, owner value is : <b><c:out value="${owner}"/></b>

    </body>
</html>

In our jsp you can get two new things that are used to set the property value in object stored in servlet:

  1. <%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%> : We have registered the core jstl with the prefix “c” to use.
  2. <c:set property=”lastName” value=”Mondal” target=”${owner}”/> : In this line we have store the value “Mondal” in property “lastName” of object of Person which is stored in scope variable owner by specifying  target=”${owner}”.

While running the example in Tomcat 6 server, we get the following output:

Setting property value in object stored in scope variable

Setting property value in object stored in scope variable

You can try the example by yourself, download the example from bellow link and import in eclipse to modify and try:

Source + lib : Download

Related Posts:

Leave a comment ?

2 Comments.

  1. I am new in struts and found your website very useful and informative. Can you give some examples for add/edit and update records by using mysql

    Thanks in advance
    MWS

Leave a Comment Cancel reply

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>