aspose file tools*
The moose likes Servlets and the fly likes Server side validation to be shown in Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Server side validation to be shown in Javascript" Watch "Server side validation to be shown in Javascript" New topic
Author

Server side validation to be shown in Javascript

Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
Hi,
My program flow is a typical ...JSP-->Servlet-->EJB-->DB
I have to do a validation that if a user enters a Qty on my JSP page,I have to check if that Qty is in stock(DB) or not. If the Qty is not in the stock I have to put a javascript ala.. pop-window saying "oops! the ordered qty is more"..

How do I proceed without comprimising MVC?? I know it is common validation,its just that my brain stopped working on Friday eve...

I will appreciate any points...
Thanks
Rajani
Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
You may pass a variable to your jsp file and do something like this.

<%
if( invalid_value){
%>
alert("oops");
<%
}
%>

Not sure if this is the right answer for you.

Kevin
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
Its on the same page, what I mean by that is there is only one order page. it should similate like a client side verification, like we generally do. But in my situation , the data verfication is done form server side(the Qty is more or less).
If the pop up window says , your ordered Qty is more than what we have in stock, then user will change that Qty.
SO what I mean is the page is the same.
Hope I was clear or I made worse
Thanks for the quick reply though...
Rajani
Rajiv Babu
Greenhorn

Joined: Nov 23, 2004
Posts: 12
There are 2 ways of doing it,

1. Validating at the client side using javascript
2. Validating at the server side.

In the former you have to pass the value to the jsp when the page is loaded. Say if qty A you have 40 pieces, pass 40 and store it in a javascript variable on load of the page and validate on submit if user has entered the right quantity.

The above method will need code change at both client side and server side if new items are added are deleted.

Best thing to do would be to validate at the server side. Call a validation class from your servlet and based on success or failure return the user to same page/next page. This is extensible also because you can fetch all items from the db and validate the corresponding quantities against the values entered by the user. So in future if new items are added/items deleted you wont have to change the code.
Kev lam
Greenhorn

Joined: Mar 04, 2005
Posts: 14
Even you use the same jsp file to submit data to validate and display the result you can still do this.

On the server side, after the validation, you may put a boolean variable or an object in the request object to indicate if the data is passed the validation. In your jsp file, you may get that variable from request and check it in javascript code.

I hope I understand your question.

Thanks.
kevin
Bryan Hughes
Greenhorn

Joined: Feb 16, 2004
Posts: 10
When you detect the error in your DB layer, why not throw an exception that is then caught in your servlet? In the servlet's catch block, add an attribute to either the session or the request and then forward back to the JSP. The JSP then looks for that value in the session or request and kicks off the corresponding Javascript.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Server side validation to be shown in Javascript