Sunday, March 7, 2021

Java Servlet program to fetch book details in a library

//The 'books.java' servlet file.

 package Library;


import java.io.IOException;

import java.io.PrintWriter; 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import java.math.* ; // for BigDecimal and BigInteger support

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

 *

 * @author ashu

 */

public class books 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 {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

    try{

        out.println("<html>");

        out.println("<head><title>Servlet JDBC</title></head>");

        out.println("<body>");

        // connecting to database

        Class.forName("com.mysql.jdbc.Driver");

        Connection con =DriverManager.getConnection ("jdbc:mysql://localhost/dbmsbooks","root","ashutosh");

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM dbbooks");

        // displaying records

        out.println("<center>");

        out.println("<h1>IGNOU RC Varanasi website</h1>");

        out.println("<h2>"+"Following is the details of the books available in library related to DBMS: "+"</h2>");

        out.println("<table border=1>");

        out.println("<tr><th>BookID</th><th>Title</th><th>Authors</th><th>Publication</th><th>Availability</th></tr>");

        while(rs.next()){

            out.println("<tr>");

            out.println("<td>"+rs.getInt(1)+"</td>"+

                        "<td>"+rs.getString(2)+"</td>"+

                        "<td>"+rs.getString(3)+"</td>"+

                        "<td>"+rs.getString(4)+"</td>"+

                        "<td>"+rs.getString(5)+"</td>");

            out.println("</tr>");

        }

        out.println("</center></body></html>");  

  } catch (SQLException e) {

 throw new ServletException("Servlet Could not display records.", e);

  } catch (ClassNotFoundException e) {

  throw new ServletException("JDBC Driver not found.", e);

  } 

  out.close();

  }

        



    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**

     * Handles the HTTP <code>GET</code> method.

     *

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }


    /**

     * Handles the HTTP <code>POST</code> method.

     *

     * @param request servlet request

     * @param response servlet response

     * @throws ServletException if a servlet-specific error occurs

     * @throws IOException if an I/O error occurs

     */

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        processRequest(request, response);

    }


    /**

     * Returns a short description of the servlet.

     *

     * @return a String containing servlet description

     */

    @Override

    public String getServletInfo() {

        return "Short description";

    }// </editor-fold>


}

-----------------------------------------------------------------------------------------
//The corresponding HTML file

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        
    <center>
        <h1>Welcome to the IGNOU RC Varanasi website</h1>
        <h3>Here you can have all information about counselling classes and library books</h3>

<!-- books.java is the servlet file which should get executed/invoked once below link is clicked, -->
        <div>For books availability in the library <a href="books">click here</a></div>
    </center>
    </body>
</html>

-----------------------------------------------------------------------------------------------

//Coresponding XML file

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>books</servlet-name>
        <servlet-class>Library.books</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>books</servlet-name>
        <url-pattern>/books</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows