JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

Struts BEAN Cookie Tag ( < bean:cookie >)

September 2, 2010 by Krishna Srinivasan Leave a Comment

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

[code lang=”html”]<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>[/code]

< bean:cookie >

< bean:cookie > -It is used to define a scripting variable based on the value(s) of the specified request cookie. It retrieves the value of the specified cookie (single or multiple) and define the result as a page scope attribute of type cookie or cookie[] if multiple are specified.

Example Code for < bean:cookie >

1.Create an Jsp page and name it as BeanCookieTag.jsp.In this example we retrieve the cookie object as a bean which has been added in the action class before this page is invoked.Displays each property of specified cookie bean.

BeanCookieTag.jsp

[code lang=”html”]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
< head>

< title> Struts Bean Cookie Tag </title>
< head>
< body bgcolor="#DDDDDD">
< h1 > bean:cookie tag example </h1>

< h2 > Cookie Property names and their values </h2>

< bean:cookie id="cookie" name="name"/>

< h3> Cookie Name:</h3>
< bean:write name="cookie" property="name"/>
< h3> Cookie Value:</h3>
< bean:write name="cookie" property="value"/>
< h3> Cookie Max-Age:</h3>
< bean:write name="cookie" property="maxAge"/>
< h3> Cookie Secure:</h3>
< bean:write name="cookie" property="secure"/>
</body>
</html>
[/code]

2.Simple Action class BeanCookieAction.java which is a sub class of Action class used to process the user’s request.In this class we create a cookie object and add it to the response.
BeanCookieAction.java
[code lang=”java”]
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 BeanCookieAction 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 {
Cookie ck=new Cookie("name","Java Struts");
response.addCookie(ck);
return mapping.findForward(SUCCESS);
}
}

[/code]

3.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.
[code lang=”xml”]

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

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

< struts-config>

< action-mappings>
< action path="/cookie" type="com.myapp.struts.BeanCookieAction">
< forward name="success" path="/BeanCookieTag.jsp"/>

</action>
</action-mappings>

</struts-config>

[/code]

4.Modify index.jsp which conatins the jsp forward to the action class .
[code lang=”html”]

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

< jsp:forward page="cookie.do"/>

[/code]

5.Building and running the application

Output

Access page:http://localhost:8080/beancookie/

also read:

  • Struts 2.0 Introduction and Validations using Annotations
  • Introduction to Struts Actions
  • What’s new in Struts 2.0?
  • Internationalization and Taglibs in Struts 1.2

Filed Under: Struts Tagged With: Struts Tags

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved