Question

The are some jsp pages in my project in which they all can be accessed only when there is a user session. I have an interceptor for check whether session is in or not. If the session out, the page is redirected to login page. But after login success i need to be redirected to the page which working earlier. For eg: If i am working in xxx.jsp page, when the session out, i directed to login page. After successful login i need to redirect to xxx.jsp. plz help me.

Was it helpful?

Solution

In Action.class

 try {
     HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
        String backurl = request.getHeader("referer");
        System.out.println(" Backurl : " + backurl);
        Map session = ActionContext.getContext().getSession();
        if (session.get("cus") != null) {
            return "already_login";
        } else {

            return "not_yet_login";
        }

    } catch (Exception e) {
        logger.info("Error : " + e);
        logger.info("Error Message : " + e.getMessage());

    }

In struts.xml

<action name="call_Action_Class" class="controller.Action.class">
        <result name="already_login">success.jsp</result>
        <result name="not_yet_login">new_login_page.jsp</result>
</action>

In new_login_page.jsp

<form action="login_dedirect" method="post" id="logsubmit">

      <h2>Email :</h2>
      <input type="text" name="email"  />
      <h2 >Password :</h2>
      <input type="password" name="passwd" id="pwd" />
      <input type="submit"  value="Login" />
 </form>

In struts.xml

<action name="login_dedirect" class="controller.ActionRedirect.class">
        <result name="success" type="redirect" >${backurl}</result>
        <result name="fail">new_login_page.jsp</result>
</action>

In ActionRedirect.class

private String backurl;//Getter & Setter Method.

try{
   System.out.println(" Backurl :"+backurl);

   return "success";
}catch(Exception e){
     return "fail";
}

OTHER TIPS

In struts you use actions for business logic. and you write these actions in struts-config.xml. in action class you return action mapping to redirect to any jsp page or servlet as you want. here below i show you example

In struts-config

 <form-beans>
    <form-bean name="Register" type="FormBeans.Register"/>

</form-beans>

above name attribute is used for input form and type is action class you register with form. Below is action class where you write business logic

 public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    Register r=(Register)form;
    DBManager db=new DBManager();
    boolean rs=db.checkCustomer(r);
    if(rs){
        HttpSession session =request.getSession(true);
        session.setAttribute("fname", r.getEmail());
        return mapping.findForward(SUCCESS);
    }

    return mapping.findForward("failure");
}

now come back to struts-config and their are two tags

<action-mappings>
    <action input="/WEB_INF/registeration.jsp" name="Register" path="/register"    scope="session" type="actions.registration" validate="false">
    <forward name="failure" path="/registeration.jsp"/>
    <forward name="success" path="/mainmenu.jsp"/>
     </action>
    </action-mapping>

you can use both global forward and forward in action tag to redirect where ever you want

************updated**********************

Above is strut1 and below is for struts 2 . you have defined the xml file like strut-config .Although redirecting process is same but their is very small difference e.g in xml file

<struts> 
<constant name="struts.devMode" value="true" /> 
  <package name="helloworld" extends="struts-default"> 

   <action name="hello" 
     class="HelloWorldAction" 
       method="execute"> 
    <result name="success">/HelloWorld.jsp</result> 
   </action> 
  </package> 
  </struts> 

here constant tag show that we are in developer mode.this will help to show logs for debuging your application. and result tage is for redirecting to any jsp page you want

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top