Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-02-2010, 04:30 PM   PM User | #1
arvind368
New to the CF scene

 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
arvind368 is an unknown quantity at this point
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>
arvind368 is offline   Reply With Quote
Old 11-03-2010, 05:33 PM   PM User | #2
sujithpr
New Coder

 
Join Date: Aug 2009
Location: Cochin,India
Posts: 39
Thanks: 2
Thanked 1 Time in 1 Post
sujithpr is an unknown quantity at this point
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
sujithpr is offline   Reply With Quote
Old 11-03-2010, 08:10 PM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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>
__________________
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

Last edited by BubikolRamios; 11-03-2010 at 08:13 PM..
BubikolRamios is offline   Reply With Quote
Old 03-25-2013, 08:52 AM   PM User | #4
jvyrus
New to the CF scene

 
Join Date: Mar 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
jvyrus is an unknown quantity at this point
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?
jvyrus is offline   Reply With Quote
Reply

Bookmarks

Tags
servlets, validation

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:46 PM.


Advertisement
Log in to turn off these ads.