How to get the request url?

I am trying to redirect to the page where the user was trying to log in.

I mean, some page -> login -> somepage

I know it;

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 does not work. The requested URL is always "LoginPro", which handles the login process. When the user clicks the login button, the page goes to LoginPro. Therefore, the request URL is always loginPro ...

It seems so; somepage -> login -> loginPro -> LoginAction (url request is loginPro ..) -> loginPro

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

+6
struts2
source share
3 answers

Thank you for your responses.

I found this method and it works!

url = request.getHeader ("referer");

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

+5
source share

When you click on the "Login" link, and then in java, behind this request stores the url as a static variable.

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

Then after inserting the login information u call come other method. Use this static variable for redirection.

For example: I used on 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; } 
+3
source share

How do you redirect to your login action? If this is the only place (or some kind of common database that does the redirection), you could add a variable to the session, do your SessionAware login action, and then just extract / remove the source URL after a successful login and use it ?

0
source share

All Articles