Actual path of servlet in server
In Java EE, it is often necessary to obtain the actual file system path of a servlet in server or resources related to it, such as configuration files, within the server. This can be done using the ServletContext object, which provides methods to interact with the servlet’s environment.
Methods to Get the Actual Path
- Using ServletContext.getRealPath(String path)
- Using ServletContext.getResource(String path) and getResourceAsStream(String path)

Table of Contents
1. Using ServletContext.getRealPath(String path)
The getRealPath(String path) method returns the real path on the server’s file system corresponding to a given virtual path.
Example: In your servlet:
java
package com.example;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
@WebServlet("/getPath")
public class PathServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the ServletContext
ServletContext context = getServletContext();
// Get the real path of the servlet
String realPath = context.getRealPath("/WEB-INF/config.properties");
// Output the real path
response.setContentType("text/plain");
response.getWriter().println("Real Path: " + realPath);
}
}
In this example, the servlet retrieves the real path of a file config.properties located in the WEB-INF directory and outputs it.
2. Using ServletContext.getResource(String path) and getResourceAsStream(String path)
The getResource(String path) method returns a URL object pointing to the resource, while getResourceAsStream(String path) returns an InputStream to read the resource.
java
package com.example;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@WebServlet("/loadConfig")
public class ConfigServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the ServletContext
ServletContext context = getServletContext();
// Get the resource as InputStream
InputStream input = context.getResourceAsStream("/WEB-INF/config.properties");
if (input != null) {
// Load properties from the InputStream
Properties properties = new Properties();
properties.load(input);
// Output properties
response.setContentType("text/plain");
properties.store(response.getWriter(), "Loaded Properties");
} else {
response.getWriter().println("Resource not found");
}
}
}
In this example, the servlet loads a properties file from the WEB-INF directory and outputs its content
Summary Actual path of servlet in server
To obtain the actual path of a servlet or related resources on the server, you can use the following methods provided by the ServletContext object:
- ServletContext.getRealPath(String path) : Returns the real file system path corresponding to the virtual path.
- ServletContext.getResource(String path) : Returns a URL object pointing to the resource.
- ServletContext.getResourceAsStream(String path) : Returns an InputStream to read the resource.
Practical Use Cases
- Loading Configuration Files : Read and load configuration files located in the WEB-INF directory.
- Accessing Static Resources : Retrieve the real path of static resources like images, CSS files, or JavaScript files.
- File Manipulation : Perform file operations like reading, writing, or deleting files on the server.
By using these methods, you can effectively interact with the file system within the server, allowing your servlet to access and manage resources as needed.