Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot