コード例 #1
0
  /**
   * Gets user email address, first and last name, puts them into a User object, puts the Object
   * user into session scope, adds a Cookie called emailCookie with the email address as its value,
   * stores the away into a EmailList.txt file that is store in openshift in OPENSHIFT_DATA_DIR
   * folder and locally under WEB-INF.
   *
   * @param request provides parameters for user information
   * @param response add the cookie to the response
   * @return String representing URL to go to next
   */
  private String registerUser(HttpServletRequest request, HttpServletResponse response) {

    // get the user data
    String email = request.getParameter("email");
    String firstName = request.getParameter("firstName");
    String lastName = request.getParameter("lastName");

    // store the data in a User object
    User user = new User();
    user.setEmail(email);
    user.setFirstName(firstName);
    user.setLastName(lastName);

    // write the User object to a file
    // ServletContext sc = getServletContext();
    // String path = sc.getRealPath("/WEB-INF/EmailList.txt");
    String path = this.getActualFile();
    System.out.println("Path: " + path);
    UserIO.add(user, path);

    // store the User object as a session attribute
    HttpSession session = request.getSession();
    session.setAttribute("user", user);

    // add a cookie that stores the user's email to browser
    Cookie c = new Cookie("emailCookie", email);
    c.setMaxAge(60 * 60 * 24 * 365 * 2); // set age to 2 years
    c.setPath("/"); // allow entire app to access it
    response.addCookie(c);

    // create and return a URL for the appropriate Download page
    String productCode = (String) session.getAttribute("productCode");
    String url = "/" + productCode + "_download.jsp";
    return url;
  }
コード例 #2
0
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String url = "/index.html";

    // get current action
    String action = request.getParameter("action");
    if (action == null) {
      action = "join"; // default action
    }

    // perform action and set URL to appropriate page
    if (action.equals("join")) {
      url = "/index.jsp"; // the "join" page
    } else if (action.equals("add")) {
      // get parameters from the request
      String firstName = request.getParameter("firstName");
      String lastName = request.getParameter("lastName");
      String email = request.getParameter("email");

      // store data in User object
      User user = new User();
      user.setEmail(email);
      user.setFirstName(firstName);
      user.setLastName(lastName);

      // validate the parameters
      String message;
      if (UserDB.emailExists(user.getEmail())) {
        message = "This email address already exists.<br>" + "Please enter another email address.";
        url = "/index.jsp";
      } else {
        message = "";
        url = "/thanks.jsp";
        UserDB.insert(user);
      }
      request.setAttribute("user", user);
      request.setAttribute("message", message);
    }
    getServletContext().getRequestDispatcher(url).forward(request, response);
  }