Advertisement
Guest User

Untitled

a guest
May 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.07 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package controller;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.Vector;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.RequestDispatcher;
  15. import model.MemberBean;
  16. import model.ObjectPersistance;
  17.  
  18. /**
  19.  *
  20.  * @author mainr30
  21.  */
  22. public class WaterSportServlet extends HttpServlet {
  23.  
  24.     private static final long serialVersionUID = 1L;
  25.     //ObjectPersistance is the name of Darryl's class, ObjectManager is the name of the instance.
  26.     ObjectPersistance<MemberBean> ObjectManager;
  27.  
  28.     @Override
  29.     public void init() throws ServletException {
  30.         String sep = File.separator;
  31.         ObjectManager = new ObjectPersistance<MemberBean>("C:" + sep + "HNC" + sep + "TestStore", "Membership");
  32.     }
  33.  
  34.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  35.             throws ServletException, IOException {
  36.  
  37.         //Initialize the variables used for navigation
  38.         String page = request.getParameter("page");
  39.         String url = "/add.jsp";
  40.         String action = request.getParameter("action");
  41.  
  42.         //does page have a value?
  43.         if (page == null) {
  44.             response.sendRedirect("WaterSportServlet?page=add&action=");
  45.         } //If page has a value, lets see what that value is
  46.         else {
  47.             //If it equals add run the code for the add page
  48.             if (page.equals("add")) {
  49.                 if (action.equals("addMember")) {
  50.                     url = "/index.jsp";
  51.                     String title = request.getParameter("title");
  52.                     String forename = request.getParameter("forename");
  53.                     String surname = request.getParameter("surname");
  54.                     String postcode = request.getParameter("postcode");
  55.                     String email = request.getParameter("email");
  56.                     int age = Integer.parseInt(request.getParameter("age"));
  57.                     int membertype = Integer.parseInt(request.getParameter("membertype"));
  58.                     int id = getId();
  59.  
  60.                     //Create a new instance of the bean
  61.                     MemberBean b = new MemberBean(title, forename, surname, postcode, age, email, membertype, id);
  62.  
  63.                     //Pass the bean over to the ObjectManager (darryl's class)
  64.                     ObjectManager.saveObject(b, id);
  65.                     request.setAttribute("allMembers", ObjectManager.getAllObjects());
  66.                     url = "/view.jsp";
  67.                 } else {
  68.                     url = "/index.jsp";
  69.                 }
  70.             } //if it equals view then goto view
  71.             else if (page.equals("view")) {
  72.                 url = "/view.jsp";
  73.                 Vector allMembers = ObjectManager.getAllObjects();
  74.                 request.setAttribute("allMembers", allMembers);
  75.  
  76.                 if (action.equals("Delete")) {
  77.                     int id = Integer.parseInt(request.getParameter("memberid"));
  78.                     String username = request.getParameter("username");
  79.                     String password = request.getParameter("password");
  80.  
  81.                     if (username.equals("admin") && password.equals("password")) {
  82.                         ObjectManager.deleteObject(id);
  83.                         url = "/view.jsp";
  84.                     }
  85.                 }
  86.  
  87.                 else if (action.equals("Edit")) {
  88.  
  89.                     int id = Integer.parseInt(request.getParameter("memberid"));
  90.  
  91.                     Vector<MemberBean> memberList = ObjectManager.getAllObjects();
  92.  
  93.                     for (MemberBean b : memberList) {
  94.                         if (id == b.getId()) {
  95.                             request.setAttribute("member", b);
  96.                         }
  97.                     }
  98.                 }
  99.             } //If it doesnt match any of the above, go back to the add page.
  100.             else {
  101.                 url = "/index.jsp";
  102.             }
  103.  
  104.             //Make the actual request
  105.             RequestDispatcher dispatcher =
  106.                     getServletContext().getRequestDispatcher(url);
  107.             dispatcher.forward(request, response);
  108.         }
  109.     }
  110.  
  111.     //Generate an ID
  112.     private int getId() throws IOException {
  113.         Vector<Integer> ids = new Vector<Integer>();
  114.         for (MemberBean b : ObjectManager.getAllObjects()) {
  115.             ids.add(b.getId());
  116.         }
  117.         for (int i = 1; i < Integer.MAX_VALUE; i++) {
  118.             if (!ids.contains(i)) {
  119.                 return (i);
  120.             }
  121.         }
  122.         throw new IndexOutOfBoundsException("Ran out of numbers");
  123.     }
  124.  
  125.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  126.     /**
  127.      * Handles the HTTP <code>GET</code> method.
  128.      * @param request servlet request
  129.      * @param response servlet response
  130.      * @throws ServletException if a servlet-specific error occurs
  131.      * @throws IOException if an I/O error occurs
  132.      */
  133.     @Override
  134.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  135.             throws ServletException, IOException {
  136.         processRequest(request, response);
  137.     }
  138.  
  139.     /**
  140.      * Handles the HTTP <code>POST</code> method.
  141.      * @param request servlet request
  142.      * @param response servlet response
  143.      * @throws ServletException if a servlet-specific error occurs
  144.      * @throws IOException if an I/O error occurs
  145.      */
  146.     @Override
  147.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  148.             throws ServletException, IOException {
  149.         processRequest(request, response);
  150.     }
  151.  
  152.     /**
  153.      * Returns a short description of the servlet.
  154.      * @return a String containing servlet description
  155.      */
  156.     @Override
  157.     public String getServletInfo() {
  158.         return "Short description";
  159.     }// </editor-fold>
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement