bhomass
asked on
pick a body of http post in servlet
I have a java app in which I posted a long text message to a servlet app.
I looked thru the HttpServletRequest class. it is not obvious how I pick up the text I sent. the text data is not in an attribute, since I did not do a form post.
the HttpServletRequest object has a inputbuffer, which seems to be full of data. but how do I get to it?
I looked thru the HttpServletRequest class. it is not obvious how I pick up the text I sent. the text data is not in an attribute, since I did not do a form post.
the HttpServletRequest object has a inputbuffer, which seems to be full of data. but how do I get to it?
could you post the code now
ASKER
client side I did
m_url = new URL ("http://"+SERVER+"/" + PROJPATH +"/"+ NEWREQUEST);
m_urlConn = m_url.openConnection();
m_urlConn.setDoInput (true);
m_urlConn.setDoOutput (true);
m_urlConn.setUseCaches (false);
// Specify the content type.
// urlConn.setRequestProperty
// ("Content-Type", "application/x-www-form-ur lencoded") ;
// Send POST output.
m_req_os = new DataOutputStream (m_urlConn.getOutputStream ());
String content =
"lots lots of text";
m_req_os.writeBytes (content);
m_req_os.flush ();
server side I have
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
this.request = request;
// herer is my question. How do I get content sent by the client?
}
m_url = new URL ("http://"+SERVER+"/" + PROJPATH +"/"+ NEWREQUEST);
m_urlConn = m_url.openConnection();
m_urlConn.setDoInput (true);
m_urlConn.setDoOutput (true);
m_urlConn.setUseCaches (false);
// Specify the content type.
// urlConn.setRequestProperty
// ("Content-Type", "application/x-www-form-ur
// Send POST output.
m_req_os = new DataOutputStream (m_urlConn.getOutputStream
String content =
"lots lots of text";
m_req_os.writeBytes (content);
m_req_os.flush ();
server side I have
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
this.request = request;
// herer is my question. How do I get content sent by the client?
}
In the serverSide, try adding this code:
ServletInputStream in = request.getInputStream();
...
ServletInputStream in = request.getInputStream();
...
ASKER
I tried that. followed by
ServletInputStream is = request.getInputStream();
byte buf[] = new byte[1000];
String contentString = "";
int nread = 0;
while (-1!=(nread=is.read(buf))) {
contentString += new String(buf, 0, nread);
}
but, I got 0 char's out of the input strream???
ServletInputStream is = request.getInputStream();
byte buf[] = new byte[1000];
String contentString = "";
int nread = 0;
while (-1!=(nread=is.read(buf)))
contentString += new String(buf, 0, nread);
}
but, I got 0 char's out of the input strream???
ASKER
ok, I added "body=" in front of the content in the client, and got it from request.getParameter("body ") in the server;
not as elegant as I wanted, but this is good enough for me.
not as elegant as I wanted, but this is good enough for me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.