问题
I created a web application using netbeans and glassfish server. I created a new java file inside that application. I want to find the current application path in that java file.
回答1:
You can get path information from a servlet with methods of HttpServletRequest class:
public class RequestInfoExample extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String requestURI = request.getRequestURI();
String contextPath = request.getContextPath();
}
Javadoc information:
getRequestUri() - Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request...
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...
来源:https://stackoverflow.com/questions/5854554/need-to-find-the-web-application-path