Search code examples
javajspdesign-patternsstruts2post-redirect-get

browser asks to resend data when page is reloaded


I am writing an application using struts2. I am facing a problem in the login part. When a login is performed,it will redirect to the index.jsp page.Now the problem arises. The problem is when after loggin in, index.jsp is reloaded, browser asks me resend data. I dont know why it is happening? here is my struts.xml code for authenticate action:-

<action name="authenticate" class="com.action.LoginAction">  
    <result name="success">index.jsp</result>
    <result name="failure">loginerror.jsp</result> 
</action>

and here is code for the login action class:-

public String execute() {
    if(sessionMap.containsKey("project_user")){
        return "success";
    }

    Project_User project_User=Login.checkCredentials(email_id,password);

    if(project_User!=null) {
        sessionMap.put("project_user", project_User);
        return "success";
    } else
        return "failure";
}

also when index.jsp comes, the url area of browser remians unchanged, the link in url field of browser still shows the action name like:- localhost:8084/Tek-Hub/authenticate/ if anyone knows about it plzzz help me. Thanxx


Solution

  • You need to use a pattern named PRG (Post / Redirect / Get).

    This way, a second request will be performed when executing the first action result (because of the redirection), and a refresh of the landing page (eg. pressing F5) will hit the second action (the GET one), instead of the login action (the POST one).

    Change this:

    <action name="authenticate" class="com.action.LoginAction">  
        <result name="success">index.jsp</result>
        <result name="failure">loginerror.jsp</result> 
    </action>
    

    to this:

    <action name="authenticate" class="com.action.LoginAction">  
        <result name="success" type="redirectAction">index.action</result>
        <result name="failure">loginerror.jsp</result> 
    </action>
    
    <action name="index" class="com.action.IndexAction">  
        <result name="success">index.jsp</result>
    </action>
    

    enter image description here