`
amly0615
  • 浏览: 9634 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
文章分类
社区版块
存档分类
最新评论

getReader has been called for this request

阅读更多


I want to add logging to my Servlet, so I've created Filter which should display request and go to the Servlet. But unfortunately I've encoutered exception:
java.lang.IllegalStateException: getReader() has already been called for this request
    at org.apache.catalina.connector.Request.getInputStream(Request.java:948)
    at org.apache.catalina.connector.RequestFacade.getInputStream(RequestFacade.java:338)
    at com.noelios.restlet.ext.servlet.ServletCall.getRequestEntityStream(ServletCall.java:190)

So to fix this problem I've found solution with Wrapper, but it doesn't work. What else can I use/change in code? Any ideas?

[MyHttpServletRequestWrapper]
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper
{
    public MyHttpServletRequestWrapper(HttpServletRequest request)
    {
        super(request);
    }

    private String getBodyAsString()
    {
        StringBuffer buff = new StringBuffer();
        buff.append(" BODY_DATA START [ ");
        char[] charArr = new char[getContentLength()];
        try
        {
            BufferedReader reader = new BufferedReader(getReader());
            reader.read(charArr, 0, charArr.length);
            reader.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        buff.append(charArr);
        buff.append(" ] BODY_DATA END ");
        return buff.toString();
    }

    public String toString()
    {
        return getBodyAsString();
    }
}

[MyFilter]
public class MyFilterimplements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        final HttpServletResponse httpServletResponse = (HttpServletResponse) response;

        final HttpServletRequestWrapper requestWrapper = new MyHttpServletRequestWrapper(httpServletRequest);
        final String requestBody = requestWrapper.toString();

        chain.doFilter(request, response);
    }
}

java servlets servlet-filters




link|improve this question



asked Sep 6 '11 at 10:45




smas
2,764318


82% accept rate












feedback





2 Answers

active oldest votes





up vote1down vote accepted



Looks like the restlet framework has called getRequestEntityStream() on the Request object which in turn calls getInputStream(), so calling getReader() on the request throws IllegalStateException. The Servlet API documentation for getReader() and getInputStream() says:
public java.io.BufferedReader getReader()
    ...
    ...
Throws:
    java.lang.IllegalStateException - if getInputStream() method has been called on this request

public ServletInputStream getInputStream()
    ...
    ...
    Throws:
    java.lang.IllegalStateException - if the getReader() method has already been called for this request

From the documentation it seems that we cannot call both getReader() and getInputStream() on the Request object. I suggest you use getInputStream() rather than getReader() in your wrapper.

The main problem is that you can't read the input both as binary stream and character stream, not even if the one is called in a filter and the other in the servlet.

分享到:
评论

相关推荐

    WebService简介

    WebService学习,java端通过Axis2,Cxf发布WebService的应用

    servlet2.4doc

    The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. doGet...

    JSP的Request对象练习源代码

    6. **读取请求体内容**:对于POST请求,如果数据在请求体中,可以使用`getInputStream()`或`getReader()`来读取。 7. **分发请求**:`RequestDispatcher`接口的`forward()`和`include()`方法允许我们在服务器端将...

    4 JSP内建对象之request

    **JSP内建对象之request详解** 在JavaServer Pages (JSP) 技术中,内建对象扮演着核心角色,它们提供了与服务器交互的基本功能。"request"对象是这七个内建对象之一,它是 javax.servlet.http.HttpServletRequest ...

    完美解决request请求流只能读取一次的问题

    解决request请求流只能读取一次的问题,我们可以使用自定义的HttpServletRequestWrapper,覆写getInputStream()和getReader()方法,从而实现流的重复读取。这可以在SpringBoot项目中使用Filter拦截器对所有请求流中...

    java 获取request中的请求参数代码详解

    在 Java 中,我们可以使用 `getInputStream()` 方法或 `getReader()` 方法来获取 Request Body 中的数据。 ```java public String getRequestData(HttpServletRequest httpServletRequest) { ...

    获取request的参数

    MyRequestObject obj = mapper.readValue(request.getReader(), MyRequestObject.class); // 使用obj处理请求... } ``` 在Python的Django框架中,`HttpRequest`对象提供了类似的方法: ```python from django....

    对Servlet 中 request.setEncoding("utf-8")的分析

    如果`usingReader`为`true`,则意味着已经调用了`getReader()`方法,这时再设置编码是无效的,因为`getReader()`已经创建了一个`BufferedReader`,它会根据之前的编码进行读取,所以此时会抛出`...

    JAVA100例之实例84Request

    在本实例"JAVA100例之实例84Request"中,我们将深入探讨如何处理和解析这些请求,特别是与Servlet相关的Java API。Servlet是Java中用于创建动态Web应用程序的关键技术,它能够接收和响应来自客户端的HTTP请求。 1. ...

    第20章 Request和Response.pdf

    - POST请求时,可以通过`getInputStream()`或`getReader()`获取请求正文中携带的数据。 4. **通用方法**: - `getParameter(String name)`:获取请求参数的值。 - `getParameterValues(String name)`:获取请求...

    JSP内置对象:Request和Response的简单介绍及使用

    **JSP内置对象Request与Response详解** 在JavaServer Pages (JSP) 技术中,Request和Response是两个非常重要的内置对象,它们分别代表了HTTP请求和响应的生命周期中的关键部分。了解并熟练掌握这两个对象的使用对于...

    JSP request(return String)用法详例

    在实际开发中,`request`对象还提供其他功能,如获取请求参数(`request.getParameter()`),读取请求体数据(`request.getInputStream()`或`request.getReader()`),检查请求是否是安全的(`request.isSecure()`)...

    .net数据访问类 SQL Helper 类

    注意: Application Block for .NET(用于 .NET 的应用程序块)是基于对成功的 .NET 应用程序进行详细研究而设计的。它以源代码的形式提供,您可以原样使用,也可以针对自己的应用程序进行自定义。该应用程序块并不...

    day14_servlet&http&request_java_httprequestservlet_milejfl_

    - **读取请求体**:`getInputStream()`或`getReader()`用于读取POST请求中包含的数据。 4. **HttpServletResponse接口**: - **设置响应状态码**:`setStatus(int code)`用于设置HTTP响应的状态码,如200(成功)...

    webbrowse设置显示内容

    【标题】:“WebBrowser控件配置与内容显示” 在.NET框架中,`WebBrowser`控件是一个非常实用的组件,它允许开发人员在Windows应用程序中嵌入一个网页浏览器的功能。这个控件通常用于C#编程,是`.NET`库的一部分,...

    okhttp通信框架和java后台的交互,客户端,服务端代码对接

    String requestBody = req.getReader().lines().collect(Collectors.joining(System.lineSeparator())); // 处理请求逻辑 // 返回响应 resp.getWriter().write(responseBody); } } ``` 四、代码对接 Android...

    JavaScript 学习 - 提高篇

    this.getReader = Book_getReader; // 将 getReader 函数赋值给对象的属性 } function Book_getReader() { // ...方法实现 } ``` 这里 `Book_getReader` 被定义为 `Book` 对象的一个方法。 **调用方法** ```...

    JSP对象及使用方法(带例子)

    | 14 | `getReader()` | 返回解码过的请求体 | | 15 | `getRemoteAddr()` | 返回发送此请求的客户端IP地址 | | 16 | `getRemoteHost()` | 返回发送此请求的客户端主机名 | | 17 | `setAttribute(String key, Object ...

    JSP九大内置对象详解

    | 14 | `BufferedReader getReader()` | 获取已解码的请求体 | | 15 | `String getRemoteAddr()` | 返回发送此请求的客户端IP地址 | | 16 | `String getRemoteHost()` | 返回发送此请求的客户端主机名 | | 17 | `...

Global site tag (gtag.js) - Google Analytics