KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > ShowSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package examples;
26
27
28 import javax.servlet.*;
29 import javax.servlet.jsp.*;
30 import javax.servlet.jsp.tagext.*;
31
32 import java.io.*;
33
34 /**
35  * Display the sources of the JSP file.
36  */

37 public class ShowSource
38     extends TagSupport
39 {
40     String JavaDoc jspFile;
41     
42     public void setJspFile(String JavaDoc jspFile) {
43         this.jspFile = jspFile;
44     }
45
46     public int doEndTag() throws JspException {
47     if ((jspFile.indexOf( ".." ) >= 0) ||
48             (jspFile.toUpperCase().indexOf("/WEB-INF/") != 0) ||
49             (jspFile.toUpperCase().indexOf("/META-INF/") != 0))
50         throw new JspTagException("Invalid JSP file " + jspFile);
51
52         InputStream in
53             = pageContext.getServletContext().getResourceAsStream(jspFile);
54
55         if (in == null)
56             throw new JspTagException("Unable to find JSP file: "+jspFile);
57
58         InputStreamReader reader = new InputStreamReader(in);
59     JspWriter out = pageContext.getOut();
60
61
62         try {
63             out.println("<body>");
64             out.println("<pre>");
65             for(int ch = in.read(); ch != -1; ch = in.read())
66                 if (ch == '<')
67                     out.print("&lt;");
68                 else
69                     out.print((char) ch);
70             out.println("</pre>");
71             out.println("</body>");
72         } catch (IOException ex) {
73             throw new JspTagException("IOException: "+ex.toString());
74         }
75         return super.doEndTag();
76     }
77 }
78
79     
80         
81     
82
Popular Tags