Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validation of the JSP page using Servlets

    I have written a jsp page and I have done the client side validation using java script. But now i am asked to do the server side validation in servets . Please help with the code. I tried many ways , but i am unable to do that. Please rescue

    register.jsp
    <%--
    Document : test
    Created on : Nov 1, 2010, 3:41:06 PM
    Author : db2admin
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>

    <title> Registration </title>

    <script type="text/javascript">

    function checkWholeForm(theForm)
    {
    var why = "";

    why += checkname(theForm.username, "user name");
    why += checkPassword(theForm.password);
    if ( theForm.password.value != theForm.password2.value )
    {
    why += "The two passwords do not match. Please enter the same password twice.\n";
    }
    why += checkname(theForm.firstname, "first name");
    why += checkname(theForm.lastname, "last name");
    why += checkEmail(theForm.email);
    why += checkPhone(theForm.phno);

    if (why != "")
    {
    alert(why);
    return false;
    }
    return true;
    }

    function checkname(fld, name)
    {
    var val = fld.value;
    if ( val.length < 3 || val.length > 10 )
    {
    fld.focus();
    return "You must enter a " + name + " minimum 3 characters long.\n";
    }

    return "";
    }

    function checkPassword(fld)
    {
    var val = fld.value;
    if ( val.length < 4 || val.length > 10 )
    {
    fld.focus();
    return "You must enter a password that is atleast 5 characters long.\n";
    }

    return "";
    }

    function checkEmail(fld)
    {
    var val = fld.value;
    //relational algebra standard for checking the emails
    var emailFilter=/^[\w\-\'\.]+\@([\w\-\']+\.)+[a-z]{2,6}$/i;
    if ( ! (emailFilter.test(val)) )
    {
    fld.focus();
    return "Please enter a valid email address.\n";
    }
    return "";
    }

    function checkPhone(fld)
    {
    var val = fld.value;

    // strip out ALL non-numeric characters
    var ph = val.replace(/[^\d]/g, "");


    if ( ph.length != 10 )
    {
    fld.focus();
    return "The phone number must contain 10 digits.\n";
    }

    return "";
    }
    </script>

    </head>

    <body bgcolor="lightblue">
    <form name="myform" method="post" action="SuccessfulRegistration.jsp" onsubmit="return checkWholeForm(this);">
    <form name="myform" method="post" action="/servlet/validates"
    <center>
    <table>
    <tr>
    <center> <th><h1 style="font-family:arial;color:black;font-size:20px;"><i>eOnline Auction</i></h1></th></center>
    </tr>
    </table>
    <br>
    <br>
    <br>
    <br>
    <table border="1" cellspacing="1" cellpadding="0" align="center" width=300>
    <tr>
    <td colspan="2" bgcolor="lightblue"><center><b>Registration Form</b></center></td>
    </tr>
    <tr>
    <td>UserName</td>
    <td><input type="text" size="20" name="username"></td>
    </tr>
    <tr>
    <td>Password </td>
    <td><input type="password" name="password"></td>
    </tr>
    <tr>
    <td>Confirm password </td>
    <td><input type="password" name="password2"></td>
    </tr>
    <tr>
    <td> FirstName </td>
    <td><input type="text" name="firstname"></td>
    </tr>
    <tr>
    <td> LastName </td>
    <td><input type="text" name="lastname"></td>
    </tr>
    <tr>
    <td> Email </td>
    <td><input type="text" name="email"></td>
    </tr>
    <tr>
    <td> Address </td>
    <td><input type="text" name="address"></td>
    </tr>
    <tr>
    <td> Street </td>
    <td><input type="text" name="street"></td>
    </tr>
    <tr>
    <td> City </td>
    <td><input type="text" name="city"></td>
    </tr>
    <tr>
    <td> State </td>
    <td><input type="text" name="state"></td>
    </tr>
    <tr>
    <td> Zipcode </td>
    <td><input type="text" name="zipcode"></td>
    </tr>
    <tr>
    <td> Phonenumber </td>
    <td><input type="text" name="phno"></td>
    </tr>
    <tr>
    <td colspan ="2"><center><input type ="submit" value = "Register"></center></td>
    </tr>
    </table>
    </center>
    </form>

    </body>
    </html>

  • #2
    New Coder
    Join Date
    Aug 2009
    Location
    Cochin,India
    Posts
    39
    Thanks
    2
    Thanked 1 Time in 1 Post

    Smile

    Server side validation is required because client side validation may be bypassed by disabling javascript . But you really mean to do server side validation in a seperate Servlet ?? You could do it in the same jsp page... Only one <form> tag is needed in your jsp page

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,958
    Thanks
    120
    Thanked 76 Times in 76 Posts
    will give you a short example how to do it:

    say you have this div on top of everything that you already have, providing that action on your <form redirects to that same form:
    Code:
    <div>
    <% 
          /*will pick up your street*/    
          String street = request.getParameter("street");
          if  street.equals("")
          {       
             out.print("error: street must be entered, data not submited !");
          }
          else
          {  
             //do saving to database here      
             out.print("data saved")
          }
    
    %>
    </div>
    Last edited by BubikolRamios; 11-03-2010 at 08:13 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BubikolRamios View Post
    will give you a short example how to do it:

    say you have this div on top of everything that you already have, providing that action on your <form redirects to that same form:
    Code:
    <div>
    <% 
          /*will pick up your street*/    
          String street = request.getParameter("street");
          if  street.equals("")
          {       
             out.print("error: street must be entered, data not submited !");
          }
          else
          {  
             //do saving to database here      
             out.print("data saved")
          }
    
    %>
    </div>
    The author has asked for the validation made in a servlet. Your code looks more like a scriplet (because the validation is done inside of the jsp), doesn't it?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •