Currently coding a very simple web application where I am building the framework from scratch ( MVC design) .
I do not have issues with the code, my questions is related to the design.
I will explain the scenario what I am trying to do.
Planning to do the following:
In a JSP I have a html form for user input and a button which will submit it.
Click the submit button:
An ajax call will be made to an Initial Processing Servlet, whose task is to go through every input paramter and validate them (server side validation).
The reason I want to use ajax, is because I want to be able to display the “error messages after input validation” without actually refreshing the page
In the Initial Processing Servlet:
If there are any input validation error (ex:wrong email format, invalid date etc), then send back response to the parent JSP with the error messages.
If no validation error was encountered, then forward request to the next Servlet which will in turn process the request and display the next JSP.
My Questions:
Assume there were no validation errors in the form input. (This is where my dilemma arises).
- Wouldn’t the first JSP’s ajax call be left hanging waiting for some kind of response?
-
Is it OK that that we don’t plan on sending back a response to an ajax call?
(I am assuming that these requests might eventually timeout. But I just feel uncomfortable about this.) -
Is there a better way to do what I am trying to do?
PS:
I don’t want to make two separate calls to the server when submit button is clicked. Explained below
- Ajax Call 1: validate form. (send back success or error message)
- if ajax call returns success then submit form normally. (I would have to do validations twice if I use this method which is inefficient.)
Yes, it would be left hanging. Return a 1 or something from the Ajax call to give it the green light to proceed
It is ok in the sense of server resources, but your user will be left hanging until a timeout is reached. Very bad practice.
Struts or Spring validation layers will do exactly what you want to do. Most of the major frameworks provide this functionality