KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26
27 /**
28  * Positive test for being able to filter input. The input will be echoed
29  * back in the response, after having been converted to upper case by the
30  * associated filter. Use request parameter <code>type</code> to determine
31  * whether to call getReader() ("reader") or getInputStream() ("stream").
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:56 $
35  */

36
37 public class FilterRequest01 extends HttpServlet {
38
39     public void service(HttpServletRequest request,
40                         HttpServletResponse response)
41         throws IOException, ServletException {
42
43         response.setContentType("text/plain");
44         PrintWriter writer = response.getWriter();
45         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
46
47         String JavaDoc type = request.getParameter("type");
48         if (type == null)
49             type = "reader";
50         if (type.equalsIgnoreCase("reader")) {
51             BufferedReader br = request.getReader();
52             while (true) {
53                 int c = br.read();
54                 if (c < 0)
55                     break;
56                 sb.append((char) c);
57             }
58             br.close();
59         } else {
60             ServletInputStream sis = request.getInputStream();
61             while (true) {
62                 int c = sis.read();
63                 if (c < 0)
64                     break;
65                 sb.append((char) c);
66             }
67             sis.close();
68         }
69
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         StaticLogger.reset();
78
79     }
80
81 }
82
Popular Tags