1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package examples;
18
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.JspTagException;
26 import javax.servlet.jsp.JspWriter;
27 import javax.servlet.jsp.tagext.TagSupport;
28
29
30
31
32 public class ShowSource
33 extends TagSupport {
34 String jspFile;
35
36 public void setJspFile(String jspFile) {
37 this.jspFile = jspFile;
38 }
39
40 public int doEndTag() throws JspException {
41 if ((jspFile.indexOf("..") >= 0) ||
42 (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
43 (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
44 throw new JspTagException("Invalid JSP file " + jspFile);
45
46 InputStream in
47 = pageContext.getServletContext().getResourceAsStream(jspFile);
48
49 if (in == null)
50 throw new JspTagException("Unable to find JSP file: " + jspFile);
51
52 InputStreamReader reader = new InputStreamReader(in);
53 JspWriter out = pageContext.getOut();
54
55
56 try {
57 out.println("<body>");
58 out.println("<pre>");
59 for (int ch = in.read(); ch != -1; ch = in.read())
60 if (ch == '<')
61 out.print("<");
62 else
63 out.print((char) ch);
64 out.println("</pre>");
65 out.println("</body>");
66 } catch (IOException ex) {
67 throw new JspTagException("IOException: " + ex.toString());
68 }
69 return super.doEndTag();
70 }
71 }
72
73
74
75