KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Include03


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23
24 /**
25  * Test to insure that an included servlet can set request attributes that are
26  * visible to the calling servlet after the <code>include()</code> returns.
27  * The spec is silent on this topic, but it seems consistent with the overall
28  * intent to behave in this manner.
29  *
30  * The test includes either a servlet ("/Include03a") or a JSP page
31  * ("/Include03b.jsp") depending on the value specified for the "path"
32  * parameter. The default is the servlet.
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
36  */

37
38 public class Include03 extends HttpServlet {
39
40     private static final String JavaDoc specials[] =
41     { "javax.servlet.include.request_uri",
42       "javax.servlet.include.context_path",
43       "javax.servlet.include.servlet_path",
44       "javax.servlet.include.path_info",
45       "javax.servlet.include.query_string" };
46
47
48     public void doGet(HttpServletRequest request, HttpServletResponse response)
49         throws IOException, ServletException {
50
51         // Prepare this response
52
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
53         response.setContentType("text/plain");
54     PrintWriter writer = response.getWriter();
55
56         // Acquire the path to which we will issue an include
57
String JavaDoc path = request.getParameter("path");
58         if (path == null)
59             path = "/Include03a";
60
61         // Create a request dispatcher and call include() on it
62
RequestDispatcher rd =
63             getServletContext().getRequestDispatcher(path);
64         if (rd == null) {
65             sb.append(" No RequestDispatcher returned/");
66         } else {
67             rd.include(request, response);
68         }
69         response.resetBuffer();
70
71         // We MUST be able to see the attribute created by the includee
72
String JavaDoc value = null;
73         try {
74             value = (String JavaDoc)
75                 request.getAttribute(path.substring(1));
76         } catch (ClassCastException JavaDoc e) {
77             sb.append(" Returned attribute not of type String/");
78         }
79         if ((sb.length() < 1) && (value == null)) {
80             sb.append(" No includee-created attribute was returned/");
81         }
82
83         // We MUST NOT see the special attributes created by the container
84
for (int i = 0; i < specials.length; i++) {
85             if (request.getAttribute(specials[i]) != null) {
86                 sb.append(" Returned attribute ");
87                 sb.append(specials[i]);
88                 sb.append("/");
89             }
90         }
91
92         // Write our response
93
if (sb.length() < 1)
94             writer.println("Include03 PASSED");
95         else {
96             writer.print("Include03 FAILED -");
97             writer.println(sb.toString());
98         }
99
100         while (true) {
101             String JavaDoc message = StaticLogger.read();
102             if (message == null)
103                 break;
104             writer.println(message);
105         }
106         StaticLogger.reset();
107
108     }
109
110 }
111
Popular Tags