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.
BalusC Feb 05 '10 at 11:36
source share