how to get the base url from jsp request object?

Maverick :

How to get the base url from the jsp request object? http://localhost:8080/SOMETHING/index.jsp, but I want the part till index.jsp, how is it possible in jsp?

BalusC :

So, you want the base URL? You can get it in a servlet as follows:

String url = request.getRequestURL().toString();
String baseURL = url.substring(0, url.length() - request.getRequestURI().length()) + request.getContextPath() + "/";
// ...

Or in a JSP, as <base>, with little help of JSTL:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="req" value="${pageContext.request}" />
<c:set var="url">${req.requestURL}</c:set>
<c:set var="uri" value="${req.requestURI}" />
...
<head>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
</head>

Note that this does not include the port number when it's already the default port number, such as 80. The java.net.URL doesn't take this into account.

See also:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get request URL from a Node response object?

How to get base url without accessing a request

How to get the original request url from a servlet/jsp after multiple servlet forwards

How to get parameters value as it is from the URL with JSP

how to get URL from address bar in JSP

How to get parameters from the URL with JSP

Get Server IP address from JSP Request/session object

How to get JSON from URL with Get Request?

How to get full url from django request

How to receive GET request from URL

How to get the url from the request on the cloud function?

How to get property from Request object Laravel?

How to get "base" object from Exposed ResultRow

How to get the same route URL from request object as defined in app.use in express (Node.js)?

get a value from url - jsp

Laravel 5.1 get variable from pretty URL to Request object

get a particular object from jsonBody with query param of request url in wiremock

How do I get an object from Model to AngularJS variable in JSP?

In Spring MVC How to get form object in JSP from controller ?

AngularJS Restangular: How to remove default request param from base url

Accessing Request object from custom JSP tags

How to intercept a request by URL base?

How to get a file or blob from an object URL?

How to get JSON Object from URL

How to make a get request with a URL-encoded object in angular?

How to remove JSP's native request URL?

How to get Params from url for Axios GET request?

How to Request Specific Object From JSON URL with C# WebClient

Angular 4 override base url get request