This form based application can be used to searching multiple text fields.This is mainly used for Filtering the records base on DataBase.This Form is mainly for searching the records text by text entering it's based on entering text fields.

step1:create table empTable(empId int(10),empName varchar(20),empSalary decimal(20));

 step2: develop a index.jsp file and it forwarded to search.jsp


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

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Page</title>

    </head>

    <body>

        <h1>Employee Data</h1>

        <form name="empForm" action="search.jsp" method="post">
            <div>

                <table border="1">

                    <thead>

                        <tr>

                            <th style="alignment-adjust: auto">Employee Id:</th>

                        <td><input type="text" name="empId" value="" size="20" /></td>

                        </tr>

                        <tr>

                            <th>Employee Name :</th>

                             <td><input type="text" name="empName" value="" size="20" /></td>

                        </tr>

                        <tr>

                            <th>Employee Salary :</th>

                             <td><input type="text" name="empSalary" value="" size="20" /></td>

                        </tr>

                        <tr>

               <td colspan="3" >

               <center> <input type="submit" value="GetData" /></center>

                                </td>

                        </tr>

                    </thead>

                </table>

            </div>

        </form>

    </body>

</html>

 

  step3: Enter a any value to any one text field.If  user enetrdata is matched into DataBase .we can get a data.Otherwise,No records found in DataBase.

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

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ page isELIgnored="false"%>
<%--
    Document   : index
    Created on : Sep 5, 2013, 9:25:13 AM
    Author     : USER
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

   <sql:setDataSource var="emp" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/ashok"
     user="root"  password=""/>
        <h1>Employee Data</h1>
        <form name="empForm" action="search.jsp" method="post">
          
            <div>
                <table border="1">
                    <thead>
                        <tr>
                            <th style="alignment-adjust: auto">Employee Id:</th>
                        <td><input type="text" name="empId" value="<%=request.getParameter("empId")%>" size="20" /></td>
                        </tr>
                        <tr>
                            <th>Employee Name :</th>
                             <td><input type="text" name="empName" value="<%=request.getParameter("empName")%>" size="20" /></td>
                        </tr>
                        <tr>
                            <th>Employee Salary :</th>
                             <td><input type="text" name="empSalary"
                                        value="<%=request.getParameter("empSalary")%>" size="20" /></td>
                        </tr>
                        <tr>
                            <td colspan="3" >
                    <center> <input type="submit" value="GetData" /></center>
                                </td>
                        </tr>
                    </thead>
                </table>
 <table border="1">
                    <thead>
                        <tr>
                            <th>Emp Id</th>
                            <th>Emp Name</th>
                            <th>Emp Salary</th>
                        </tr>
                    </thead>
                    <tbody>
         
                        <%
   out.println("select *from emptable emp where emp.empId like '%"+request.getParameter("empId")+"%' and "
           + "               emp.empName like '%"+request.getParameter("empName")+"%' and"
           + " emp.empSalary like '%"+request.getParameter("empSalary")+"%'   ");
  
   out.println("select count(*) from emptable");
                            %>
                    <sql:query var="empData" dataSource="${emp}">
                        select *from emptable emp where
                        emp.empId like '%<%=request.getParameter("empId")%>%'
                        and emp.empName like '%<%=request.getParameter("empName")%>%' and
emp.empSalary like '%<%=request.getParameter("empSalary")%>%'
                    </sql:query>


 
                       <c:forEach var="row" items="${empData.rows}">
<tr>
   <td><c:out value="${row.empId}"/></td>
   <td><c:out value="${row.empName}"/></td>
   <td><c:out value="${row.empSalary}"/></td>
 
</tr>
</c:forEach>
                    </tbody>
                </table>
            </div>
        </form>

   

We can the result into same page Because we can pass the redirect page.(same page).

Download Code Here Searching Based on Multiple Text Fields

3 comments:

  1. thanks ashok for giving me the searching form based app..

    ReplyDelete
  2. HI Ashok it's good but it's not recommended to write database code in jsp's in real time scenarios . my suggestion is if we use ajax we can reduce page page loading traffic... :-)

    ReplyDelete
  3. Thanks @Anonymous....Sorry frd's my account somebody hacked and currently am not using this account and blog..i started new blog..i will inform once completed...I don't have permissions to access this account and blog.

    ReplyDelete