KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > displaytag > sample > DisplaySourceServlet


1 /**
2  * Licensed under the Artistic License; you may not use this file
3  * except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://displaytag.sourceforge.net/license.html
7  *
8  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */

12 package org.displaytag.sample;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.PrintWriter JavaDoc;
17
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.http.HttpServlet JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23
24 /**
25  * Servlet used to display jsp source for example pages.
26  * @author Fabrizio Giustina
27  * @version $Revision$ ($Author$)
28  */

29 public class DisplaySourceServlet extends HttpServlet JavaDoc
30 {
31
32     /**
33      * D1597A17A6.
34      */

35     private static final long serialVersionUID = 899149338534L;
36
37     /**
38      * the folder containg example pages.
39      */

40     private static final String JavaDoc EXAMPLE_FOLDER = "/"; //$NON-NLS-1$
41

42     /**
43      * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
44      */

45     protected final void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc,
46         IOException JavaDoc
47     {
48
49         String JavaDoc jspFile = request.getRequestURI();
50
51         // lastIndexOf(".") can't be null, since the servlet is mapped to ".source"
52
jspFile = jspFile.substring(0, jspFile.lastIndexOf(".")); //$NON-NLS-1$
53

54         if (jspFile.lastIndexOf("/") != -1) //$NON-NLS-1$
55
{
56             jspFile = jspFile.substring(jspFile.lastIndexOf("/") + 1); //$NON-NLS-1$
57
}
58
59         // only want to show sample pages, don't play with url!
60
if (!jspFile.startsWith("example-")) //$NON-NLS-1$
61
{
62             throw new ServletException JavaDoc("Invalid file selected: " + jspFile); //$NON-NLS-1$
63
}
64
65         String JavaDoc fullName = EXAMPLE_FOLDER + jspFile;
66
67         InputStream JavaDoc inputStream = getServletContext().getResourceAsStream(fullName);
68
69         if (inputStream == null)
70         {
71             throw new ServletException JavaDoc("Unable to find JSP file: " + jspFile); //$NON-NLS-1$
72
}
73
74         response.setContentType("text/html"); //$NON-NLS-1$
75

76         PrintWriter JavaDoc out = response.getWriter();
77
78         out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " //$NON-NLS-1$
79
+ "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); //$NON-NLS-1$
80
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"); //$NON-NLS-1$
81
out.println("<head>"); //$NON-NLS-1$
82
out.println("<title>"); //$NON-NLS-1$
83
out.println("source for " + jspFile); //$NON-NLS-1$
84
out.println("</title>"); //$NON-NLS-1$
85
out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=ISO-8859-1\" />"); //$NON-NLS-1$
86
out.println("</head>"); //$NON-NLS-1$
87
out.println("<body>"); //$NON-NLS-1$
88
out.println("<pre>"); //$NON-NLS-1$
89
for (int currentChar = inputStream.read(); currentChar != -1; currentChar = inputStream.read())
90         {
91             if (currentChar == '<')
92             {
93                 out.print("&lt;"); //$NON-NLS-1$
94
}
95             else
96             {
97                 out.print((char) currentChar);
98             }
99         }
100         out.println("</pre>"); //$NON-NLS-1$
101
out.println("</body>"); //$NON-NLS-1$
102
out.println("</html>"); //$NON-NLS-1$
103
}
104
105 }
106
Popular Tags