1 12 package org.displaytag.sample; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.PrintWriter ; 17 18 import javax.servlet.ServletException ; 19 import javax.servlet.http.HttpServlet ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 24 29 public class DisplaySourceServlet extends HttpServlet 30 { 31 32 35 private static final long serialVersionUID = 899149338534L; 36 37 40 private static final String EXAMPLE_FOLDER = "/"; 42 45 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , 46 IOException 47 { 48 49 String jspFile = request.getRequestURI(); 50 51 jspFile = jspFile.substring(0, jspFile.lastIndexOf(".")); 54 if (jspFile.lastIndexOf("/") != -1) { 56 jspFile = jspFile.substring(jspFile.lastIndexOf("/") + 1); } 58 59 if (!jspFile.startsWith("example-")) { 62 throw new ServletException ("Invalid file selected: " + jspFile); } 64 65 String fullName = EXAMPLE_FOLDER + jspFile; 66 67 InputStream inputStream = getServletContext().getResourceAsStream(fullName); 68 69 if (inputStream == null) 70 { 71 throw new ServletException ("Unable to find JSP file: " + jspFile); } 73 74 response.setContentType("text/html"); 76 PrintWriter out = response.getWriter(); 77 78 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"); out.println("<head>"); out.println("<title>"); out.println("source for " + jspFile); out.println("</title>"); out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />"); out.println("</head>"); out.println("<body>"); out.println("<pre>"); for (int currentChar = inputStream.read(); currentChar != -1; currentChar = inputStream.read()) 90 { 91 if (currentChar == '<') 92 { 93 out.print("<"); } 95 else 96 { 97 out.print((char) currentChar); 98 } 99 } 100 out.println("</pre>"); out.println("</body>"); out.println("</html>"); } 104 105 } 106 | Popular Tags |