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!
flush()and/orclose()thesosstream? 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. Thedojo/io/iframelibrary 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.