Saturday, March 13, 2021

Simple Servlet program to query books details and update a book's availability status by taking book ID as input

 //The HTML index page

<!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>

        

        <!-- modifyBooks.java is the Servlet file which should get executed/invoked once below link is clicked, -->

        <br><br>

        <div>For modifying a book's availability status in the library

            

            <form method="POST" action="modifyBooks">

                <br><br>

            enter book id <input type = "text" id ="bookId" name ="bookId"> 

            <br><br>

            enter new availability status (Yes/No) <input type = "text" id ="status"  name ="status">

            <br><br>

                <button type="submit"> Update </button>

            </form>

        </div>

        

        

    </center>

    </body>

</html>

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

//The 'books.java' servlet

/*
 * 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.
 */
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.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 'modifyBooks.java' servlet

/*
 * 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.
 */
package Library;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ashu
 */
@WebServlet(name = "modifyBooks", urlPatterns = {"/modifyBooks"})

public class modifyBooks extends HttpServlet { 

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException 

    {   
        response.setContentType("text/html;charset=UTF-8");
        
    try(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();
            
            String bookId = request.getParameter("bookId");
            String status = request.getParameter("status");
            //out.println(bookId);
            //out.println(status);
            
            stmt.executeUpdate("UPDATE dbbooks SET available = '" + status + " 'WHERE bookID = "+bookId+";");
            
            out.println("Book status updated successfuly!");
            out.println("</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);
        }
  }    }
------------------------------------------------------------------------------------------------------------
Output:
Home page:



Books details

BookID 1 status change from 'yes' to 'no' request made


Change done successfuly


Verify the change by seeing books details once again






2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. very informative article you have published on this website. My website is www.jankarihub.com

    ReplyDelete

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...