13

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;
    }
5
  • 2
    Do you want the Request, a specific HTTP header, the body, ...? Commented Sep 4, 2012 at 9:32
  • hi i want to convert the HttpServlet request into String. Commented Sep 4, 2012 at 9:37
  • Please add an example of how such a String might look like. Commented Sep 4, 2012 at 9:38
  • What exactly are you trying to do? You're writing about converting a request to String but the code you're showing us involves using JAXB to create an object of the class root by parsing the request body. On a side note, you should start the names of your classes with capital letters. Commented Sep 4, 2012 at 9:40
  • First of, i am trying to get a httpServletRequest from client. then i am trying to convert the httpServletRequest to String. In my parsing method i am using JAXB for unmarshall. so for parsing i need to change my httpServletRequest to string. Now am struggling to convert the httpServletRequest to String. Commented Sep 4, 2012 at 9:50

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi when i try to add a line String marshalledXml = IOUtils.toString(request.getInputStream()); it is showing an error message like "The method toString() in the type Object is not applicable for the arguments (ServletInputStream)"
Are you sure you have the IOUtils from org.apache.commons.io? It's a supported feature since v1.0: commons.apache.org/io/api-1.4/org/apache/commons/io/…
By adding a jar file i can able to write String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream()); but this is affecting my DOM parser. but nowhere am using this line in my DOM parser. Is this the only way to convert httpServletRequest to String. Is there any other way?
You could read the input stream and put it into a string yourself. But if your DOM parser is so unstable that it falls apart when you add a jar, you might have more serious issues...
If i taken off the line String marshalledXml = org.apache.commons.io.IOUtils.toString(request.getInputStream()); then my DOm parser is working fine. But still i am using jar file in my project. is there any otherway to convert httpServletRequest to String.
|
3
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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.