Advertisement
Guest User

Untitled

a guest
May 28th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 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.RequestDispatcher;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import model.MemberBean;
  16. import model.ObjectPersistance;
  17.  
  18. /**
  19.  *
  20.  * @author mainr30
  21.  */
  22. public class MemberManager extends HttpServlet {
  23.  
  24.     private static final long serialVersionUID = -8978694942580274157L;
  25.     ObjectPersistance<MemberBean> ObjectManager;
  26.  
  27.     @Override
  28.     public void init() throws ServletException {
  29.         String sep = File.separator;
  30.         ObjectManager = new ObjectPersistance<MemberBean>("C:" + sep + "HNC" + sep + "Teststore", "Members");
  31.     }
  32.  
  33.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  34.             throws ServletException, IOException {
  35.         String url = "/index.jsp";
  36.         String submit = request.getParameter("submit");
  37.         if (submit != null) {
  38.             if (submit.equals("newmember")) {
  39.                 url = "/index.jsp";
  40.             }
  41.             else if (submit.equals("add")) {
  42.                 String title = request.getParameter("title");
  43.                 String forename = request.getParameter("forename");
  44.                 String surname = request.getParameter("surname");
  45.                 String postcode = request.getParameter("postcode");
  46.                 String email = request.getParameter("email");
  47.                 int age = Integer.parseInt(request.getParameter("age"));
  48.                 int membertype = Integer.parseInt(request.getParameter("membertype"));
  49.                 int id = getId();
  50.  
  51.  
  52.                 //Create a new instance of the bean
  53.                 MemberBean b = new MemberBean(title, forename, surname, postcode, age, email, membertype, id);
  54.  
  55.                 //Pass the bean over to the ObjectManager to be saved(darryl's class)
  56.                 ObjectManager.saveObject(b, id);
  57.  
  58.                 //Put all the beans into the response
  59.                 request.setAttribute("AllMembers", ObjectManager.getAllObjects());
  60.                 url = "/view.jsp";
  61.             }
  62.             else if (submit.equals("view")) {
  63.                 url = "/view.jsp";
  64.                 //Put all the beans into the response
  65.                 request.setAttribute("AllMembers", ObjectManager.getAllObjects());
  66.             }
  67.  
  68.             else if(submit.equals("delete")){
  69.                 url = "/view.jsp";
  70.                 String username = request.getParameter("username");
  71.                 String password = request.getParameter("password");
  72.                 int id = Integer.parseInt(request.getParameter("memberid"));
  73.  
  74.                 if (username.equals("admin") && password.equals("admin")){
  75.                     ObjectManager.deleteObject(id);
  76.                 }
  77.                
  78.                 request.setAttribute("Members", ObjectManager.getAllObjects());
  79.             }
  80.         }
  81.  
  82.         RequestDispatcher dispatcher =
  83.                 getServletContext().getRequestDispatcher(url);
  84.         dispatcher.forward(request, response);
  85.     }
  86.  
  87.     private int getId() throws IOException {
  88.         Vector<Integer> ids = new Vector<Integer>();
  89.         for (MemberBean b : ObjectManager.getAllObjects()) {
  90.             ids.add(b.getId());
  91.         }
  92.         for (int i = 1; i < Integer.MAX_VALUE; i++) {
  93.             if (!ids.contains(i)) {
  94.                 return (i);
  95.             }
  96.         }
  97.         throw new IndexOutOfBoundsException("Ran out of numbers");
  98.     }
  99.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  100.  
  101.     /**
  102.      * Handles the HTTP <code>GET</code> method.
  103.      * @param request servlet request
  104.      * @param response servlet response
  105.      * @throws ServletException if a servlet-specific error occurs
  106.      * @throws IOException if an I/O error occurs
  107.      */
  108.     @Override
  109.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  110.             throws ServletException,
  111.             IOException {
  112.         processRequest(request, response);
  113.     }
  114.  
  115.     /**
  116.      * Handles the HTTP <code>POST</code> method.
  117.      * @param request servlet request
  118.      * @param response servlet response
  119.      * @throws ServletException if a servlet-specific error occurs
  120.      * @throws IOException if an I/O error occurs
  121.      */
  122.     @Override
  123.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  124.             throws ServletException,
  125.             IOException {
  126.         processRequest(request, response);
  127.     }
  128.  
  129.     /**
  130.      * Returns a short description of the servlet.
  131.      * @return a String containing servlet description
  132.      */
  133.     @Override
  134.     public String getServletInfo() {
  135.         return "Short description";
  136.     }// </editor-fold>
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement