是否可以从servlet访问RAW servlet请求?

时间:2014-02-04 11:50:48

标签: java servlets request

这可能是个愚蠢的问题,但我想确定。

如果你有这样简单的html表单:

  <form action='/requestlogger' method='post'>
    <input type='hidden' name='field1' value='fo' />
    <input type='hidden' name='field2' value='bar'/>
    <input type='hidden' name='field3' value='baz'/>
    <input type='submit'/>
  </form>

然后进行提交,然后浏览器生成如下请求:

POST http://localhost:9090/requestlogger HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost:9090/requestlogger/requestlogger.jsp
Accept-Language: pl-PL
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CMDTDF; BRI/1; BRI/2; .NET4.0C)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:9090
Content-Length: 31
Connection: Keep-Alive
Pragma: no-cache
Cookie: JSESSIONID=69A1DD26A835FDBD8CAEA8BB3BEBF36B

field1=fo&field2=bar&field3=baz

我很好奇我是否可以从servlet捕获这些原始数据,所以我写了测试方法:

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
    try {
        Writer out = resp.getWriter();
        BufferedReader br = new BufferedReader(req.getReader());
        String read = br.readLine();
        while(read != null) {
            out.write(read);
            read = br.readLine();
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

但我只得到了:

field1=fo&field2=bar&field3=baz

当我想到这一点时,这正是我想要得到的,因为webserver(tomcat)处理复杂的东西,我准备好使用HttpServletRequest,所有的头数据都可以通过API(req.getContentLength(),req.getCookies (),req.getHeaderNames()等。)

所以我认为你无法从servlet访问“完整”的RAW请求数据?或者我错过了什么?

由于

0 个答案:

没有答案