JSP Search Example code

This tutorial discusses how to create database driven search application.

JSP Search Example code

JSP Search - Search Book Example

     

Search Book Example

In this tutorial we are going to develop a small sample JSP search application that searches the data from database. In this example we will search the book from database.

We are using MySQL database and JSP & Servlet to create the application. We are using Eclipse IDE to deploy and run the JSP search book example on Tomcat sever. You can export the application as war file from Eclipse and run on any other server also.

In this tutorial you will learn how to search book by ISBN Code or Book Category in a jsp and servlet web application .This  tutorial create in eclipse. The tutorial  structure  as :

In this tutorial create two jsp files and one servlet  class given below :

  1. searchbookform.jsp
  2. searchbook.jsp
  3. SearchBook.java

Step 1:

The jsp file "searchbookform.jsp" create in side of  "WebContent" . The code of "searchbookform.jsp" given below :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Search Book</title>

</head>

<body>

<form method="post" action="/searchbook/searchbook">

<table>

<tr>

<td>ISBN Code:</td>

<td><input type="text" name="isbncode" size="10" /></td>

<td>Book Category:</td>

<td><select name="bookcategories">

<option value="-1">-Select Category-</option>

<option value="java">java</option>

<option value="c">c</option>

<option value="c++">c++</option>

<option value="php">php</option>

</select></td>

<td><input type="submit" value="Search Book" /></td>

</tr>

</table>

</form>

</body>

</html>

Step 2:

The "searchbook.jsp"  file in side of  "WebContent" given below :

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Search Book</title>

</head>

<body>

<table align="center">

<%

List booklist=new ArrayList();

booklist=(ArrayList)request.getAttribute("booklist");

if(booklist!=null && booklist.size()>0 ){

%>

<h2 align="center">Book List</h2>

<tr>

<th>ISBN Code</th>

<th>Book Name</th>

<th>Category</th>

</tr>

<%

for(int i=0;i<booklist.size();i++){

List book=(List)booklist.get(i);

%>

<tr>

<td><%=book.get(1) %></td>

<td><%=book.get(2) %></td>

<td><%=book.get(3) %></td>

</tr>

<%

}

}else{

%>

Not  Available  Any Book Details

<%}%>

</table>

</body>

</html>

Step 3:

The code of "SearchBook.java" in side of  "src" folder given below :

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

public class SearchBook extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html"); 
HttpSession session = request.getSession(true);
List booklist=new ArrayList();
Connection con = null;

String url = "jdbc:mysql://localhost:3306/";
String db = "searchbook";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";

String isbncode="";
String bookcategory="";
isbncode=request.getParameter("isbncode");
bookcategory=request.getParameter("bookcategories");
String sqlqueary="SELECT * FROM book WHERE book_name LIKE '%%' ";
if(isbncode!=null && !(isbncode.equals(""))){
sqlqueary+=" and isbn_code='"+isbncode+"'";
}
if(bookcategory!=null && !(bookcategory.equals("-1"))){
sqlqueary+=" and category='"+bookcategory+"'";
}

try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
try{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlqueary);
while (rs.next()) {
List book=new ArrayList();
book.add(rs.getInt(1));
book.add(rs.getInt(2));
book.add(rs.getString(3));
book.add(rs.getString(4));
booklist.add(book);
}
}catch (SQLException s){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
request.setAttribute("booklist",booklist); 
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/searchbook.jsp");
dispatcher.forward(request, response); 
}

}

Step 5:

Again "open web.xml" and modify as :

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

<!DOCTYPE web-app

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>

<servlet-name>SearchBook</servlet-name>

<servlet-class>SearchBook</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>SearchBook</servlet-name>

<url-pattern>/searchbook</url-pattern>

</servlet-mapping>

</web-app>

The "mysql-connector-java-5.0.5.jar"  download and paste in lib folder.

Step 6:

Create database

create database if not exists 'searchbook';

Create table

CREATE TABLE 'book' (
'book_id' bigint(11) NOT NULL AUTO_INCREMENT,
'isbn_code' varchar(100) NOT NULL,
'book_name' varchar(100) DEFAULT NULL,
'category' varchar(100) NOT NULL,
PRIMARY KEY ('book_id')
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

Step 7:

When run Application  "searchbook " display output as :

If  click "Search Book" without insert any ISBN code and select book category display output as:

If enter ISBN code  as :

If match ISBN code display output as :

If not match ISBN code and  book category display message as:

Download Source Code