Link to home
Start Free TrialLog in
Avatar of bhomass
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?
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

could you post the code now
Avatar of bhomass
bhomass

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-urlencoded");

                // 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();
...
Avatar of bhomass

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???
Avatar of bhomass

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.
ASKER CERTIFIED SOLUTION
Avatar of CetusMOD
CetusMOD
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial