How do I get my web application real path?
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!
Related Examples
- How do I get servlet request header information?
- How do I get servlet request URL information?
- How do I read servlet context initilization parameters?
- How do I know session last access time?
- How do I get a notification when session attribute was changed?
- How do I capture session creation and removal events?
- How do I invalidate user's session?
- How do I get client IP and hostname in Servlet?
- How do I set the maximum age of a cookie?
- How do I send a response status in servlet?