0

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.

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
vignesh
  • 1,573
  • 7
  • 33
  • 60
  • It's not clear what **server path** means in your question. Do you want to know where your java class is located on the server or do you try to get the context path of your webapp from your java file? – Matt Handy May 02 '11 at 07:38
  • I want to get the root directory of my application – vignesh May 02 '11 at 07:41
  • You need to give some more information? What framework do you use? JSF? What is your "java file". Is it a servlet or a managed bean? – Matt Handy May 02 '11 at 09:03
  • I am using Spring Web MVC framework and servlet. – vignesh May 02 '11 at 09:19

1 Answers1

0

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...

Matt Handy
  • 29,855
  • 2
  • 89
  • 112