How do I get my web application real path?

Bookmark this example!  
Category: javax.servlet, viewed: 835 time(s).

This code helps you to get the physical path where your web application is deployed on the server. It may be useful so you can for instance read or write file on the server. But please aware that this method will only work when your web application is deployed in an exploded way, if it was deployed in a war format the getRealPath() method just return null.

 
package org.kodejava.example.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class GetWebApplicationPathServlet extends HttpServlet {
 
    public GetWebApplicationPathServlet() {
	super();
    }
 
    protected void doGet(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
 
	String path = getServletContext().getRealPath("");
	PrintWriter writer = response.getWriter();
	writer.println("Application path: " + path);
    }
 
    protected void doPost(HttpServletRequest request,
	    HttpServletResponse response) throws ServletException, IOException {
    }
}
 
 
Can't find what you are looking for? Join our FORUMS and ask some questions!
Firefox 2
Google

100 Top & Latest

GetJava Download Button

Locations of visitors to this page
eXTReMe Tracker
visitor stats