2

I send a request to the ActLand, then intercept(), if not logged in then redirect to Login.jsp.

The struts.xml:

<struts>        
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="login" class="com.interceptor.LoginInterceptor"/>
            <interceptor-stack name="loginStack">
                <interceptor-ref name="login"/>
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="loginStack"/>
        <global-results>
            <result name="loginRedirect" type="redirect">/login.jsp</result>
        </global-results>
        <action name="ActLand" class="com.action.ActLand">
            <result name="ok">/index.jsp</result>
        </action>
        <action name="ActLogin" class="com.action.ActLogin">
            <result name="ok">/index.jsp</result>
            <result name="fail">/login.jsp</result>
        </action>                     
    </package>
</struts>

The interceptor:

public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();    

        int userId = -1;
        if (session.get("userId") != null) {
            userId = Integer.parseInt(session.get("userId").toString());
        }

        Object action = invocation.getAction();
        if (userId < 0) {                
            return "loginRedirect";
        } else if (!(action instanceof ActLogin)) {
            return "loginRedirect";
        }
        return invocation.invoke();
}

The Error:

SEVERE: Could not execute action: /ActLand
java.lang.IllegalStateException
    at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:473)
    at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:138)
    at org.apache.struts2.dispatcher.ServletRedirectResult.sendRedirect(ServletRedirectResult.java:217)
    at org.apache.struts2.dispatcher.ServletRedirectResult.doExecute(ServletRedirectResult.java:197)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.components.ActionComponent.executeAction(ActionComponent.java:289)
    at org.apache.struts2.components.ActionComponent.end(ActionComponent.java:172)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at org.apache.jsp.index_jsp._jspx_meth_s_005faction_005f0(index_jsp.java:152)
    at org.apache.jsp.index_jsp._jspx_meth_s_005fif_005f0(index_jsp.java:120)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

I'm sorry, I believe I did every thing that I had been instructed to, but things are complicated...

Roman C
  • 49,761
  • 33
  • 66
  • 176
Eddie
  • 1,903
  • 2
  • 21
  • 46

2 Answers2

1

Add <interceptor-ref name="defaultStack"/> to the loginStack after login interceptor. It will help to solve the problem with redirect result and not only.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Still not working? don't know what happen... – Eddie May 17 '13 at 05:18
  • index.jsp is just a regular jsp, which has this tag in the first line. – Eddie May 17 '13 at 06:45
  • What result return the action named ActLand? – Roman C May 17 '13 at 06:49
  • in ActLand execute() method, I only return "ok". It load a List of Lands from a LandDAO, like: list = new LandDAO().loadAll(); return "ok"; – Eddie May 17 '13 at 06:52
  • It calls the action in the cycle. – Roman C May 17 '13 at 06:54
  • how can that happen? didn't the interceptor means to intercept an action to act? I thought like... before ActLand can do anything, the interceptor will does it works first, then stop actLand to do anything, redirect blah blah? Then what should I do properly? – Eddie May 17 '13 at 06:57
  • Remove s:action tag from JSP and replace it with servlet redirect or http meta refresh, remove results with location "/index.jsp". – Roman C May 17 '13 at 07:03
  • Hey~!! This work!! <% response.sendRedirect("ActLand"); %> And comment the result "ok" in struts.xml. But can the ActLand still working after that? – Eddie May 17 '13 at 07:11
  • No, it doesn't have result then you need to give it forward (default) result to the other jsp or `type="redirectAction"` result to action. – Roman C May 17 '13 at 07:15
  • Argg! How stupid I am!! I just solved it... the logic in interceptor is wrong... Really sorry... how dump :D Thank you very much... You helped a lot! – Eddie May 17 '13 at 07:18
  • I have posted an interceptor for login in some of my answers, it might help you with other things too. – Roman C May 17 '13 at 07:37
  • Thanks very much, I will find it in your answers... I'm working on a project using struts2... I will ask you some later, I think :) Hope it not bother you – Eddie May 17 '13 at 10:48
  • Erm... I'd like to ask a question about configuration... but it's kind of complicate, I ask the internet but they just don't give me the answer I want, so I want to ask you personally. Would you give me your email so I can mail you directly the question? Mine is Eddie.Marvin116@gmail.com – Eddie May 17 '13 at 11:46
0

How silly I am... just wrong logic in interceptor. All is fine now!!

 public String intercept(final ActionInvocation invocation) throws Exception {
        Map<String, Object> session = ActionContext.getContext().getSession();

        int userId = -1;
        if (session.get("userId") != null) {
            userId = Integer.parseInt(session.get("userId").toString());
        }


        Object action = invocation.getAction();
        //If Act Login, then let through
          if ((action instanceof ActLogin)) {
            return invocation.invoke();
        }
        //Else, if not login, nor not Actlogin, then redirect
        if (userId < 0) {
            System.out.println("check id session " + userId);
            return "loginRedirect";
        }
        //Else Logined, let through
        return invocation.invoke();
    }

In the original code:

 if (userId < 0) {                
    return "loginRedirect";
 } else if (!(action instanceof ActLogin)) {
   return "loginRedirect";
 }

This cause an infinitite Loop, because i'm not loged in, then userId always<0 then always redirect and never let through the ActLogin to Log in

Eddie
  • 1,903
  • 2
  • 21
  • 46