0

I have a client makes file upload request via dojo.io.iframe to pass a binary data to a web application running on websphere app server and the servlet class inside of the web app makes a rest web service call to an external system to pass the binary data. So there are three layers involved.

The problem I am having is trying to display returned json data which contains exact exception happened in the last layer after making rest web service call back to the client.

The client html page looks like this

<form id="myForm" method="post" enctype="multipart/form-data">
    <div id="output"></div>

    <input id="filename" type="file" name="filename" />
    <input id="submitButton" type="submit" value="Submit"></input>
</form>

The client javascript looks like this I am having trouble in printing out RuntimeException caught in a servlet to a client.

require(["dojo/io/iframe","dojo/dom","dojo/on","dojo/dom-construct","dojo/domReaddy!"],function(iframe,dom,on,domConst) {
    on(dom.byId("submitButton"),"click",function() {
        iframe.send({
            form: "myForm",
            handleAs: "json",
            url: "/rootContext/myServlet"
            }).then(function(data) {
                    domConst.place("<p>" + data + "</p>","output");
            }, function(err) {
                domConst.place("<p>" + err + "</p>","output"); // hope to print out exception caught in third layer
            });
    });
});

Servlet looks like this.

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    ....
    ServletOutputStream sos = null;
    ....
    try {
       domSomething(payLoad);
    } catch (RuntimeException e) [
        sos = response.getOutputStream();
        sos.print(e.getMessage());
    }
}

private void doSomething(String payLoad) {
    URL url = null;
    HttpURLConnection conn = null;
    OutputStream os = null;
    resource = new URL("someWhere");
    conn = (HttpURLConnection) resource.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");

    os = conn.getOutputStream();
    os.write(payLoad.getBytes());
    os.close();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
        String retMsg = null;
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        while ((retMsg = br.readLine()) != null) {
           sb.append(retMsg);
        }
        br.close();
        throw new RuntimeException(sb.toString()); // throws JSON format data
}

The servlet class makes rest web service call to an external system and if uploaded file data transmission fails it returns exception in json to the servlet class and I was trying to get that error data back to a client. This servlet class works fine for file uploading and if a corrupted file is uploaded then exception is generated. I didn't include logging mechanism in here, but it is implemented so that there is a log file in the app server that registers data transmitted included returned exception.

I am new to Dojo and having trouble using iframe api to display json data passed by a servlet class. I'd appreciate your advice on this matter. Thank you!

8
  • is your code getting to the then or error callback? Commented Sep 18, 2014 at 23:06
  • Some things I noticed: don't you have to flush() and/or close() the sos stream? If you want it to appear in the error handler, it should change the HTTP status code, probably to 500 if the cause of the exception is the server itself, not the user. The dojo/io/iframe library is not capable of retrieving the response if the request is a cross domain request, so be sure that they're on the same domain. Commented Sep 19, 2014 at 5:55
  • @tik27: i'm sorry for my late reply back. error callback gets called and at div id="output", domConst.place() print following message: "TypeError: Unable to get property 'value' of undefined or null reference". It sounds like error variable doesn't point to response object the servlet class returns... Commented Sep 19, 2014 at 13:17
  • @Dimitri M: sos stream gets closed. I didn't include full lines of instructions. All three layers are in same domain. Commented Sep 19, 2014 at 13:20
  • @tik27: odd part is if I wrap josn data in <html>..</html> tag and set handleAs : "html" then function(data) { .. } gets called however with message like "object undefined". Commented Sep 19, 2014 at 13:53

1 Answer 1

1

first of all I'd appreciate others who have contributed ideas to my question. The problem I was having fixed. I had to do following steps.

In the servlet class, wrap json data by html/body/textarea tags

From client side javascript, set handleAs: "json"

function(data) will be invoked and data would be json object so call whatever property name via . then mapping value will be printed out

Came to right solution via many trials/errors. One of the mistake I made was I used wrong slash for closing textarea tag by mistake in the servlet class.

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

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.