Example on ApplicationAware Interface of struts 2

Struts » on Oct 24, 2011 { 8 Comments } By Sivateja

So ApplicationAware interface, we need to implement our Action class from ApplicationAware interface when ever our Action class need to get context behavior, means we can share our data across all the files of the web application by putting in a global object that’s context 🙂

When we implement our Action class from ApplicationAware interface then the controller doesn’t inject exactly servlet context object, instead it will injects a map object and this will created once by the controller and the same object will be injected to all files of the struts application.

Example on struts 2 ApplicationAware

files we used…

  • index.jsp
  • error.jsp
  • success.jsp
  • [ in java4s package ]
  • web.xml [ in web-inf ]
  • struts.xml [ in web-inf/classes folder ]

Directory Structure


<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="verify">

    <s:textfield name="uname" label="Enter Username" /><br>
    <s:textfield name="age" label="Enter Age" /><br>
    <s:textfield name="country" label="Enter Country" /><br>

    <s:submit value="Click" align="center" /> 



<%@ taglib prefix="s" uri="/struts-tags" %>

Hello <s:property value="#application.a" />
Your age is:  <s:property value="#application.b" />
country:  <s:property value="#application.c" />

package java4s;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.*;
import java.util.Map;

public class LogingEx extends ActionSupport implements ApplicationAware{
	private static final long serialVersionUID = 1L;

	private String uname,country;
	private int age;
	Map m;

	public String getUname() {
		return uname;

	public void setUname(String uname) {
		this.uname = uname;

	public String getCountry() {
		return country;

	public void setCountry(String country) { = country;

	public int getAge() {
		return age;

	public void setAge(int age) {
		this.age = age;

	public void setApplication(Map m)

	public String execute()
		m.put("b", age);

		return SUCCESS;



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


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

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    <include file="struts-default.xml"/>
    <package name="a" extends="struts-default">
        <action name="verify" class="java4s.LogingEx">
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>

After Execution



You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


8 Responses to “Example on ApplicationAware Interface of struts 2”
  1. Mohammed Vaseem says:

    Am getting exception in this program. Usually the exception messages will be displayed on the console and as well as on the browser. But am seeing this first time that, the exception messages are displaying on the browser but in console screen alike messages are displayed. Please provide the reason.

    When I was tracing the program, I got to know exception was coming at line 44 of program.

    The browser exception messages are
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)

  2. Java4s says:


    Its working fine from my end,any ways try these…

    – Make sure you download the program [ Click on Download button ]
    – If you copy the source code from the web page, while pasting the code it will prints some other characters in place of quotes.
    – If you copy, just remove all the double quotes and insert again manually.

    try finally..!!

  3. Abhishek says:

    Hey when i run this demo it will not show proprety value of my successfull pgae please help me where i am getting wrong in this

  4. Java4s says:


    Make sure you are writing #application.keys in your map object.

    Download the program and check again and lemme know.

  5. sanjeev kumar says:

    what is Value stack, Action context,and Action Invocation in struts2?

  6. Raghu says:


    This Program is very useful to get knowledge about applicationaware interface.But what is the difference between applicationaware and session?

    1.Can i use applicationaware instead of session to store User infomation on login session?

  7. asif says:

    Thanqqqqqqqqqqqqqqqqq its simply supeb to learn

  8. Lova Chittumuri says:

    Thank you… nice..!


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.