44

I'm trying to get a correct current URL in JSP in Spring webapp. I'm trying to use the following fragment in the JSP file:

${pageContext.request.requestURL}

The issue is that the returned URL contains prefix and suffix defined by UrlBasedViewResolver. For example the correct URL is:

http://localhost:8080/page

But the returned one is:

http://localhost:8080/WEB-INF/jsp/page.jsp

2
  • Do you need the absolute URL? Or you just want to build a internal link to another page in your application? Commented Mar 15, 2012 at 9:11
  • 1
    I need the absolute URL.
    – user405935
    Commented Mar 17, 2012 at 13:12

11 Answers 11

53

The best way would be to use EL like this:

${requestScope['javax.servlet.forward.request_uri']}
2
  • 1
    This works perfectly. It returns the current action URL without the base URL and query strings. Commented Dec 15, 2016 at 15:44
  • 3
    This does not work. It does not return the absolute URL.as requested by the OP in the comments to the question. Commented Sep 21, 2017 at 9:52
8

In a jsp file:

request.getAttribute("javax.servlet.forward.request_uri")
1
  • 3
    Does not return an absolute URL as requested by the OP in the comments to the question. Commented Sep 21, 2017 at 9:52
6

Maybe you are looking for something like:

<%= new UrlPathHelper().getOriginatingRequestUri(request) %>

This is not that elegant but solved my problem.

4

You can make Interceptor and set request attribute e.g.

  request.setAttribute("__SELF",request.getRequestURI);

and in jsp

  <form action="${__SELF}" ></form>   
4

Anyone who wants to know about other than the reuqest URI, for example a query string, you can check all the names of the variables in the code of RequestDispatcher(of Servlet API 3.1+) interface.

You can get the query string like this:

${requestScope['javax.servlet.forward.query_string']}
3

I just found the right answer for your question. The key is using Spring Tags.

<spring:url value="" />

If you put the value attribute empty, Spring will display the mapping URL set in your @RequestMapping.

2
  • 6
    It doesn't display anything in my case :/
    – user405935
    Commented Aug 2, 2012 at 19:44
  • I believe the browser will use the current URL when the target is empty. Does the server/webapp also?
    – DerMike
    Commented May 26, 2014 at 9:32
3

Try this:

<%@ page import="javax.servlet.http.HttpUtils.*" %>
<%= javax.servlet.http.HttpUtils.getRequestURL(request) %> 
2
  • Thanks, upvoted, please edit with the nondepercated option.
    – 3xCh1_23
    Commented Apr 3, 2017 at 16:43
  • @MUYBelgium Have you tried opening a separate Stack-Overflow question ? I wish I had more time to help you.
    – 3xCh1_23
    Commented Apr 5, 2017 at 18:48
0

Which Spring version are you using? I have tested this with Spring 3.1.1.RELEASE, using the following simple application:

Folder structure
-----------------------------------------------------------------------------------

spring-web
    |
     --- src
          |
           --- main
                 |
                  --- webapp
                         |
                          --- page
                         |     |
                         |      --- home.jsp
                         |
                          --- WEB-INF
                               |
                                --- web.xml
                               |
                                --- applicationContext.xml

home.jsp
-----------------------------------------------------------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html dir="ltr" lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Welcome to Spring Web!</title>
    </head>
    <body>
        Page URL: ${pageContext.request.requestURL}
    </body>
</html>

web.xml
-----------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" metadata-complete="true" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>org.example.web</display-name>

<servlet>
    <servlet-name>spring-mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/webContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-mvc-dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>

applicationContext.xml
-----------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<context:annotation-config />
<context:component-scan base-package="org.example" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/page/" />
    <property name="suffix" value=".jsp" />
</bean>

<mvc:annotation-driven />

On accessing http://localhost:8080/spring-web/page/home.jsp, the URL is correctly displayed as http://localhost:8080/spring-web/page/home.jsp.

1
  • I'm using Spring 3.1.1 too. What I need is an URL that is mapped by a controller. For example if you had a controller with @RequestMapping("/home") that would use page/home.jsp as a view, then using ${pageContext.request.requestURL} in that jsp would return localhost:8080/spring-web/page/home.jsp but in the browser it would be localhost:8080/spring-web/home.
    – user405935
    Commented Apr 21, 2012 at 20:25
0

Here's an approached I've had success with to build an internal link (not absolute):

    Path:  ${requestScope['javax.servlet.forward.servlet_path']}
Params: ${requestScope['javax.servlet.forward.query_string']}

Putting those together joined by ? to construct the current url:

<c:set var="currentUrl" value="${requestScope['javax.servlet.forward.servlet_path']}?${requestScope['javax.servlet.forward.query_string']}"/>

Add additional url parameters as needed like:

<c:url value="${currentUrl}" var="enUrl"><c:param name="lang" value="en"/></c:url>
<c:url value="${currentUrl}" var="esUrl"><c:param name="lang" value="es"/></c:url>
-1

I used the following in a similar situation. ${currentUrl} can then be Used where needed. Needs core tag library

<c:url value = "" var = "currentUrl" ></c:url>
-2
*<% String myURI = request.getAttribute("javax.servlet.forward.request_uri").toString(); %>
                <% String[] split = myURI.split("/"); %>
                <% System.out.println("My url is-->"+ myURI
                        + "  My url splitter length --->"+split.length
                        +"last value"+split[4]);%>
<%--                <jsp:param name="split[4]" value="split[4]" /> --%>
                <c:set var="orgIdForController" value="<%= split[4] %>" />
                <a type="button" class="btn btn-default btn-xs"
                    href="${pageContext.request.contextPath}/supplier/add/${orgIdForController}">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                    Add
                </a>
 - List item*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.