Last updated on November 17th, 2015 at 05:13 am

Sometimes programmers need to write a JSP or Servlet code that will save a text file to a particular path, Execute a shell / perl script etc., for this we need to find the real path in which our JSP or Servlet is running. This will help us to define the path the scripts or files need to placed and executed. We are giving you code for JSP and Servlet which is almost the same to get the real path.

JSP

path =  getServletContext().getRealPath("/")
out.println("<div> The Script Path Is "+path+"</div>")

In Java / Servlet it will be slightly different but same logic

String absoluteFooWebPath = getServletContext().getRealPath("/");
System.out.println("Path is :- "+absoluteFooWebPath);

JSP script above will display the result direclty on the Webpage. But the second script will write to the system logs when it is run as a Servlet.

Leave a Reply

Your email address will not be published. Required fields are marked *