Example program for validating user in JSP
In this example we have to develop a JSP application which will validate user via servlet and JSP page. We are using tomcat server for running servlet.
Validating user means to find out whether the user is an existing user. We are using two files UserValidation.jsp and Validation.java and making the application in "webapps/JSPMultipleForms" in tomcat server. UserValidation.jsp is taking input through the user and then after taking these input it will send these values to servlet where all these inputted values are checked from the data available in database. For database connection we are taking MySQL as backing database and servlet "Validation.java" is responsible for making database connection and validating input.
In this example, we are taking the MySQL database for adding values into database and this whole application is being deployed in "Tomcat web server". In our example we have taken path "//192.168.10.59/messagepaging" for connection to the database. Table for adding data is named as "User" and input values are user and password.
User Table Structure of MySQL:
|create table `user` (
`user` varchar (256),
`password` varchar (256)
insert into `user` (`user`, `password`) values('amit','kumar');
Code for validating servlet is given as below:
"Validation.java" will firstly load the driver. Here for
will do the working for us.
After loading appropriate driver servlet is making connection with database and we have stored these values from database into temporary variables.
These temporary variables are responsible for validating user input. Code for "UserValidation.jsp":
|<%@ page language="java" %>
<h2><font color="#0000FF"><b>Validation </b></font>
Please enter username and password</h2>
<form name="frm" action="/JSPMultipleForms/Validation" method="Post" >
Name: </b> <input type="text" name="user" value=""/><br>
Password:</b><input type="password" name="pass" value=""/>
<input type="submit" value="Check" />
For running and executing servlet we have to do the Servlet-mapping in the "web.xml". Code for validating servlet "web.xml" is given as below:
|<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- JSPC servlet mappings end -->
For running this example you have to follow these steps:
1.Create and Save "Validation.java".
2.Compile this java file and place the Validation.class file into classes folder.
3.Do the servlet mapping in the web.xml
4.Create and Save "UserValidation.jsp" and place it into appropriate folder.
5.Start the Tomcat Server.
6.Type following line in address bar http://localhost:8080/JSPMultipleForms/UserValidation.jsp.
If wrong input is given then message will flash "You are not a valid user".
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: July 2, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles