Example usage for javax.servlet.http HttpSession setAttribute

List of usage examples for javax.servlet.http HttpSession setAttribute

Introduction

In this page you can find the example usage for javax.servlet.http HttpSession setAttribute.

Prototype

public void setAttribute(String name, Object value);

Source Link

Document

Binds an object to this session, using the name specified.

Usage

From source file:miage.ecom.web.controller.LoginController.java

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String formSetup(Model model, HttpSession session) {
    CartBean cart;//  w  ww. java 2  s . c o m
    if (session.getAttribute("cart") == null) {
        cart = new CartBean();
    } else {
        cart = (CartBean) session.getAttribute("cart");
        session.setAttribute("cart", cart);
    }

    model.addAttribute("cartTotalValue", ecomBeanFrontLocal.getTotalValue(cart));
    model.addAttribute("nbProducts", ecomBeanFrontLocal.getCartContents(cart).size());
    return "login";
}

From source file:miage.ecom.web.controller.LoginController.java

@RequestMapping(value = "/signup", method = RequestMethod.GET)
public String signupForm(Model model, HttpSession session) {
    CartBean cart;/* w w  w  .  j  av a2  s .co m*/
    if (session.getAttribute("cart") == null) {
        cart = new CartBean();
    } else {
        cart = (CartBean) session.getAttribute("cart");
        session.setAttribute("cart", cart);
    }

    model.addAttribute("cartTotalValue", ecomBeanFrontLocal.getTotalValue(cart));
    model.addAttribute("nbProducts", ecomBeanFrontLocal.getCartContents(cart).size());
    return "signup";
}

