tags:

views:

760

answers:

3

Here's the scenario.

My Java web application has a 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 + 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"?

+4  A: 

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

Adeel Ansari
A: 

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();
}
Jataro
+3  A: 

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

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:

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2204870</title>
        <base href="${pageContext.request.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.

BalusC
Doc states, "The context path returned by `ServletContext.getContextPath()` should be considered as the prime or preferred context path of the application". That was the reason I included this one to my original answer, after realizing the thing. I didn't remove my first attempt, as I want the OP to consider reading through the docs and know a little more about related methods. I hope you are getting my point.
Adeel Ansari
@Vinegar: For JSP files, the `getServletPath()` returns `/filename.jsp`. The OP wasn't asking for that at all, this is certainly not the way to go.
BalusC
I would certainly make correction in my post, but this time I was expecting the response to my comment, which is entirely different :).
Adeel Ansari