2

I'd like to get an URL address of the page that redirects me to another page, in other words suppose that I have three jsp file with the names of one.jsp, two.jsp and three.jsp. the codes of each of them can be seen in the below section:

one.jsp

    <a href="two.jsp">go to page two</a>

two.jsp

        <%
            response.sendRedirect("three.jsp");
        %>

three.jsp

        <%
            out.print(request.getHeader("referer"));
        %>

the out put of these will be http://localhost:8080/one.jsp , but instead I expected to get http://localhost:8080/two.jsp as the result.

Now I've got two question:

  1. why I get 'one.jsp' as a result?
  2. How can i get the URL address of pages that do redirecting? ( in this case two.jsp)
wero
  • 32,544
  • 3
  • 59
  • 84
Elyas Hadizadeh
  • 3,289
  • 8
  • 40
  • 54

1 Answers1

4

This behaviour depends on the browser and seems not to be defined, see this question.

Therefore it is simpler to add parameter information about the redirect to the redirect URL and evaluate that parameter in three.jsp:

two.jsp:

<%response.sendRedirect("three.jsp?source=two");%>
Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84
  • It's a good solution because each time I can evaluate the source parameter, but do you want to say that there is no java feature that can handle this duty without passing any parameter? – Elyas Hadizadeh Feb 05 '16 at 10:35
  • 1
    @ElyasHadizadeh its not that Java/servlets have no feature to access the information but rather that the information (e.g. the referer header) sent from browser is not reliable. – wero Feb 05 '16 at 10:41
  • thank you buddy ! one +1 for your answer, but still I don't tick it as an accepted answer maybe there is one person to have another solution, but one more question, you've said `information (e.g. the referer header) sent from browser is not reliable`, may I ask you why? why it is not reliable? I didn't get you. – Elyas Hadizadeh Feb 05 '16 at 10:53
  • 1
    its not reliable because it is browser specific and not RFC standard specific. Each browser has the option to implement it as they wish... – MaVRoSCy Feb 05 '16 at 10:57