-3

There is something like location.href javascript but for java. I need it in a jsp page. I'm using javascript now, but will be better for me in a jsp page. Tx.

ivan arias
  • 39
  • 6

3 Answers3

2

If you are using RequestDispatcher.forward() to route the request from controller to the view, you can use in your servlet:

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

or in JSP:

${requestScope['javax.servlet.forward.request_uri']}

If you want the path of your file from inside the JSP:

${pageContext.request.servletPath}

If you are inside the servlet, you can try this way:

String uri = request.getRequestURI();

String pageName = uri.substring(uri.lastIndexOf("/")+1);

If you are somewhere else:

String port = ( Executions.getCurrent().getServerPort() == 80 ) ? "" : (":" + Executions.getCurrent().getServerPort());
url = Executions.getCurrent().getScheme() + "://" + Executions.getCurrent().getServerName() + port + Executions.getCurrent().getContextPath() +  Executions.getCurrent().getDesktop().getRequestPath();
Bruno Franco
  • 2,028
  • 11
  • 20
1

If you want the browser to navigate to a different location, use a redirect:

http://docs.oracle.com/javaee/5/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String)

If you just want to know which page you are on (its path) then there are is:

request.getRequestURI()  -> /foo/bar?key=value
request.getRequestURL()  -> http://www.domain.de/foo/bar (without query)
request.getPathInfo()    -> /foo/bar (including /)
request.getQueryString() -> key=value (without ?)

But generally have a look here: https://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html

Scheintod
  • 7,953
  • 9
  • 42
  • 61
  • not redirect, i need to know what is mi url path, example "http://www.mipage/subdomine/mipage.jsp". i need to know "http://www.mipage/subdomine/" – ivan arias Aug 22 '14 at 12:24
0

Another possible way is to use the RequestDispatcher. Have a look at redirect jsp from servlet RequestDispatcher

Community
  • 1
  • 1
Reporter
  • 3,897
  • 5
  • 33
  • 47