Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package controller;
- import java.io.File;
- import java.io.IOException;
- import java.util.Vector;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import model.MemberBean;
- import model.ObjectPersistance;
- /**
- *
- * @author mainr30
- */
- public class MemberManager extends HttpServlet {
- private static final long serialVersionUID = -8978694942580274157L;
- ObjectPersistance<MemberBean> ObjectManager;
- @Override
- public void init() throws ServletException {
- String sep = File.separator;
- ObjectManager = new ObjectPersistance<MemberBean>("C:" + sep + "HNC" + sep + "Teststore", "Members");
- }
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String url = "/index.jsp";
- String submit = request.getParameter("submit");
- if (submit != null) {
- if (submit.equals("newmember")) {
- url = "/index.jsp";
- }
- else if (submit.equals("add")) {
- String title = request.getParameter("title");
- String forename = request.getParameter("forename");
- String surname = request.getParameter("surname");
- String postcode = request.getParameter("postcode");
- String email = request.getParameter("email");
- int age = Integer.parseInt(request.getParameter("age"));
- int membertype = Integer.parseInt(request.getParameter("membertype"));
- int id = getId();
- //Create a new instance of the bean
- MemberBean b = new MemberBean(title, forename, surname, postcode, age, email, membertype, id);
- //Pass the bean over to the ObjectManager to be saved(darryl's class)
- ObjectManager.saveObject(b, id);
- //Put all the beans into the response
- request.setAttribute("AllMembers", ObjectManager.getAllObjects());
- url = "/view.jsp";
- }
- else if (submit.equals("view")) {
- url = "/view.jsp";
- //Put all the beans into the response
- request.setAttribute("AllMembers", ObjectManager.getAllObjects());
- }
- else if(submit.equals("delete")){
- url = "/view.jsp";
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- int id = Integer.parseInt(request.getParameter("memberid"));
- if (username.equals("admin") && password.equals("admin")){
- ObjectManager.deleteObject(id);
- }
- request.setAttribute("Members", ObjectManager.getAllObjects());
- }
- }
- RequestDispatcher dispatcher =
- getServletContext().getRequestDispatcher(url);
- dispatcher.forward(request, response);
- }
- private int getId() throws IOException {
- Vector<Integer> ids = new Vector<Integer>();
- for (MemberBean b : ObjectManager.getAllObjects()) {
- ids.add(b.getId());
- }
- for (int i = 1; i < Integer.MAX_VALUE; i++) {
- if (!ids.contains(i)) {
- return (i);
- }
- }
- throw new IndexOutOfBoundsException("Ran out of numbers");
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException,
- IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException,
- IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement