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 webPage = req.getServletPath(); 13 14 int chopPoint = webPage.indexOf(".source"); 16 17 webPage = webPage.substring(0, chopPoint - 1); 18 webPage += "p"; 20 String realPath = getServletConfig().getServletContext().getRealPath(webPage); 22 System.out.println("realPath: " + realPath); 23 24 res.setContentType("text/plain"); 26 27 ServletOutputStream out = res.getOutputStream(); 29 30 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(); } 44 } 45 } 46 | Popular Tags |