You are on page 1of 5

Online Retrieval of Book from BookStore

Index.jsp

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


<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="postparam" method="post"
action="http://localhost:15239/book_shop/bkshop">
<select name="combo">
<option>Select all authors from the Authors table</option>
<option>Select all publishers from the Publishers table</option>
<option>Select a specific author and list all books for that author</option>
<option>Select a specific publisher and list all books published by that
publisher</option>
</select>
<select name="author">
<option>Select</option>
<option>Pressman</option>
<option>CarlHamacher</option>
</select>
<select name="publisher">
<option>Select</option>
<option>TATA</option>
<option>McGrawHill</option>
</select>
<input type="submit" value="Search" name="disp" /><br>
</form>
</body>
</html>

Bkshop.java

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

public class bkshop extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String op,a,p;
Connection dbcon;
try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
dbcon=DriverManager.getConnection("jdbc:odbc:booksp", "sa",
"123456789");
}
catch(ClassNotFoundException e)
{
System.out.println("Jdbc odbc bridge not found");
return;
}
catch(SQLException e)
{
System.out.println("Sql execption thrown in init");
return;
}

try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet bkshop</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet bkshop at " + request.getContextPath () + "</h1>");
a=request.getParameter("author");
p=request.getParameter("publisher");
op=request.getParameter("combo");
out.print("Author="+a);
out.println("Publisher="+p);
out.println("Operation is "+op);
out.println("</body>");
out.println("</html>");
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Books database</title>");
out.println("</head>");
out.println("<body>");
if(op.equals("Select a specific author and list all books for that author"))
{
try{
Statement stat=dbcon.createStatement();
// Query for database
ResultSet rs=stat.executeQuery("SELECT * from book WHERE author='" +a+
"'order by title asc");
out.println("<h3>Books list for particular Author</h1>");
out.println("<table border=1>");
out.println("<tr><th>Title</th>");
out.println("<th>Year</th>");
out.println("<th>ISBN</th>");
out.println("<th>Author</th>");
out.println("<th>Publisher</th></tr>");
while( rs.next( ) )
{
out.println("<tr><td>"+rs.getString("title")+"</td><td>"+
rs.getString("year")+"</td><td>"+
rs.getString("isbn")+"</td><td>"+
rs.getString("author")+"</td><td>"+
rs.getString("publisher")+"</td></tr>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else if(op.equals("Select a specific publisher and list all books published by that
publisher"))
{
try{
Statement stat1=dbcon.createStatement();
// Query for database
ResultSet rs1=stat1.executeQuery("SELECT * from book WHERE
publisher='" +p+ "' order by title asc");
out.println("<h3>Books list for particular
Publisher</h1>");
out.println("<table border=1>");
out.println("<tr><th>Title</th>");
out.println("<th>Year</th>");
out.println("<th>ISBN</th>");
out.println("<th>Author</th>");
out.println("<th>Publisher</th></tr>");
while( rs1.next( ) )
{
out.println("<tr><td>"+rs1.getString("title")+"</td><td>"+
rs1.getString("year")+"</td><td>"+
rs1.getString("isbn")+"</td><td>"+
rs1.getString("author")+"</td><td>"+
rs1.getString("publisher")+"</td></tr>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else if(op.equals("Select all authors from the Authors table"))
{
try{
Statement stat2=dbcon.createStatement();
// Query for database
ResultSet rs2=stat2.executeQuery("SELECT DISTINCT author from book");
out.println("<h3>Authors list from book database</h1>");
out.println("<table border=1>");
out.println("<tr><th>Authors</th></tr>");
while( rs2.next( ) )
{
out.println("<tr><td>"+rs2.getString("author")+"</td></tr>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
else if(op.equals("Select all publishers from the Publishers table"))
{
try{
Statement stat3=dbcon.createStatement();
// Query for database
ResultSet rs3=stat3.executeQuery("SELECT DISTINCT publisher from
book");
out.println("<h3>Publishers list from book
database</h1>");
out.println("<table border=1>");
out.println("<tr><th>Publishers</th></tr>");
while( rs3.next( ) )
{
out.println("<tr><td>"+rs3.getString("publisher")+"</td></tr>");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
out.println("</table>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}
}

You might also like