Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get domain URL and application name?

Tags:

jsp

base-url

Here's the scenario.

My Java web application has following path

https://www.mywebsite.com:9443/MyWebApp 

Let's say there is a JSP file

https://www.mywebsite.com:9443/MyWebApp/protected/index.jsp 

and I need to retrieve

https://www.mywebsite.com:9443/MyWebApp  

within this JSP file.

Of course, there is rather a lazy and silly way of just getting the URL and then re-tracing the path back.

But is there a programatic way of doing this? Specifically, I think I can get the domain + port, but how do I actually retrieve the application name "MyWebApp"?

like image 847
ericbae Avatar asked Feb 05 '10 03:02

ericbae


People also ask

How to get domain name from http Request in java?

Firstly, we'd need to extract the host from the given URL value. We can use the URI class: String urlString = "https://www.baeldung.com/java-tutorial"; URI uri = new URI(urlString); String host = uri. getHost();

How do I get a domain name in react?

To access the domain name from an above URL, we can use the window. location object that contains a hostname property which is holding the domain name. Similarly, we can also use the document. domain property to access it.


2 Answers

Take a look at the documentation for HttpServletRequest.
In order to build the URL in your example you will need to use:

  • getScheme()
  • getServerName()
  • getServerPort()
  • getContextPath()

Here is a method that will return your example:

public static String getURLWithContextPath(HttpServletRequest request) {    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); } 
like image 71
Jataro Avatar answered Sep 29 '22 15:09

Jataro


The web application name (actually the context path) is available by calling HttpServletrequest#getContextPath() (and thus NOT getServletPath() as one suggested before). You can retrieve this in JSP by ${pageContext.request.contextPath}.

<p>The context path is: ${pageContext.request.contextPath}.</p> 

If you intend to use this for all relative paths in your JSP page (which would make this question more sense), then you can make use of the HTML <base> tag:

<%@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}" />  <!doctype html> <html lang="en">     <head>         <title>SO question 2204870</title>         <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/">         <script src="js/global.js"></script>         <link rel="stylesheet" href="css/global.css">     </head>     <body>         <ul>             <li><a href="home.jsp">Home</a></li>             <li><a href="faq.jsp">FAQ</a></li>             <li><a href="contact.jsp">Contact</a></li>         </ul>     </body> </html> 

All links in the page will then automagically be relative to the <base> so that you don't need to copypaste the context path everywhere. Note that when relative links start with a /, then they will not be relative to the <base> anymore, but to the domain root instead.

like image 40
BalusC Avatar answered Sep 29 '22 17:09

BalusC