JSP:如何读取HTTP post / get请求的内容

时间:2017-04-20 09:15:27

标签: json jsp http

如何读取将读取HTTP帖子内容并存储内容的JSP。

1 个答案:

答案 0 :(得分:1)

将包含以下内容的jsp写入文件。它将记录HTTP请求的内容。

<%@page import="javax.servlet.ServletInputStream,javax.servlet.http.HttpServletRequest"%>
<%@page import="java.io.BufferedReader,java.io.InputStreamReader"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Map"%>
<%

      BufferedReader br = request.getReader();
      String prefix="payment_intimation";

      PrintWriter writer = new PrintWriter("yourfilenamewithpath", "UTF-8");
        String line = "";
        writer.println("----META DATA-----");
        writer.println("Remote Address:"+request.getRemoteAddr());
        writer.println("Content Length:"+request.getContentLength());
        writer.println("Content Type:"+request.getContentType());
        writer.println("Character encoding:"+request.getCharacterEncoding());
        writer.println("Auth Type:"+request.getAuthType());
        writer.println("Context Path:"+request.getContextPath());
        writer.println("Local Address:"+request.getLocalAddr());
        writer.println("Local Name:"+request.getLocalName());
        writer.println("Local Port:"+request.getLocalPort());
        writer.println("Method:"+request.getMethod());
        writer.println("Path Info:"+request.getPathInfo());
        writer.println("Path Transalated:"+request.getPathTranslated());
        writer.println("Protocol:"+request.getProtocol());
        writer.println("QueryString:"+request.getQueryString());
        writer.println("Remote Host:"+request.getRemoteHost());
        writer.println("Remote User:"+request.getRemoteUser());
        writer.println("Requested Session:"+ request.getRequestedSessionId());
        writer.println("Request URI:"+ request.getRequestURI());
        writer.println("Request URL:"+ request.getRequestURL());
        writer.println("Scheme:"+ request.getScheme());
        writer.println("ServerName:"+ request.getServerName());
        writer.println("ServerPort:"+ request.getServerPort());
        writer.println("Servlet Path:"+ request.getServletPath());
        writer.println("----HEADER-----");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
             String headerName = headerNames.nextElement();
             Enumeration<String> headers = request.getHeaders(headerName);
           while (headers.hasMoreElements()) {
                  String headerValue = headers.nextElement();
                  writer.println(headerName+":"+headerValue);
             }
        }
        writer.println("----PARAMETERS-----");
        Map<String, String[]> parameters = request.getParameterMap();
        for(String parameter : parameters.keySet()) {
                String[] values = parameters.get(parameter);
                for (int i=0; i < values.length;i++) {
                  writer.println(parameter+":"+values[i]);
                }
        }
writer.println("----BODY-----");
        while((line = br.readLine()) != null) {
                writer.println(line);
        }
     writer.close();
%>