1

I have a page in which I am making an ajax call, which in turn gets forwarded to a jsp page and returns a table constructed in this jsp.

Till now this jsp used to return an html which the original page(table) used to append in a particular div. But now, we have a requirement that this jsp along with the table, returns some other info about the metadat of the query.

With this change, I would ideally not like to change the existing behaviour of some clients where they are just appending the html. But then, is there a way, in which the new pages which make this call can have both the html table(which can be appended) as well as metadata returned (which can be processed).

let me know if the question isn't clear enough.

Thanks!

kshtjsnghl
  • 591
  • 3
  • 8
  • 19

1 Answers1

0

The easiest and least destructive change would be to send it as response header.

In the servlet you can use HttpServletResponse#setHeader() to set a response header:

response.setHeader("X-Metadata", metadata);
// ...

(using a header name prefixed with X- is recommended for custom headers)

In JS you can use XMLHttpRequest#getResponseHeader() to get a response header:

var metadata = xhr.getResponseHeader('X-Metadata');
// ...

You can even set some JSON string in there so that (de)serialization is easy.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Is there any other way apart from this, where we can return an html code segment with some other metadata from a jsp. – kshtjsnghl Jun 06 '11 at 14:14
  • You'd need to change the HTML response to JSON or XML and the parser to read JSON or XML instead. This involves major changes in how you get the content from the JSP separately. You'd need a `HttpServletResponseWrapper` wherein you write the JSP content to a local buffer instead of the response. – BalusC Jun 06 '11 at 14:17