0

I want to redirect the user into last accessed page after session expire and i prompt user to login again. In that i passed last page url into one parameter and invoke a seesioerror action for validate the user.

I have this code snippet :

   <script type="text/javascript">
        var currentURL = window.location.href;

    </script>
     <%

        String url= "<script>document.writeln(currentURL);</script>";

        Object obj = session.getAttribute("user");
        if (obj == null) {
            response.sendRedirect(request.getContextPath() + "/SessionError?backurl=" + url);
        }

     %>

Now my problem is whenever I print the Java String varaible url using <% out.print("URL" + url);%> it correctly print the page URL.

But If pass it on this line response.sendRedirect(request.getContextPath() + "/SessionError?backurl=" + url); inside the snippet. It only pass <script>document.writeln(currentURL);</script> not the current page url like http://www.mysite.com/home. Please help me to get rid of this problem.

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97
mathi
  • 53
  • 6

2 Answers2

1

JSP is server side language, it will be executed before http response gets back to client. By saying that, you can use JSP variables in javascript but not the other way around. You can use javascript : document.referrer to get previous url

spiritwalker
  • 2,257
  • 14
  • 9
0

You should be using

request.getAttribute("javax.servlet.forward.request_uri")

See this question

And please don't mix sciplets and Javascript code. Its a mess.

Community
  • 1
  • 1
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47