Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *****************************************************************************
- File: ServletEditDeleteBook.java
- Desc: this is a servlet updates and deletes books
- This assignment represents my work in accordance with Seneca academic policy.
- Signed: Mikesh Mistry and Justin Goncalves
- *****************************************************************************
- */
- package controller;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import model.*;
- public class ServletEditDeleteBook extends HttpServlet {
- private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
- public void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException,
- IOException {
- //Retreive book values from the Editbook.jsp
- PrintWriter out = response.getWriter();
- String oper = request.getParameter("operation");
- String bookid =request.getParameter("id");
- String len = request.getParameter("displaylendable");
- String title= request.getParameter("displaytitle");
- String author = request.getParameter("displayauthor");
- String sat = request.getParameter("statid");
- String bookcost =request.getParameter("displayprice");
- String bookdesc = request.getParameter("displaydesc");
- if(oper!=null){//check to see if a operation is selected and all values are present and not null
- DBHelperClass db = new DBHelperClass();
- //Creates a bookRowBean object and sets the values that have been passed to the servlet from the page
- BookRowBean updatebook = GeneralHelperClass.SetBookObject(bookid,len,title,author,sat,bookcost,bookdesc);
- //Secondary validation checker (done after javascript as a second security measure)
- // When edit book button is pressed, send DB helper an updated book to update in the database
- if(oper.equals("Edit Book") && GeneralHelperClass.check_values(bookid,len,title,author,sat,bookcost,bookdesc)){
- //this updates the book and calls the DBhelper class and updates the book in the db its passed in the book oject
- if(db.updateBook(updatebook)){
- rdirect(response,request,"NONE","/ViewAllBooks.jsp");
- }else{
- rdirect(response,request,"Update Unsucessfull","/Welcome.jsp");
- }
- // When delete book button is pressed, tell DB helper to delete book
- }else if(oper.equals("Delete Book")){ // for the delete book operation
- if(db.DeleteBook(Integer.parseInt(bookid))){
- rdirect(response,request,"NONE","/ViewAllBooks.jsp");
- }else{
- rdirect(response,request,"Delete Unsucess","/Welcome.jsp");
- }
- }
- }
- }
- //redirect page using the requestdispatcher
- private void rdirect(HttpServletResponse response,
- HttpServletRequest request,String value,
- String target)throws ServletException, IOException {
- RequestDispatcher rd;
- rd = getServletContext().getRequestDispatcher(target);
- rd.forward(request, response);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement