import java.io.*;
import java.util.Date;
import java.util.Hashtable;

import javax.servlet.*;
import javax.servlet.http.*;


/**
 * Display the sources of the JSP and Java files given as pathInfo
 */
public class ViewSrc extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
      ServletOutputStream out = res.getOutputStream();
      res.setContentType("text/html");
      String theFile = req.getPathInfo();
      ServletContext sc = getServletContext(); 
      String realPath = sc.getRealPath(theFile);
      if (theFile == null)
	    throw new ServletException("Missing or Invalid file " + realPath);
      FileInputStream in = new FileInputStream(realPath); 
      if (in == null)
            throw new ServletException("Unable to find file: "+ realPath);
      InputStreamReader reader = new InputStreamReader(in);
      try {
            out.println("<body>");
            out.println("<pre>"); 
            for(int ch = in.read(); ch != -1; ch = in.read())
                if (ch == '<')
                    out.print("&lt;");
                else
                    out.print((char) ch);
            out.println("</pre>");
            out.println("</body>");
      } catch (IOException ex) {
            throw new IOException("IOException: "+ex.toString());
      }
    }

    public String getServletInfo() {
        return "Displays in html format the file given on the request";
    }

}

    
        
    
