Program to demonstrate how to get real path for a file in JSP and Servlet
Output of the program :
package com.hubberspot.jsp.servlets.examples; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/RealPathInfoServlet") public class RealPathInfoServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { doPost(request, response); } protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter out = response.getWriter(); ServletContext context = request.getServletContext(); String realpath = context.getRealPath("/WEB-INF/web.xml"); out.println("<html>"); out.println("<body>"); out.println("<h2>"); out.println("The Real Path for the file web.xml"); out.println("i.e. <br> the deployment descriptor is <br>"); out.println("<h4>"+realpath+"</h4>"); out.println("</h2>"); out.println("</html>"); out.println("</body>"); } }
Output of the program :