简体   繁体   中英

Partitioning a JSP page accessed through an ajax call

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM