Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getContextPath using servlet

Tags:

servlets

Context path in jboss-web.xml is mentioned as /Test, but my war file name is Test-0.0.1. I need this war file name using HttpServlet. Please tell me the function name. i tried getContextPath(), but it returns Test. Thanks

like image 801
gautam Avatar asked Mar 10 '10 15:03

gautam


People also ask

What is request getContextPath () in Java?

getContextPath. String getContextPath() Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character.

How do I find context path?

1- HttpServletRequest The typical way of getting the context path is through the HttpServletRequest class. Simply you can add a HttpServletRequest parameter to your controller method and then get the context path using getContextPath() method.

What is the use of server Contextpath?

The context path of a web application defines the URL that end users will access the application from. A simple context path like myapp means the web app can be accessed from a URL like http://localhost:8080/myapp.

What is ServletContext in Java?

public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.


2 Answers

If the WAR is expanded, you could use ServletContext.getRealPath() in combination with File.getName() to obtain the expanded folder name. This will be the same as the WAR file name.

String name = new File(getServletContext().getRealPath("/")).getName();
like image 41
BalusC Avatar answered Sep 20 '22 18:09

BalusC


Here is the list of functions that are available, along with a graphic showing how they are related. in this example, the war file name is usually in the "context path" piece, in this case "myapp". This is a folder create by TomCat from the WAR file, and while based on the WAR file name, it does not need to be the same. Look for a folder with a name like "Test".

enter image description here

(From HttpServletRequest Path Decoding)

like image 149
AgilePro Avatar answered Sep 17 '22 18:09

AgilePro