Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an HttpServletRequest to String?

Tags:

java

servlets

How can I convert an HttpServletRequest to String? I need to unmarshall the HttpServletRequest but when I try to, my program throws an exception.

 javax.xml.bind.UnmarshalException
 - with linked exception:
[java.io.IOException: Stream closed]
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:197)
        at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:168)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
        at com.orange.oapi.parser.XmlParsing.parse(XmlParsing.java:33)

I tried the following code to unmarshall the HttpServletRequest.

InputStreamReader is =
                new InputStreamReader(request.getInputStream());
InputStream isr = request.getInputStream();
ServletInputStream req = request.getInputStream();

My parser method:

public root parse(InputStreamReader is) throws Exception {
        root mc = null;
        try {
            JAXBContext context = JAXBContext.newInstance(root.class);
            Unmarshaller um = context.createUnmarshaller();
            mc = (root) um.unmarshal(is);
        } catch (JAXBException je) {
            je.printStackTrace();
        }
        return mc;
    }
like image 390
BKK Avatar asked Sep 04 '12 09:09

BKK


People also ask

What is the difference between ServletRequest and HttpServletRequest?

The ServletRequest and HttpServletRequest classes hold all of the accessible information about the client and the server. HttpServletRequest is a subclass of ServletRequest, and is passed to each servlet handler method (such as doGet(..), doPut(..), etc.)

What is an HttpServletRequest?

The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.

What is HttpServletRequest and HttpServletResponse?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.

How is HttpServletRequest created?

In a typical Servlet container implementation if an HTTP request comes in, an HttpServletRequest is created right when the HTTP input data of the request is parsed by the Servlet container.


2 Answers

I have the impression you're trying to read from the input stream after you've handled the request and responded to your client. Where did you put your code?

If you want to handle the request first, and do the unmarshalling later, you need to read the inputstream into a String first. This works fine if it's small requests you're handling.

I suggest using something like apache commons IOUtils to do this for you.

String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream());

Also keep in mind that you have to choose between request.getParameter(name) and request.getInputStream. You can't use both.

like image 131
Joeri Hendrickx Avatar answered Sep 18 '22 13:09

Joeri Hendrickx


String httpServletRequestToString(HttpServletRequest request) throws Exception {

    ServletInputStream mServletInputStream = request.getInputStream();
    byte[] httpInData = new byte[request.getContentLength()];
    int retVal = -1;
    StringBuilder stringBuilder = new StringBuilder();

    while ((retVal = mServletInputStream.read(httpInData)) != -1) {
        for (int i = 0; i < retVal; i++) {
            stringBuilder.append(Character.toString((char) httpInData[i]));
        }
    }

    return stringBuilder.toString();
}
like image 30
Vikash Kumar Verma Avatar answered Sep 16 '22 13:09

Vikash Kumar Verma