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 in JSP


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 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 "" 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 "//" 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:


import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Validation extends HttpServlet{

  private ServletConfig config;
  public void init(ServletConfig config)
  throws ServletException{
  public void doPost(HttpServletRequest request, HttpServletResponse response
  PrintWriter out = response.getWriter();
  String connectionURL = "jdbc:mysql://";
  Connection connection=null;
  ResultSet rs;
  String userName=new String("");
  String passwrd=new String("");
  try {
 // Load the database driver
  // Get a Connection to the database
  connection = DriverManager.getConnection(connectionURL, "root""root")
  //Add the data into the database
  String sql = "select user,password from User";
  Statement s = connection.createStatement();
  s.executeQuery (sql);
  rs = s.getResultSet();
  while ( ()){
  rs.close ();
  s.close ();
  }catch(Exception e){
  System.out.println("Exception is ;"+e);
  && passwrd.equals
  out.println("User is Valid");
  out.println("You are not a Valid User");

"" will firstly load the driver. Here for MySQL  "Class.forName("com.mysql.jdbc.Driver");" 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":

2. UserValidation.jsp

<%@ page language="java" %>
<h2><font color="#0000FF"><b>Validation&nbsp;</b></font>
Please enter username and password</h2>
<form name="frm" action="/JSPMultipleForms/Validation" method="Post" >
Name:&nbsp;</b>&nbsp;&nbsp; &nbsp;&nbsp;<input type="text" name="user" value=""/><br>
Password:</b><input type="password" name="pass" value=""/>
&nbsp;<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"?>

<web-app xmlns=""

<display-name>Welcome to Tomcat</display-name>
Welcome to Tomcat

<!-- JSPC servlet mappings start -->



<!-- JSPC servlet mappings end -->


For running this example you have to follow these steps:

1.Create and Save "".
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".

Download SourceCode

Share on Google+Share on Google+

Validating User in JSP

Posted on: July 2, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Validating User in JSP   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 13, 2012

May 1, 2012

thank u .. it helped a lot
October 27, 2012
Registering users.

When registering new users is it ok to validate their input and store it in the DB all from one single servlet?