Question

I am using Java and JSP to develop my web application.

There are many situations in which user input cause an exception on server side, in this situation user expects an error message to be shown. There are several ways that I (as a programmer) can handle the transfer of the error message from server to GUI(html) and maybe redirect to other pages and show the message there. One way to go is using session attribute, other can be ajax method calls and showing the response text. There may be other ways that I don't know. Would you tell me what is the most straight forward and standard way (if there is a standard say about this)?


The problem that made me think about this was when I was trying to show the response of an ajax call in a colorbox, and I failed, it would be appreciated if you give me a hint on this too, how can I pass the response text of an ajax method in other web page.

Was it helpful?

Solution

A way to pass messages from server to HTML is to write them in HTML. HTML is generated by server.

There are other possibilities as well, such as using query params, cookies, ajax calls etc, but if you have no special requirements you should just write the message in the output HTML.

Within the server implementation you could use request attributes, for example. But that's communication within the server application, not between UI and server.

These kind of messages should usually be either a) in response to POST request, where you have redirected to an error page with relevant info in the error page url, making the page cacheable or b) direct response to request parameters in a GET request, so same params would result in same output.

OTHER TIPS

one way to go is using session attribute, other can be ajax

A third option I can think of is a URL redirect and the new URL may contain parameters

ie.

yourdomain.com/page.jsp?name=Joe&error=Empty%20Field

I think the standard way of handling these scenarios is through AJAX, as the OP in this question is doing. Take note of the error section in the JQuery ajax method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top