KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > RequestBodyServlet


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/RequestBodyServlet.java,v 1.4.2.1 2004/02/22 18:21:18 olegk Exp $
3  * $Revision: 1.4.2.1 $
4  * $Date: 2004/02/22 18:21:18 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import java.io.IOException JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.ServletInputStream JavaDoc;
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40
41 public class RequestBodyServlet extends MultiMethodServlet {
42     protected void genericService(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
43         response.setContentType("text/html");
44         PrintWriter JavaDoc out = response.getWriter();
45         StringBuffer JavaDoc buf = null;
46         if(request.getContentLength() > 0) {
47             buf = new StringBuffer JavaDoc();
48             ServletInputStream JavaDoc in = request.getInputStream();
49             int i = 0;
50             while(i<request.getContentLength()) {
51                 int c = in.read();
52                 if(c == -1) {
53                     break;
54                 } else {
55                     buf.append((char)c);
56                 }
57             }
58         } else if("chunked".equalsIgnoreCase(request.getHeader("Transfer-Encoding"))) {
59            buf = new StringBuffer JavaDoc();
60            ServletInputStream JavaDoc in = request.getInputStream();
61            for(int c = in.read(); c != -1; c = in.read()) {
62                if(c == -1) {
63                    break;
64                } else {
65                    buf.append((char)c);
66                }
67            }
68         }
69
70         out.println("<html>");
71         out.println("<head><title>Request Body Servlet: " + request.getMethod() + "</title></head>");
72         out.println("<body>");
73
74         out.println("<p>This is a response to an HTTP " + request.getMethod() + " request.</p>");
75         out.println("<p>Body:</p>");
76         if(null != buf) {
77             out.println("<p><tt>" + buf.toString() + "</tt></p>");
78         } else {
79             out.println("No body submitted.");
80         }
81         out.println("</body>");
82         out.println("</html>");
83     }
84 }
85
86
Popular Tags