LoginSignup
5
6

More than 5 years have passed since last update.

【JSP】セッションを使う

Last updated at Posted at 2016-08-14

【目的】

JSPにてセッションを使用してデータのやりとりを行う

【環境】

eclipse 4.4.2 / Java 1.8
JSP / Servlet

【構成】

-

【ポイント】

・JSPの暗黙オブジェクトsessionを使う
・セッションでやりとりを行うデータはsetAttribute/getAttributeで設定/取得する

セッションを用いるとサーバー(アプリケーション)内で情報を受け渡しできるようになる。

public void setAttribute(String key, Object value); // キー値に値をセットする
public Object getAttribute(String key); // キーに対応する値を取得する

【例】

・A.jsp →B.jsp に”e-mail"の値を渡して表示する

- web-application
| ・・・
|- A.jsp
|- B.jsp

A.jsp
...
<%
    session.setAttribute("e-mail", "hoge@hoge.jp");
    application.getRequestDispatcher("/B.jsp").forward(request, response);
%>
B.jsp
...
<%
    String eMail = (String) session.getAttribute("e-mail");
    out.println("e-mailアドレス : " + eMail);
%>

【参考】

クッキーとセッション

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6