Deckard
Deckard

Reputation: 1433

How to get the request URL in Struts 2?

I'm trying to redirect to the page where the user tried to login.

I mean, somepage → login → somepage

I know this;

In LoginAction

HttpServletRequest request = ServletActionContext.getRequest();
String url = request.getServletPath();
setUrl(url);

In struts.xml

 <action name="LoginPro" method="login" class="LoginAction">
    <result type="redirect">${url}</result>
    <result name="input" type="tiles">login.error</result>
 </action>

But it's not working. The requested url is always "LoginPro" which is handling login process. When a user clicks login button, page goes to LoginPro. So the request url is always loginPro...

It seems to be this way; somepage → login → loginPro → LoginAction(request url is loginPro..) → loginPro

How can I redirect users to the page where they tried to login?

Upvotes: 7

Views: 21214

Answers (4)

Shanu Shine
Shanu Shine

Reputation: 51

    HttpServletRequest request = ServletActionContext.getRequest(); 
    String url = request.getRequestURI(); 

Upvotes: 0

nagesh
nagesh

Reputation: 31

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;
}

Upvotes: 3

Deckard
Deckard

Reputation: 1433

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.

Upvotes: 6

Shawn D.
Shawn D.

Reputation: 8125

How are you redirecting to your login action? If it's only a single place (or some common base that does the redirection) could you add a variable to the session, make your Login action SessionAware, and then just retrieve/remove the source URL after a successful login, and use that?

Upvotes: 0

Related Questions