サーブレット用のセッション生成プログラムコード

他のサイトからもらったソースコード

見事に動作しました。

 

chrome

①クッキー名を保護されていない(または保護されている)通信の部分から確認できます。JSESSIONになっているはず。

 

②その中のコンテンツの部分に「

HttpSessionCloneId

」を確認できます。

 

いったんそれくらいでよいのかな??

 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub HttpSession session = request.getSession(true);
int cnt; String msg, id; // セッションIDの取得 id = session.getId();
try { // セッションに格納された値の取得 String cntStr = (String)session.getAttribute("counter"); cnt = Integer.parseInt(cntStr); msg = cnt + " 回目のお越しです!"; } catch (NumberFormatException e) { cnt = 1; msg = "はじめまして!"; } cnt++; // セッションに値を格納 session.setAttribute("counter", Integer.toString(cnt));
// 応答文字コードのセット response.setContentType("text/html; charset=Shift_JIS"); // 出力ストリームの取得 PrintWriter out = response.getWriter(); out.println("<html><head>\n" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=Shift_JIS\" />\n" + "<title>セッション・デモ</title>\n" + "</head><body>\n" + "<h1>セッション・デモ</h1>\n" + "<p>" + msg + "</p>\n" + "<p>サーブレットコンテナが割り振ったセッションID:" + id + "</p>\n" + "</body></html>\n");
}