保存传入的HTTP Post请求内容

时间:2013-08-15 13:39:54

标签: java servlets http-post

我想将内容从一个incomming http post请求保存到我的servlet到txt。 问题是我不知道参数,或者我认为它通常是xml的命令请求的格式,但它也可以是html或plain txt。

例如:

如果我通过Rest发送

 <name>Kathi</name>
 <age>21</age>

我希望在txt中写出

 <name>Kathi</name>
 <age>21</age>

这是我的代码(Java Class):

public class TakeRequest extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    writeFile(new Date().toString());
    writeFile(request.toString());

}

public static void writeFile(String request) {
    File file = new File("Request.txt");
    try {
        FileWriter writer = new FileWriter(file, true);
        writer.write(request);
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

JSP文件:

 <%@ page import="MMclass.TakeRequest"%>
 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Catch Request</title>
 </head>
 <body>
    <%
        TakeRequest tr = new TakeRequest();
        tr.doPost(request, response);
    %>
 </body>
 </html>

在我的文件中总是只写

      Thu Aug 15 15:22:37 CEST 2013GET /MM/SaveFile.jsp HTTP/1.1
      Host: localhost:8080
      User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0
      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
      Accept-Language: null
      Accept-Encoding: gzip, deflate
      Cookie: JSESSIONID=16ouuh29vctv6gvfvdg9demy3
      Connection: keep-alive

感谢, SnowN

0 个答案:

没有答案