How to get the request URL in Struts 2?

Thanks for your answers.

I found this way and it works!

url = request.getHeader("referer");

This url is the exact url where the action is called.


When you click a link of Login then in java behind that request store url as static variable.

static String url = request.getHeader("referer");</p>

Then after inserting login details u call come other method. Use that static variable to redirect.

For Example: I have used in my site.

<action name="login" class="actions.Login.LoginAuthenticate" method="input">    
    <!--Cookie functionality done -->
    <result name="input">Login/login.jsp</result>
</action>

<action name="loginAuthenticate" class="actions.Login.LoginAuthenticate" method="execute">
        <!--Cookie functionality done -->
        <result name="redirect" type="redirect">${redirectUrl}</result>
        <result name="input">Login/login.jsp</result>
</action>

public String execute() throws Exception {
    if(getCheckCookies()){
            setRedirectUrl("/login");
            return "redirect";
    }
    Cookie un = new Cookie("un" , lemail);
    un.setMaxAge(-1);
    un.setVersion(1);
    servletResponse.addCookie(un);
    System.out.println("------>--------->------> " + redirectUrl);
    return "redirect";
}


public String input() throws Exception {
    HttpServletRequest request = ServletActionContext.getRequest();
    setRedirectUrl(request.getHeader("Referer"));
    return INPUT;
}

public static String redirectUrl;

public void setRedirectUrl(String redirectUrl){
    this.redirectUrl = redirectUrl;
}
public String getRedirectUrl(){
    return this.redirectUrl;
}

Tags:

Struts2