KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > servlet > SourceCodeServlet


1 package org.apache.myfaces.examples.servlet;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5 import java.io.*;
6
7 public class SourceCodeServlet extends HttpServlet
8 {
9     public void doGet(HttpServletRequest req, HttpServletResponse res)
10         throws IOException, ServletException
11     {
12         String JavaDoc webPage = req.getServletPath();
13         
14         // remove the '*.source' suffix that maps to this servlet
15
int chopPoint = webPage.indexOf(".source");
16         
17         webPage = webPage.substring(0, chopPoint - 1);
18         webPage += "p"; // replace jsf with jsp
19

20         // get the actual file location of the requested resource
21
String JavaDoc realPath = getServletConfig().getServletContext().getRealPath(webPage);
22         System.out.println("realPath: " + realPath);
23
24         // output an HTML page
25
res.setContentType("text/plain");
26
27         // print some html
28
ServletOutputStream out = res.getOutputStream();
29
30         // print the file
31
InputStream in = null;
32         try
33         {
34             in = new BufferedInputStream(new FileInputStream(realPath));
35             int ch;
36             while ((ch = in.read()) !=-1)
37             {
38                 out.print((char)ch);
39             }
40         }
41         finally {
42             if (in != null) in.close(); // very important
43
}
44     }
45 }
46
Popular Tags