We help IT Professionals succeed at work.

Testing for required input in servlet?

EmailSurfer asked
Medium Priority
Last Modified: 2010-04-01

I am using javascript in my .jsp pages to check if the user entered data in the required fields.

I wondered if most developers also tested in servlet, incase javascript did not work? Or if this was not required anymore since javascript part of most modern browsers.

I am using this code in my jsp.

<form method="post" action="loginservlet" onsubmit="return validate(this);">

Could I test in the servlet if the onsubmit was successfull?

Watch Question

You can use javascript to test user input and  this is faster too.
Sending request to server and getting back the result will take lot of time.

> incase javascript did not work?
this means that your script is not proper or a possible bug in it.

> Could I test in the servlet if the onsubmit was successfull?
you can validate in servlet also, but i would suggest u to write javascript for user input validation.
Top Expert 2004
I ALWAYS test at the servlet as well...

The javascript makes it quicker for the end user, but if that's all you're going to check, the chances of someone manipulating the script to enter code which exposes or crashes your database later on is too great to risk...

Never trust javascript.  Use it to improve the end user experience, but never trust that it has run (or has run correctly)


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

if you validate both in the front end(JSP ) using javasript and also  in the javascript its just redundancy of code.
basic of accomplishing validation is , perform all the validations in javascript , but only some validation in your servlets.

For ex: in you jsp, name, empid, salary, age etc. has to be entered  and in your javascript you are validating for each and every input , but in the servlet  its better you validate only empid since based on the empid you can retreive all the values from the database, and then if its a very important data to be validated then you can validate the user entered data against the one retrieved from the database.

Always validation entirely depends on the scenario and how important a data is.

JavaScript is always the best with respect to performance and there are various ways in whcih you can prevent your javascript code modification.

Top Expert 2004

I completely (but respectfully) disagree ;-)
I agree with Tim ;-) , best to check in a servlet.. safer and secure.. No time to hack those javascript login ;-D

-Muj ;-)



Could I ask finally,

In javascript are there any methods similar to java's indexOf, for example so I could check a email field contains a @ and . characters.

I would also further check for these in the servlet, just incase javascript is switched off.
Top Expert 2004



you will find javascript examples here
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.