Struts BEAN Size and Header Tag ( < bean:size > and < bean:header >)

Struts BEAN Tag Library

Struts BEAN tag library provides tags which are usefull in accessing beans and their properties as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Syntax to use Struts BEAN tag library

<%@ taglib prefix="bean" uri="" %>

< bean:size > and < bean:header>

< bean:size > -This tag defines a new bean containing the number of elements in the specified Collection or Map.Collection size can be counted in any way as a runtime expression specified as the value of the collection attribute or by JSP bean specified by the name attribute or as specified by property of the bean by the property attribute with respect to given name attribute.
< bean:header > – It is used to define a scripting variable based on the values of the specified request header. It retrieves the specified request header and define the result as the page scope attribute of type String or String[]

Example Code for < bean:size > and < bean:header>

1.Create an Jsp page and name it as BeanSizeTag.jsp.It displays the size of the collection specified in the name and property attributes of the < bean:size > tag. Displays the header information of the page using < bean:header> tag in this example we define two request header values (user-agent,host) and make available as the page context attribute and display.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="" %>
<%@taglib prefix="bean" uri="" %>

< html>
    < head>

        < title> Bean Size and Header Tag</title>
    < head>
    < body bgcolor="#DDDDDD">
        < h1 > bean:size and bean:header tag example </h1>

		   < bean:size id="beansize" name="BeanSizeForm" property="list"/>

            < h3> Size of the Collection in the Form Bean is:</h3>
			< bean:write name="beansize" /> < br>

		  < h2> Some of Header Object's values</h2>
		   < h3> user agent:</h3>
		    < bean:header id="header_agent" name="user-agent"/>
			 < bean:write name="header_agent"/>
			  < h3> localhost:</h3>
		    < bean:header id="header_host" name="host"/>
			 < bean:write name="header_host"/>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.It contains a list of type ArrayList which contains the elements of Integer objects.In this example list is initialized with numbers from 1 to 10 of Integer objects.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class BeanSizeForm extends org.apache.struts.action.ActionForm {

private ArrayList&lt; Integer&gt; list=new ArrayList&lt; Integer&gt;();

    public ArrayList&lt; Integer&gt; getList() {
        return list;

    public void setList() {
        for(int i=0;i&lt; 10;i++)
        this.list.add(i) ;

    public BeanSizeForm() {



3.Simple Action class which is a sub class of Action class used to process the user’s request.In this class we set the list in the ActionForm bean.

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class BeanSizeAction extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       BeanSizeForm formBean=(BeanSizeForm)form;
        return mapping.findForward(SUCCESS);

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

< struts-config>
    < form-beans>
        < form-bean name="BeanSizeForm" type="com.myapp.struts.BeanSizeForm"/>

< action-mappings>
        < action name="BeanSizeForm" path="/size" scope="request" type="com.myapp.struts.BeanSizeAction" validate="false">
        < forward name="success" path="/BeanSizeTag.jsp"/>



5.Modify index.jsp which conatins the jsp forward to the action class .

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page=""/>

6.Building and running the application
Access page:]http://localhost:8080/beansize/

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!