Testing for required input in servlet?

Posted on 2006-03-19
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?

Question by:EmailSurfer
    LVL 6

    Expert Comment

    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.
    LVL 35

    Accepted Solution

    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)

    LVL 3

    Assisted Solution


    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.

    LVL 35

    Expert Comment

    I completely (but respectfully) disagree ;-)
    LVL 7

    Expert Comment

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

    -Muj ;-)

    Author Comment


    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.
    LVL 35

    Expert Comment

    LVL 6

    Expert Comment


    you will find javascript examples here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Outlook Free & Paid Tools
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now