title: Struts2: How to pass a values from one action to another action. Or from one JSP to another JSP. Without using session
body: <p>Hi I am building a project using Struts2. However during the development I am facing many issues. The biggest issue is Session management in Struts2. There is very limited information related to this.</p>
  
  <p>I have hunt for the solution to my problem but got only limited information that does not satisfy my needs.</p>
  
  <p>I do not wish to use container's session (Apache Tomcat 7) as it will become an overhead when multiple user would try to use the application.</p>
  
  <p>I have created 2 Action classes and 2 JSPs.
  The values entered in JSP1 (Login.jsp and LoginAction.java POJO) should be used in another action (HomeAction.java POJO) and later they should display in subsequent JSPs (Home.jsp).</p>
  
  <p>I am using tiles as well.</p>
  
  <p>I have tried using  tag but I cannot able to set a value to it.</p>
  
  <p>In Struts2,
  How could I pass an object on a JSP1/Action to another Action/JSP2 ?</p>
  
  <p>A small Login page example would be appreciated.
  e.g. A username entered in a text field should display to another JSP page using 2 different Action classes.</p>
  
  <p><strong>struts.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"&gt;
  
  &lt;struts&gt;
      &lt;constant name="struts.devMode" value="true" /&gt;
      &lt;constant name="struts.custom.i18n.resources" value="ApplicationResources" /&gt; 
  
      &lt;package name="default" extends="struts-default"&gt;
          &lt;result-types&gt;
              &lt;result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /&gt;
          &lt;/result-types&gt; 
  
          &lt;action name="login" class="org.myaction.LoginAction" method="validateLoginCredentials"&gt;
              &lt;result name="autheticatedUser" type="redirect" &gt;homePage&lt;/result&gt;
              &lt;result name="fail" &gt;/jsp/Login.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="homePage" class="org.myaction.HomeAction" method="execute"&gt;
              &lt;result name="responseOK" type="tiles" &gt;/tile.NextPage&lt;/result&gt;
              &lt;result name="error" type="tiles" &gt;/tile.HomePage&lt;/result&gt;
          &lt;/action&gt;
      &lt;/package&gt;
  &lt;/struts&gt;
  </code></pre>
  
  <p><strong>LoginAction.java</strong></p>
  
  <pre><code>    public class LoginAction extends ActionSupport {
  
      //private static final String SUCCESS = "success"; 
      private static final String FAIL = "fail";
  
      private String userName;
      private String password;
  
      public String getUserName() { return userName; }
  
      public void setUserName(String userName) { this.userName = userName; }
  
      public String getPassword() { return password; }
  
      public void setPassword(String password) { this.password = password; }
  
      public String validateLoginCredentials() {
  
            if(this.userName.equals("admin") 
                        &amp;&amp; this.password.equals("allow")) {
  
           return "autheticatedUser";
                }
          addActionError(getText("error.login"));
          return FAIL;
      }
  }
  </code></pre>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0"&gt;
  
      &lt;display-name&gt;Login&lt;/display-name&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/tiles.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.apache.struts2.tiles.StrutsTilesListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;/jsp/Login.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;struts2&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>My question is, 
  In Struts2,
  How could I pass the <strong>userName</strong> from <strong>Login.jsp/LoginAction.java</strong> to <strong>Home.jsp/HomeAction.java</strong> ? </p>
  
  <p>Note: Storing into session is not preferred option but could use if there is no other way to maintain the user state in Struts2.</p>
  
  <p>A small Login page example would be appreciated.
  e.g. A username entered in a text field should display to another JSP page using 2 different Action classes.</p>
  
  <p><strong>Edited</strong></p>
  
  <p>Hi thank you! I really appreciate your reply. I presume this would work. There is one more thing I forgot to mention. 
  An another class(JavaBean) property is present in LoginAction.java class. </p>
  
  <pre><code>public class LoginAction extends ActionSupport {
  
     private UserData userData;
  
     public void setUserData(UserData userData) {
        this.userData = userData;
     }
  
     public String getUserData() {
        return userData;
     }
  
  }
  </code></pre>
  
  <p>UserData.java</p>
  
  <pre><code>public class UserData {
     private String userName;
  
     public void setUserName(String userName) {
        this.userName = userName;
     }
  
     public String getUserName() {
        return userName;
     }
  }
  </code></pre>
  
  <p>how can I set the userName property of UserData object which is in
  LoginAction.java ?</p>
  
  <p>I've tried this</p>
  
  <pre><code>&lt;s:hidden name="userData.userName" value="userName" /&gt;
  </code></pre>
  
  <p>but it's not working....</p>
  
  <p>Do you know what mistake I am making here?</p>