Advertisement
Guest User

Untitled

a guest
May 19th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. /*
  2. *****************************************************************************
  3. File: ServletEditDeleteBook.java
  4. Desc: this is a servlet updates and deletes books
  5.  
  6. This assignment represents my work in accordance with Seneca academic policy.
  7. Signed: Mikesh Mistry and Justin Goncalves
  8. *****************************************************************************
  9. */
  10.  
  11. package controller;
  12.  
  13. import java.io.IOException;
  14. import java.io.PrintWriter;
  15.  
  16. import javax.servlet.*;
  17. import javax.servlet.http.*;
  18. import model.*;
  19.  
  20. public class ServletEditDeleteBook extends HttpServlet {
  21. private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
  22.  
  23.  
  24.  
  25. public void doPost(HttpServletRequest request,
  26. HttpServletResponse response) throws ServletException,
  27. IOException {
  28. //Retreive book values from the Editbook.jsp
  29. PrintWriter out = response.getWriter();
  30. String oper = request.getParameter("operation");
  31. String bookid =request.getParameter("id");
  32. String len = request.getParameter("displaylendable");
  33. String title= request.getParameter("displaytitle");
  34. String author = request.getParameter("displayauthor");
  35. String sat = request.getParameter("statid");
  36. String bookcost =request.getParameter("displayprice");
  37. String bookdesc = request.getParameter("displaydesc");
  38.  
  39.  
  40. if(oper!=null){//check to see if a operation is selected and all values are present and not null
  41. DBHelperClass db = new DBHelperClass();
  42. //Creates a bookRowBean object and sets the values that have been passed to the servlet from the page
  43. BookRowBean updatebook = GeneralHelperClass.SetBookObject(bookid,len,title,author,sat,bookcost,bookdesc);
  44.  
  45.  
  46. //Secondary validation checker (done after javascript as a second security measure)
  47. // When edit book button is pressed, send DB helper an updated book to update in the database
  48. if(oper.equals("Edit Book") && GeneralHelperClass.check_values(bookid,len,title,author,sat,bookcost,bookdesc)){
  49.  
  50. //this updates the book and calls the DBhelper class and updates the book in the db its passed in the book oject
  51. if(db.updateBook(updatebook)){
  52. rdirect(response,request,"NONE","/ViewAllBooks.jsp");
  53. }else{
  54. rdirect(response,request,"Update Unsucessfull","/Welcome.jsp");
  55. }
  56.  
  57. // When delete book button is pressed, tell DB helper to delete book
  58. }else if(oper.equals("Delete Book")){ // for the delete book operation
  59. if(db.DeleteBook(Integer.parseInt(bookid))){
  60. rdirect(response,request,"NONE","/ViewAllBooks.jsp");
  61. }else{
  62. rdirect(response,request,"Delete Unsucess","/Welcome.jsp");
  63. }
  64. }
  65.  
  66.  
  67.  
  68.  
  69. }
  70. }
  71.  
  72.  
  73.  
  74. //redirect page using the requestdispatcher
  75. private void rdirect(HttpServletResponse response,
  76. HttpServletRequest request,String value,
  77. String target)throws ServletException, IOException {
  78. RequestDispatcher rd;
  79. rd = getServletContext().getRequestDispatcher(target);
  80. rd.forward(request, response);
  81. return;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement