Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to get real path for a file in JSP and Servlet ?.

Program to demonstrate how to get real path for a file in JSP and Servlet


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 :



 
© 2021 Learn Java by Examples Template by Hubberspot