Package servlets

Source Code of servlets.UserController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.User;
import models.Feedback;
import models.Auction;

/**
*
* @author buzz
*/
public class UserController extends HttpServlet {


    // <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 {

       String page = "user";

        response.setContentType("text/html;charset=UTF-8");

        String servletPath = request.getServletPath();

        if(servletPath.equals("/users/logout")) {

                     HttpSession session = request.getSession();
                    synchronized(session) {
      session.removeAttribute("logged");
                        response.sendRedirect(request.getContextPath() + "/home");
                    }
            }


       if(servletPath.equals("/users/show")) {
                page += "_show";

            String[] tokens = request.getRequestURI().split("/");
            User user = User.getById(new Integer(tokens[tokens.length - 1]));
            List<Feedback> to = Feedback.getByToId(new Integer(tokens[tokens.length - 1]));
            List<Auction>  auctions = Auction.getByUserId(user.getId());
            List<Auction>  auctions_now = Auction.getByLastUserId(user.getId());
            List<Auction>  auctions_win = Auction.getWin(user.getId());
            request.setAttribute("user", user);
            request.setAttribute("to", to);
            request.setAttribute("auctions", auctions);
            request.setAttribute("auctions_now", auctions_now);
            request.setAttribute("auctions_win", auctions_win);
            }

  if(servletPath.equals("/users/signup")) {
                page += "_signup";
        }
 
        getServletContext().getRequestDispatcher("/templates/header.jsp").include(request, response);
  getServletContext().getRequestDispatcher("/templates/right.jsp").include(request, response);
        getServletContext().getRequestDispatcher("/templates/" + page + ".jsp").include(request, response);
  getServletContext().getRequestDispatcher("/templates/footer.jsp").include(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 {
       
        try {
                String user = request.getParameter("username");
                String pass = request.getParameter("password");
    String mail = request.getParameter("mail");
                List<User> u = new ArrayList<User>();


                String servletPath = request.getServletPath();
                if(servletPath.equals("/users/login")) {
      if(user != null && pass != null) {

        u = User.getByName(user);

        if(u != null) {

          Boolean login = (user.equals(u.get(0).getUsername()) && pass.equals(u.get(0).getPassword()));
                   
          if(login) {
                               
            HttpSession session = request.getSession();
            synchronized(session) {
              session.setAttribute("logged", "true");
              session.setAttribute("user", u.get(0));
            }

          }
        }
      }

      response.sendRedirect(request.getContextPath());
    }
   
    if(servletPath.equals("/users/save")) {
                if(user != null && pass != null && mail != null) {
      User newUser = new User();


      newUser.setUsername(user);
      newUser.setPassword(pass);
                        newUser.setMail(mail);
                        newUser.save();

      response.sendRedirect(request.getContextPath());
    }
    }
        } catch (Exception e) {
       
        }
    }
}
TOP

Related Classes of servlets.UserController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.