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

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

1. Validation.java

import java.io.*;
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{
 this.config=config;
 }
  public void doPost(HttpServletRequest request, HttpServletResponse response
  throws 
ServletException,IOException{
  
  PrintWriter out = response.getWriter();
  String connectionURL = "jdbc:mysql://192.168.10.59/messagepaging";
  Connection connection=null;
  ResultSet rs;
  String userName=new String("");
  String passwrd=new String("");
  response.setContentType("text/html");
  try {
 // Load the database driver
  Class.forName("com.mysql.jdbc.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.next ()){
  userName=rs.getString("user");
  passwrd=rs.getString("password");
  }
  rs.close ();
  s.close ();
  }catch(Exception e){
  System.out.println("Exception is ;"+e);
  }
  if(userName.equals(request.getParameter("user")) 
  && passwrd.equals
(request.getParameter("pass"))){
  out.println("User is Valid");
  }
  else{
  out.println("You are not a Valid User");
  }
  }
}  

"Validation.java" 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>
<br>
<br>
Please enter username and password</h2>
<form name="frm" action="/JSPMultipleForms/Validation" method="Post" >
<b>
Name:&nbsp;</b>&nbsp;&nbsp; &nbsp;&nbsp;<input type="text" name="user" value=""/><br>
<b>
Password:</b><input type="password" name="pass" value=""/>
&nbsp;
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;<input type="submit" value="Check" />
</p>
</form>

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="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

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

<!-- JSPC servlet mappings start -->


<servlet>
<servlet-name>Validation</servlet-name>
<servlet-class>Validation</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Validation</servlet-name>
<url-pattern>/Validation</url-pattern>
</servlet-mapping>

<!-- JSPC servlet mappings end -->

</web-app>

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.

Output:

If wrong input is given then message will flash "You are not a valid user".

Download SourceCode