From source file:Counter.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 1;/*from   w w  w.  ja  v  a  2 s  .c  o m*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Session Counter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Your session ID is <b>" + session.getId());
    out.println("</b> and you have hit this page <b>" + count + "</b> time(s) during this browser session");

    out.println("<form method=GET action=\"" + req.getRequestURI() + "\">");
    out.println("<input type=submit " + "value=\"Hit page again\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:miage.ecom.web.controller.SearchController.java

@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(Model model, HttpSession session, @RequestParam("keywords") String keywords) {

    CartBean cart;// w w w  .  j av  a 2 s.c  o m
    if (session.getAttribute("cart") == null) {
        cart = new CartBean();
    } else {
        cart = (CartBean) session.getAttribute("cart");
        session.setAttribute("cart", cart);
    }

    List<Purchase> cartContents = ecomBeanFrontLocal.getCartContents(cart);
    model.addAttribute("cartTotalValue", ecomBeanFrontLocal.getTotalValue(cart));
    model.addAttribute("nbProducts", ecomBeanFrontLocal.getCartContents(cart).size());
    List<Category> categories = categoryFacade.findAll();
    model.addAttribute("categories", categories);

    List<Product> products = productFacadeLocal.findProductsByNameLike("%" + keywords + "%");
    products.addAll(productFacadeLocal.findProductsByDescriptionLike("%" + keywords + "%"));

    model.addAttribute("products", products);
    model.addAttribute("keywords", keywords);

    return "search";
}

From source file:org.eg.sc.web.ShopCartAction.java

public String queryAllProduct() {
    List<Product> products = shoppingCartServiceImpl.queryAllProduct();
    HttpServletRequest request = ServletActionContext.getRequest();
    request.setAttribute("products", products);
    HttpSession session = ServletActionContext.getRequest().getSession();
    OrderCar car = (OrderCar) session.getAttribute("Car");
    if (car == null) {
        car = new OrderCar();
        session.setAttribute("Car", car);
    }//from  ww w  .j  a v  a2s  . co  m

    return SUCCESS;
}

From source file:com.jspxcms.core.setup.SetupServlet.java

private void database(HttpServletRequest request, HttpServletResponse response) throws IOException,
        InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
    long begin = System.currentTimeMillis();
    String host = request.getParameter("host");
    String port = request.getParameter("port");
    String name = request.getParameter("name");
    String user = request.getParameter("user");
    String password = request.getParameter("password");
    boolean isCreateDatabase = "true".equals(request.getParameter("isCreateDatabase"));

    String adminUsername = request.getParameter("adminUsername");
    String adminPassword = request.getParameter("adminPassword");

    String version = request.getParameter("version");
    int serverPort = request.getServerPort();
    String webPort = serverPort == 80 ? null : String.valueOf(serverPort);
    String contextPath = request.getContextPath();

    if (isCreateDatabase) {
        createDatabase(host, port, name, user, password);
    } else {//w  w w  . ja v  a2  s  . c  om
        alterDatabaseCharset(host, port, name, user, password);
    }
    createDatabaseTable(host, port, name, user, password, adminUsername, adminPassword, version, webPort,
            contextPath);
    copyFile(request, host, port, name, user, password);

    HttpSession session = request.getSession();
    session.setAttribute("step", 2);
    long processed = System.currentTimeMillis() - begin;
    System.out.println("processed in: " + processed);
}

From source file:CounterRewrite.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 1;/*w w  w.j av a 2s  .  c o m*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Session Counter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Your session ID is <b>" + session.getId());
    out.println("</b> and you have hit this page <b>" + count + "</b> time(s) during this browser session");

    String url = req.getRequestURI();
    out.println("<form method=GET action=\"" + resp.encodeURL(url) + "\">");
    out.println("<input type=submit " + "value=\"Hit page again\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:puma.application.webapp.users.AuthenticationController.java

@RequestMapping(value = "/user/login-callback")
public String loginCallback(ModelMap model, @RequestParam MultiValueMap<String, String> params,
        HttpSession session) {
    // set the application attributes
    if (!params.containsKey("UserId"))
        throw new RuntimeException("No user id was given");
    session.setAttribute("user_id", params.get("UserId").get(0));
    if (!params.containsKey("Name"))
        throw new RuntimeException("No user name given");
    session.setAttribute("user_name", params.get("Name").get(0));
    if (params.containsKey("Email"))
        session.setAttribute("user_email", params.get("Email").get(0));
    else//www  .  j  a  v  a 2 s .c om
        session.setAttribute("user_email", params.get("UserId").get(0));

    Subject subject = new Subject(params.get("UserId").get(0));
    if (!params.containsKey("PrimaryTenant"))
        throw new RuntimeException("No tenant given for user " + session.getAttribute("Name"));
    session.setAttribute("user_tenant", params.get("PrimaryTenant").get(0));
    if (params.containsKey("Tenant") && params.get("Tenant").size() > 0) {
        SubjectAttributeValue tenantAttr = new SubjectAttributeValue("tenant");
        for (String t : params.get("Tenant"))
            tenantAttr.addValue(t);
        subject.addAttributeValue(tenantAttr);
    }
    if (params.containsKey("Token"))
        session.setAttribute("user_token", params.get("Token").get(0));

    // store the authorization subject
    if (params.containsKey("Role") && params.get("Role").size() > 0) {
        SubjectAttributeValue rolesAttr = new SubjectAttributeValue("roles");
        for (String r : params.get("Role")) {
            rolesAttr.addValue(r);
        }
        subject.addAttributeValue(rolesAttr);
    }
    if (params.containsKey("Manages") && params.get("Manages").size() > 0) {
        SubjectAttributeValue assignedAttr = new SubjectAttributeValue("assigned");
        for (String n : params.get("Manages"))
            assignedAttr.addValue(n);
        subject.addAttributeValue(assignedAttr);
    }
    subject.addAttributeValue(new SubjectAttributeValue("email", (String) session.getAttribute("user_email")));
    session.setAttribute("subject", subject);

    MessageManager.getInstance().addMessage(session, "success",
            "Welcome back, " + (String) session.getAttribute("user_name"));
    return "redirect:/docs";
}

From source file:net.bafeimao.umbrella.web.controller.UserController.java

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(User user, HttpSession session, @RequestParam(required = false) String returl) {
    int result = userService.login(user.getName(), user.getPassword());
    if (result == 0) {
        session.setAttribute("user", user);

        if (returl != null) {
            return "redirect:" + returl;
        } else {/*from   w w w .  j  a v  a2 s. c o m*/
            return "redirect:/profile";
        }
    } else if (result == 1) {
        LOGGER.info("??");
    } else if (result == 2) {
        LOGGER.info("???");
    }

    return "/login?error=";
}

From source file:edu.chalmers.dat076.moviefinder.controller.LoginController.java

@RequestMapping(value = "/login", method = RequestMethod.POST)
public @ResponseBody User login(HttpSession s, @RequestBody UserLoginModel login) {
    User u;/*from  www  .j  av a  2  s  . c o m*/

    if (login.getUsername().equals("admin")) {
        u = new User(login.getUsername(), UserRole.ADMIN);
    } else {
        u = new User(login.getUsername(), UserRole.VIEWER);
    }

    s.setAttribute("user", u);
    return u;
}