How to get domain and application name?

Here is the script.

There is the following way in my Java web application

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

Say there is a jsp file

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

and i need to get

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

in this jsp file.

Of course, there is a pretty lazy and dumb way to just get the url and then re-track the way back.

But is there a software way to do this? In particular, I think I can get the domain + port, but how can I get the application name "MyWebApp"?

+84
jsp base-url
Feb 05 '10 at 3:32
source share
5 answers

The name of the web application (actually the context path) is available by calling HttpServletrequest#getContextPath() (and thus NOT getServletPath() , as suggested earlier). You can get this in the JSP at ${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 understandable), you can use the HTML <base> :

 <%@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 on the page will automatically refer to <base> , so you won’t need to copy the context path everywhere. Note that when relative links begin with / , they will no longer refer to <base> , but instead to the root of the domain.

+85
Feb 05 '10 at
source share

Take a look at the documentation for the HttpServletRequest .
To create 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(); } 
+93
Feb 05 '10 at 4:15
source share

I would strongly suggest you read the docs for similar methods. If you are interested in the context path, look here ServletContext.getContextPath () .

+6
Feb 05 2018-10-02T00
source share

The following code may be useful for a web application using JavaScript.

 var newURL = window.location.protocol + "//" + window.location.host + "" + window.location.pathname; newURL = newURL.substring(0,newURL.indexOf("")); 
+1
Jul 01 '13 at 3:57
source share

If the url is passed to you as a String and you want to extract the context root of this application, you can use this regular expression to extract it. It will work for full URLs or relative URLs that start with the context root.

 url.replaceAll("^(.*\\/\\/)?.*?\\/(.+?)\\/.*|\\/(.+)$", "$2$3") 
0
May 7 '14 at 20:10
source share



All Articles