CS

Java

Servlets

Http request:

Session in Web Applications

Cookies

Example of Session in applications

Login code

 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
        res.setContentType("text/html");
        String login = req.getParameter("login");
        String password = req.getParameter("password");
        String logged = check(connection, login, password);
        System.out.println("check Login logged: " + logged);
        System.out.println("check Login login, password: " + login + " " + password);
        if (logged != null) {
            HttpSession session = req.getSession(true);
            session.setAttribute("login", logged);
            res.sendRedirect("ProductList");
        } else {
        ...

Function code

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
        res.setContentType("text/html");
        PrintWriter toClient = res.getWriter();
        String categoryId = req.getParameter("id");
        HttpSession session = req.getSession(false);
        String login = null;
        if (session != null) {
            login = (String)session.getAttribute("login");
            System.out.println("ProductList logged");
            System.out.println("ProductList login: " + login);
        }
        toClient.println(Utils.header("Products", login));
        
        ...

Logout code

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
        res.setContentType("text/html");
        HttpSession session = req.getSession(false);
        if (session != null) {
            session.invalidate();
            res.sendRedirect("CheckLogin");
        }
    }

In folder northbrickSession

Session in Servlets

Server Sent Event

Using a pendrive to execute the programas

See the instructions


Edit v8.1