0

i want to save the content from a incomming http post request to my servlet into a txt. The Problem is i don´t know the Parameters, or the format of the incomming request i think it usually will be xml, but it also can html or plain txt.

for example:

if i sending via Rest

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

i want that in the txt is written

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

this is my code (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 File:

 <%@ 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>

in my File alway´s only writing

      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

thanks, SnowN

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
SnowN
  • 53
  • 2
  • 9
  • Maybe this helps you: http://stackoverflow.com/questions/14525982/getting-request-payload-from-post-request-in-java-servlet – yname Aug 15 '13 at 13:49
  • In the JSP file you aren't actually performing an HTTP POST request, you're just calling the method of an instance that happens to be the same one the Servlet container would use to handle an HTTP POST. You seem to be confusing the two. – Sotirios Delimanolis Aug 15 '13 at 13:50
  • Similar questions have been asked before. For instance see http://stackoverflow.com/questions/5023718/how-to-retreive-raw-post-data-from-httpservletrequest-in-java – Buhb Aug 15 '13 at 13:53
  • Oh Thanks Buhb i´ve used google 2hours and don´t find this question =( thats help me alot ;-) – SnowN Aug 15 '13 at 14:03
  • 1
    Searching is easier if you know the right terms by having learnt the subject using the right tutorials. Every sane tutorial on subject "HTTP" tells you that the "request content" as you called it is actually to be called "request body". – BalusC Aug 15 '13 at 14:25

0 Answers0