我想将传入的 http post 请求中的内容保存到我的 servlet 到 txt 中。问题是我不知道参数或传入请求的格式,我认为它通常是 xml,但它也可以是 html 或纯 txt。
例如:
如果我通过休息发送
<name>Kathi</name>
<age>21</age>
我想在txt中写
<name>Kathi</name>
<age>21</age>
这是我的代码(Java 类):
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
谢谢,雪诺