JavaEEでのSessionドメインおよび認証コードの作成

14501 ワード

HttpSeisson:
   (   )          ,                  Session  。

Sessionライフサイクル:
     (      )。
       30  。

Session運転原理:
1.       Servlet ,            SessionId   Session  。
2.   ,SessionId    Cookie ,          。
3.      Servlet ,Cookie  SessionId                Session  。

Sessionの動作原理をテストします.
Sessionオブジェクトを取得し、値を追加します.
public class Demo01 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        HttpSession session = request.getSession();
        session.setAttribute("username", username);
        System.out.println(session.getId());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

セッションドメインのデータを取得するには、次の手順に従います.
public class Demo02 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        response.getWriter().write(username + " " + session.getId());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

ドメインオブジェクトの有効範囲:
Context :             。
Session :          。
Request :         。

Context  > Session  > Request 

Sessionドメインの不動態化と活性化:
  :      ,   Session   Serializable(     )              work         。
  :      , work           Session   。

ショッピングカートの追加と表示機能を簡単に実現する:
1.        :            ,          ,  Session    ,        。
2.        (     ):         、       、           。
3.     Servlet :       。
4.      Servlet :    Servlet ,      ,         ,        Session 。
5.        Servlet :  Session       ,           。

商品情報類:
public class Book implements Serializable {
    /**
     *    
     */
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    public Book() {
        super();
    }
    public Book(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "  :" + id + ",   :" + name;
    }
}

商品を入手するツール類:
public class BookUtil {
    private static HashMap books = new HashMap<>();
    static {
        books.put("1", new Book("1", "    "));
        books.put("2", new Book("2", "    "));
        books.put("3", new Book("3", "    "));
        books.put("4", new Book("4", "    "));
    }

    public static HashMap getBooks() {
        return books;
    }

    public static Book findBookById(String id) {
        return books.get(id);
    }
}

商品展示サーブレット類:
public class ShowAllBooks extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String path = request.getContextPath();
        HashMap books = BookUtil.getBooks();
        for (String key : books.keySet()) {
            Book book = books.get(key);
            out.write(""+ book.getName() +"
"
); } out.write("カートの "); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

カートサーブレットクラスの追加:
public class AddBook extends HttpServlet {
    @SuppressWarnings("unchecked")
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        Book book = BookUtil.findBookById(id);
        HttpSession session = request.getSession();
        ArrayList car = (ArrayList)session.getAttribute("car");
        if (car == null) {
            car = new ArrayList<>();
        }
        car.add(book);
        session.setAttribute("car", car);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

カートサーブレットクラスを表示するには、次の手順に従います.
public class ShowCar extends HttpServlet {
    @SuppressWarnings("unchecked")
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        ArrayList car = (ArrayList)session.getAttribute("car");
        if (car != null) {
            for (Book book : car) {
                out.write(book + "
"
); } } else { out.write(" "); response.setHeader("refresh", "3;url=" + request.getContextPath() + "/showAllBooks"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

人間とコンピュータの図霊テスト(検証コード):
  jar :ValidateCode.jar
public class CodeServlert extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ValidateCode vCode = new ValidateCode(100, 25, 4, 9);
        String code = vCode.getCode();
        request.getSession().setAttribute("wcode", code);
        vCode.write(response.getOutputStream());
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}