Get Post data

Hello,

I hava a servlet which should handle POST data sent by a client.

I tried to get the contend using the request Object of the servlet, but everything I tried failed :(

I hope someone can help me and tell me how I can get this data.

Here is the client code:

publicclass RequestHandler{

publicvoid handleRequest()throws Exception{

URL url =new URL("http://127.0.0.1:8080/XMLServer/XMLServerServlet");

HttpURLConnection con = (HttpURLConnection)url.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setRequestMethod("POST");

con.connect();

Document xmlRequest = DocumentHelper.createDocument();

Element root = xmlRequest.addElement("req")

.addAttribute("t","0");

Element p = root.addElement("p")

.addAttribute("p1","10");

String request = xmlRequest.asXML();

System.out.println(request);

PrintWriter writer =new PrintWriter(new OutputStreamWriter(con.getOutputStream()));

writer.write(request, 0, request.length());

writer.flush();

BufferedInputStream instream =new BufferedInputStream(con.getInputStream());

StringBuffer inreader =new StringBuffer(con.getInputStream().available());

int i = 0;

java.util.Map headermap = con.getHeaderFields();

java.util.Set keys = headermap.entrySet();

java.util.Iterator iter = keys.iterator();

while(iter.hasNext()){

String tmp ="" + iter.next();

}

while((i = instream.read()) != -1){

inreader.append((char)i);

}

System.out.println("Server Response: " + inreader.toString());

}

[2630 byte] By [TMKa] at [2007-11-26 20:56:26]
# 1
Can nobody help me?
TMKa at 2007-7-10 2:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Well, your question has nothing at all to do with servlets. But I would suggest that calling the available() method is a bad idea in general, and it's a particularly bad idea if you expected it to return the entire content of the response.

It would also help if you described your problem. "Failed" is too brief for us to understand what was wrong.

DrClapa at 2007-7-10 2:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

OK let me explain what problem I have at the moment.

I want to write a servlet which is called by a client. The client sends xml data via a post request. If you have a POST request, then it is easy to get the data sent by the client because every param has a name and you easily can get the data by this piece of code:

String myData = request.getParameter("MyParameter");

Due to some reasons I can't use a GET request. Instead of this I want to use a POST request. The code for this request I have postet above.

My problem is that I dont know how I can retrieve the data on the servlet side because in this way I have no key-value pair.

I hope I described my problem better now and someone can tell me how to get the POST data.

Bye,

TMKTMK

TMKa at 2007-7-10 2:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Why do you need to send XML data via POST ?

XML and HTML are very different.

HTTP Post works with HTML , when you have an HTML form method="POST" , and you have HTML input fields inside the form.

There's no need to pass XML data via HTTP post, simple save the XML to the hard - disk, get the URL of the XML file and then process it with either an XML DOM parser or SAX parser. You can also convert an XML file to a JavaBean with JAXB.

If you know JSP 2.0, JSTL 1.1 and are trying to process the XML data in the displayed page, then you can easily transform the XML file with either XSLT or you can use JSTL XML tags directly in the JSP page.

Message was edited by:

appy77

appy77a at 2007-7-10 2:24:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Maybe something like this:

BufferedReader is = new BufferedReader(new InputStreamReader(request.getInputStream()));

StringBuffer xmlFull = new StringBuffer(100);

String buf = null;

while((buf = is.readLine())!= null){

xmlFull.append(buf);

}

log.debug("xmlFull: "+xmlFull.toString());

Document doc = null;

try {

doc = DocumentHelper.parseText(xmlFull.toString());

}catch(DocumentException dex ){

log.error("Doc Error: "+dex.getMessage(), dex);

} finally {

}

log.debug("doc: "+doc);

Now you would deal with an XML document, not key-value pairs, but it's better than nothing ;-)

By the way, you should add this before sending the request:

con.setRequestProperty("Content-Type", "text/xml");

Message was edited by:

benubach

Message was edited by:

benubach

benubacha at 2007-7-10 2:24:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Thanks a lot benubach, your way works!
TMKa at 2007-7-10 2:24:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Glad to hear that :-) You're very welcome :-D
benubacha at 2007-7-10 2:24:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...