开发者

Design question: Serverside form validation using ajax and Servlet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question

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).

  1. Wouldn't the first JSP's ajax call be left hanging waiting for some kind of response?
  2. 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.)

  3. 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

  1. Ajax Call 1: validate form. (send back success or error message)
  2. if ajax call returns success then submit form normally. (I would have t开发者_高级运维o do validations twice if I use this method which is inefficient.)


  1. Yes, it would be left hanging. Return a 1 or something from the Ajax call to give it the green light to proceed

  2. It is ok in the sense of server resources, but your user will be left hanging until a timeout is reached. Very bad practice.

  3. Struts or Spring validation layers will do exactly what you want to do. Most of the major frameworks provide this functionality


Don't reinvent the wheel, someone else has already solved this problem and done it well.

You can use jQuery to perform form validation; it supports browser based validation (numeric/alpha/length/regex etc) and you can also get it to perform server side validation for example by sending the form contents to a web service and listening for the response.

It is also highly recommended to perform the same validation at the server side when the form is submitted as all browser based/ajax validation can be bypassed by simply disabling javascript or by manually constructing the POST request for the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