KickJava   Java API By Example, From Geeks To Geeks.

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


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 ensure that a forwarded-to servlet can receive request attributes
26  * set by the calling servlet, as well as set their own request attributes.
27  *
28  * The test forwards to either a servlet ("/Forward03a") or a JSP page
29  * ("/Forward03b.jsp") depending on the value specified for the "path"
30  * parameter. The default is the servlet.
31  *
32  * @author Craig R. McClanahan
33  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:57 $
34  */

35
36 public class Forward03a extends HttpServlet {
37
38     private static final String JavaDoc specials[] =
39     { "javax.servlet.include.request_uri",
40       "javax.servlet.include.context_path",
41       "javax.servlet.include.servlet_path",
42       "javax.servlet.include.path_info",
43       "javax.servlet.include.query_string" };
44
45
46     public void doGet(HttpServletRequest request, HttpServletResponse response)
47         throws IOException, ServletException {
48
49         // Prepare this response
50
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
51         response.setContentType("text/plain");
52     PrintWriter writer = response.getWriter();
53
54         // Verify that we can retrieve the forwarded attribute
55
if (request.getAttribute("Forward03") == null)
56             sb.append(" Cannot retrieve forwarded attribute/");
57
58         // Verify that we can set and retrieve our own attribute
59
request.setAttribute("Forward03a", "This is our own attribute");
60         if (request.getAttribute("Forward03a") == null)
61             sb.append(" Cannot retrieve our own attribute");
62
63         // Verify that no special attributes are present
64
for (int i = 0; i < specials.length; i++) {
65             if (request.getAttribute(specials[i]) != null) {
66                 sb.append(" Returned attribute ");
67                 sb.append(specials[i]);
68                 sb.append("/");
69             }
70         }
71
72         // Write our response
73
if (sb.length() < 1)
74             writer.println("Forward03 PASSED");
75         else {
76             writer.print("Forward03 FAILED -");
77             writer.println(sb.toString());
78         }
79
80         while (true) {
81             String JavaDoc message = StaticLogger.read();
82             if (message == null)
83                 break;
84             writer.println(message);
85         }
86         StaticLogger.reset();
87
88     }
89
90 }
91
Popular Tags