KickJava   Java API By Example, From Geeks To Geeks.

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


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:56 $
34  */

35
36 public class Forward03 extends HttpServlet {
37
38
39     public void doGet(HttpServletRequest request, HttpServletResponse response)
40         throws IOException, ServletException {
41
42         // Prepare this response
43
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
44         response.setContentType("text/plain");
45     PrintWriter writer = response.getWriter();
46
47         // Acquire the path to which we will issue a forward
48
String JavaDoc path = request.getParameter("path");
49         if (path == null)
50             path = "/Forward03a";
51
52         // Write the request attribute we will be forwarding
53
request.setAttribute("Forward03", "This is the forwarded attribute");
54         if (request.getAttribute("Forward03") == null)
55             sb.append(" Cannot retrieve attribute to forward/");
56
57         // Create a request dispatcher and call forward() on it
58
RequestDispatcher rd =
59             getServletContext().getRequestDispatcher(path);
60         if (rd == null) {
61             sb.append(" No RequestDispatcher returned/");
62         } else {
63             if (sb.length() < 1)
64                 rd.forward(request, response);
65         }
66
67         // Write our response if an error occurred
68
if (sb.length() >= 1) {
69             writer.print("Include03 FAILED -");
70             writer.println(sb.toString());
71             while (true) {
72                 String JavaDoc message = StaticLogger.read();
73                 if (message == null)
74                     break;
75                 writer.println(message);
76             }
77         }
78         StaticLogger.reset();
79
80     }
81
82 }
83
Popular Tags