Testing for required input in servlet?


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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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.

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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 ;-)
EmailSurferAuthor Commented:

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.

you will find javascript examples here
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.