Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

validate form and show errors in same jsp  RSS feed

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP which will let a user register for an account at my website. If the user submits wrong or illegal info into the JSP,
then I want to return the same JSP with an appropriate error message next to/above each wrongly filled (form) fields.

If possible, highlight the wrongly filled form field - this feature is not necessary though.

I have given a sample below to show what I need. I understand that the sample must be using something like javascript, but
I don't know all that client side scripting. I only want to use JSP to do it. As I said, I want to sort of return the JSP form to the
user after marking all the mistakes and how to correct them.

How do I do this ?

Thanks.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As always design the HTML that will give the results you want. Then, write JSP code to create that HTML.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:As always design the HTML that will give the results you want. Then, write JSP code to create that HTML.


I tried this -

JSP -




RegServlet -

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thingking of an approach. Create a Form Bean. It has field String user, boolean isUserExists = false and other things. If verification servlet sees that the user exists already, then it sets the bean isUserExists = true. The JSP displays an message (i.e error message) only when isUserExists = true for a form bean. Makes sense ? Would this be a good way ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too implicit for my tastes. Firstly, I wouldn't be doing this without JavaScript. Making a round-trip just to change the style of an element went out of vogue long long ago.

That said, if I were going to write this as if it were 10 years ago, I'd send an explicit list of fields that are in error to the JSP, and use a CSS class to "mark" those fields for styling.

Your HTML design doesn't do anything to show a field in error. You can't do anything in JSP until you know what the final markup needs to be.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me what the following mean.

Bear Bibeault wrote:
Too implicit for my tastes.


Bear Bibeault wrote:
Your HTML design doesn't do anything to show a field in error. You can't do anything in JSP until you know what the final markup needs to be.


Thanks.
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 2 types of form validation exists.

1. Client side validation 2. Server side validation.

Client side validation can be done using client side technologies like java script. However, to make your application robust, you should always validate user inputs in server side with/out client side validation.

Now, coming to your requirement, say you want to validate user id and pwds. User id and pwds cant be blank and so you can validate it upfront using java script. However, there are ways to skip java script validation and user can still submit the form with blank user id and pwds. Here comes the server side validation. You should not rely of client side validation and validate the form again using java or whatever technology you are using. And return the error message and display it in JSP using custom tags.

For example, if you go to paypal and try to sign up as business user with invalid user input, the error messages are shown along side the fields. They are using custom tags to display the error messages in the jsps.

Edit: It looks like PayPal have changed the validation display mechanism for some flows. You can find another example in example .



 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something that web frameworks such as Struts 2, SpringMVC or Stripes can do for you. Are you using any framework?
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create your HTML with all the possible errors already there. Style them as invisible unless a certain request attribute is present. Then, in your JSP, you validate, set the necessary request attributes and redirect to the same page if validation fails.

This is how you do it. Though I would also validate client-side. In JS, it's quite simple.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:This is something that web frameworks such as Struts 2, SpringMVC or Stripes can do for you. Are you using any framework?


Or JSF. Which was designed precisely to behave the way you wanted.
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dieter Quickfend wrote:Create your HTML with all the possible errors already there. Style them as invisible unless a certain request attribute is present. Then, in your JSP, you validate, set the necessary request attributes and redirect to the same page if validation fails.

This is how you do it. Though I would also validate client-side. In JS, it's quite simple.


Thanks. Is there any example for this ? I can make the code myself, but why do it when it already exists ? Are there any drawbacks in this approach ?
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amar Saikia wrote:There are 2 types of form validation exists.

1. Client side validation 2. Server side validation.

Client side validation can be done using client side technologies like java script. However, to make your application robust, you should always validate user inputs in server side with/out client side validation.

Now, coming to your requirement, say you want to validate user id and pwds. User id and pwds cant be blank and so you can validate it upfront using java script. However, there are ways to skip java script validation and user can still submit the form with blank user id and pwds. Here comes the server side validation. You should not rely of client side validation and validate the form again using java or whatever technology you are using. And return the error message and display it in JSP using custom tags.

For example, if you go to paypal and try to sign up as business user with invalid user input, the error messages are shown along side the fields. They are using custom tags to display the error messages in the jsps.

Edit: It looks like PayPal have changed the validation display mechanism for some flows. You can find another example in example .




+1. Thanks for the answer and also for an example. How do you know they/xomidhan are using custom tags ? I cannot see them in view source for that page. I did notice that the url changes from
http://xomidhan.org/Question.do?m=1 to http://xomidhan.org/QuestionProcess.do;jsessionid=9213FB7DB0F7F00D990527020E3BA88E , when I enter junk info and send it.
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you know they/xomidhan are using custom tags ? I cannot see them in view source for that page


Because I was involved in creating those custom tags in both sites . Of course you wont see custom tags in view source of a page. Here custom tags were used to generate condition based HTML markup.
 
Anindya Roy
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can take following approach

1. you have a form in jsp page
2. you are getting request values in servlet
3. you are validating each field in servlet
4. if you find any validation error for any field then put that in a attribute using request.setAttribute("field1","field1 error message") and forward to the same jsp page. Get the error message from the request object in jsp page and display
5. if all validations pass for all fields then you can take user to the home page after login


hope this helps
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!