0

I have recently upgraded my code from Struts 1 to Struts 2 and the app is not working after deploying it in my test environment (Linux box). The test env has 3 server instances with different url for each instance. I have deployed my new code(Struts 2) in instance#2 and instance#1 and #3 has old code(Struts 1)

The problem is once i login to the url's of Instance 1 and 3, I am successfully able to login to Instance #2. But when I login to Instance #2 url directly, struts 2 action is not being invoked and stays in login page itself

web.xml

    <!-- Note how the Application Security Team's security filter is listed 
        FIRST! -->
    <filter-name>AppSecSecurityFilter</filter-name>
    <filter-class>com.qwest.appsec.TomcatSecurityFilter</filter-class>
    <!-- Required. The name for this application -->
    <init-param>
        <param-name>applicationName</param-name>
        <param-value>NATE</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>AppSecSecurityFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping>
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/does_not_exist_jaasinit.html</form-login-page>
        <form-error-page>/appsec/access_denied_en.html</form-error-page>
    </form-login-config>
</login-config><filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping><session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
</welcome-file-list>

Struts.xml:

<struts><package name="loginPackage" namespace="/" extends="struts-default">

    <action name="nateLoginAction" class="com.wireless.nate.actions.LoginAction">
        <result name="success">creditNate.jsp</result>           
        <result name="error">login.jsp </result>           
         <result name="input">login.jsp</result>          
    </action>
</package></struts>   

LoginAction.java

 public class LoginAction extends ActionSupport implements 
 ServletRequestAware,ServletResponseAware
 {

private static final long serialVersionUID = -3510995405804328464L;
private Logger logger = Logger.getLogger(this.getClass());  
HttpServletRequest request;
HttpServletResponse response;
LoginActionForm loginActionForm;
ActionContext context;
ActionSupport actionSupport;  



public LoginActionForm getLoginActionForm() {
    return loginActionForm;
}

public void setLoginActionForm(LoginActionForm loginActionForm) {
    this.loginActionForm = loginActionForm;
}




@Override
public void setServletResponse(HttpServletResponse response) {
    this.response=response; 
}

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request=request;

}
public String execute() throws Exception
{
  System.out.println("inside action execute method");
  logger.debug("+execute()");

  ValueStack stack = context.getValueStack(); 
  Map<String, Object> context = new HashMap<String, Object>();   


// Get the html form fields from the cookies
String salesCode = "";
String loginUserId = "";
javax.servlet.http.Cookie[] cookies = request.getCookies();
javax.servlet.http.Cookie thisCookie = null;




    if (null != cookies)
    {
      for (int i = 0; i < cookies.length; i++)
      {
        thisCookie = cookies[i];
        logger.debug("request.getCookies():");
        logger.debug("  cookies[" + i + "].getName()=" + cookies[i].getName());
        logger.debug("  cookies[" + i + "].getValue()=" + cookies[i].getValue());

        if (thisCookie.getName().equals("salesCode"))
        {
          salesCode = cookies[i].getValue();
        }
        else if (thisCookie.getName().equals("user"))
        {
          loginUserId = cookies[i].getValue();
        }
      }
    }
    loginActionForm.setSalesCode(salesCode.toUpperCase());
    loginActionForm.setUser(loginUserId);

   context.put("loginActionForm", loginActionForm);
    stack.push(context);       

    return SUCCESS;

   } 

   public void validate(){

    System.out.println("inside action validate method");
    context =  ActionContext.getContext();
    actionSupport=(ActionSupport)context.getActionInvocation().getAction();

          if(loginActionForm.getUser() == null || loginActionForm.getUser().length() == 0){
              addFieldError("user.required","User name is required");
          }
          if(loginActionForm.getPassword() == null || loginActionForm.getPassword().length() ==0){
              addFieldError("password.required","Password is required");
          }


    }

 }
sopna
  • 1
  • 1
  • Are you... trying to re-use your S1 `ActionForm` classes?! What's the extra code for in `validate`? What makes you think the `validate` process happens before `execute`? I think you should take a step back and understand the S2 request flow. – Dave Newton Jul 18 '19 at 13:55
  • Yes I have reused my ActionForm classes. I faced issues in Validator framework hence I used the validate() method for userid/password check. The issue here is validate() or execute() never gets called whenever I hit the url directly. Only when I hit the Instance#1 or #3 url where struts1 code is deployed then it works and at that time I have noticed validate() gets called first and then execute() – sopna Jul 18 '19 at 14:11
  • Why would the S2 action be hit if you're accessing the S1 apps? In any case, `validate` is called first because of how S2 request processing works. – Dave Newton Jul 18 '19 at 14:16
  • No S1 apps are not hitting S2 actions. Once I login to S1 apps it hits the S1 actions only . Until that session is active, I am able to access my S2 apps which hits S2 action.If i don't login to S1 or when i logout S1 app, then S2 apps are also not logging in and s2 actions are not invoking . I know it may sound confusing but the issue is until S1 apps sessions are active I am able to access S2 app – sopna Jul 18 '19 at 14:35
  • Then you're either (a) using a load balancer that's trying to share sessions, or (b) you're trying to use a session in the S2 app that's being created by the S1 app, or (c) ... other. You need to provide more detail, otherwise there's nothing anyone can do. – Dave Newton Jul 18 '19 at 14:38
  • It's not the same session which could be shared with Struts 2 web application if so the let the interceptor know about it. See https://stackoverflow.com/a/20629671/573032 – Roman C Jul 22 '19 at 12:17

0 Answers0