keymasterviriya1150

Untitled

Aug 21st, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. public class CreateBookServlet extends HttpServlet {
  16. public static ArrayList<Book> books;
  17. public void init() throws ServletException{
  18. books = new ArrayList<Book>();
  19. }
  20. @Override
  21. protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  22. try (PrintWriter out = response.getWriter()) {
  23. out.println("<!DOCTYPE html>");
  24. out.println("<html>");
  25. out.println("<head>");
  26. out.println("<title>Create Book</title>");
  27. out.println("</head>");
  28. out.println("<body>");
  29. out.println("<h1>Servlet BookServlet</h1>");
  30. out.println("<form action=\"\" method=\"POST\">");
  31. out.println("<h3>Add New Book</h3>");
  32. out.println("<p>");
  33. out.println("ID : <input type=\"text\" name=\"id\">");
  34. out.println("</p>");
  35. out.println("<p>");
  36. out.println("TITLE : <input type=\"text\" name=\"title\">");
  37. out.println("</p>");
  38. out.println("<p>");
  39. out.println("PRICE : <input type=\"text\" name=\"price\">");
  40. out.println("</p>");
  41. out.println("<input type=\"submit\" value=\"ADD BOOK\">");
  42. out.println("</form>");
  43. out.println("</body>");
  44. out.println("</html>");
  45. }
  46. }
  47.  
  48. @Override
  49. protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  50. int id = Integer.parseInt(request.getParameter("id"));
  51. String title = request.getParameter("title").toString();
  52. float price = Float.parseFloat(request.getParameter("price"));
  53. if(books.add(new Book(id,title,price))){
  54. try (PrintWriter out = response.getWriter()) {
  55. out.println("<!DOCTYPE html>");
  56. out.println("<html>");
  57. out.println("<head>");
  58. out.println("<title>Create Book</title>");
  59. out.println("</head>");
  60. out.println("<body>");
  61. out.println("<h1>Servlet BookServlet</h1>");
  62. out.println("<p>");
  63. out.println("Add Book Complete");
  64. out.println("</p>");
  65. out.println("<a href=\"./\">GO TO MENU</a>");
  66. out.println("</body>");
  67. out.println("</html>");
  68. }
  69. }else{
  70. try (PrintWriter out = response.getWriter()) {
  71. out.println("<!DOCTYPE html>");
  72. out.println("<html>");
  73. out.println("<head>");
  74. out.println("<title>Create Book</title>");
  75. out.println("</head>");
  76. out.println("<body>");
  77. out.println("<h1>Servlet BookServlet</h1>");
  78. out.println("<p>");
  79. out.println("Add Book Error");
  80. out.println("</p>");
  81. out.println("<a href=\"./\">GO TO MENU</a>");
  82. out.println("</body>");
  83. out.println("</html>");
  84. }
  85. }
  86.  
  87. }
  88. @Override
  89. public String getServletInfo() {
  90. return "Short description";
  91. }// </editor-fold>
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment