Search from Database In Servlet

In this section, we have developed an Employee Search application. We created three file search.jsp, viewSearch.jsp and

Search from Database In Servlet

Search from Database In Servlet


In this section, we have developed an Employee Search application. We created three file search.jsp, viewSearch.jsp and 

Brief description of the flow of the application: 

  • User opens search.jsp in the browser and enter the search text  and click on the "Search" button. 
  • retrieves data from the database according to the Search text.
  • viewSearch.jsp display the search data from database on the browser. 

Step 1: Create a web page ("search.jsp") to Search  the user.

<form method="post" name="frm" action="search">
<table border="0" width="300" align="center" bgcolor="#CDFFFF">
<tr><td colspan=2 style="font-size:12pt;color:#00000;" align="center">
<h3>Search Employee</h3></td></tr>
<tr><td ><b>Employee Name</b></td>
<td>: <input  type="text" name="emp_name" id="emp_name">
<tr><td ><b>Department</b></td>
<td>: <input  type="text" name="emp_dept" id="emp_dept">
<tr><td ><b>Email</b></td>
<td>: <input  type="text" name="email" id="email">
<tr><td colspan=2 align="center">
<input  type="submit" name="submit" value="Search"></td></tr>

Step:2Create a Servlet ("")  to retrieve the data.

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

public class Search extends HttpServlet{ 
 public void doPost(HttpServletRequest request, 
  HttpServletResponse response)
  throws ServletException,IOException{
  PrintWriter out = response.getWriter();

  System.out.println("MySQL Connect Example.");
  Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "userdetails";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "root";

  Statement st;
  try {
  conn = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  String  emp_name  = request.getParameter("emp_name");
  String  emp_dept  = request.getParameter("emp_dept");
  String  email  = request.getParameter("email");

  ArrayList al=null;
  ArrayList emp_list =new ArrayList();
  String query = 
  "select * from employee where employee_name='"+emp_name+"' or 
   department='"+emp_dept+"' or email='"+email+"' 
  order by employee_name";
  System.out.println("query " + query);
  st = conn.createStatement();
  ResultSet  rs = st.executeQuery(query);

  al  = new ArrayList();
  System.out.println("al :: "+al);

 System.out.println("empList " + emp_list);

  // out.println("emp_list " + emp_list);

  String nextJSP = "/viewSearch.jsp";
  RequestDispatcher dispatcher = 
  System.out.println("Disconnected from database");
  } catch (Exception e) {

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);

The ServletContext.getRequestDispatcher() method takes a String argument describing a path within the scope of  the ServletContext. This path must be relative to the root of the ServletContext and begin  with a '/'. The method uses the path to look up a Servlet, using the Servlet path matching rules, wraps it with a RequestDispatcher object, and returns the resulting object. If no Servlet can be resolved based on the given path, a RequestDispatcher is provided that returns the content for that path.

Step 4: "web.xml" to  mapping  the Servlet  ("")  

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns=""

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

Step 4: "viewSearch.jsp" to  display the data. 

<%@ page import="java.util.*" %> 

<table width="700px" align="center"
style="border:1px solid #000000;">
<td colspan=8 align="center"
<b>Employee Record</b></td>
<tr style="background-color:efefef;">
<td><b>Employee Name</b></td>
<td><b>Contact No</b></td>

int count=0;
String color = "#F9EBB3";

ArrayList al = (ArrayList)request.getAttribute("empList");
Iterator itr = al.iterator();


color = "#eeffee";
color = "#F9EBB3";
ArrayList empList = (ArrayList);
<tr style="background-color:<%=color%>;">
<td colspan=8 align="center"
style="background-color:eeffee"><b>No Record</b></td>

Output of this Program:

search.jsp to search the Employee

After search the result.

If no record in the database then display No Record.

Download the application