ここで探しているのは、リクエスト、セッション、またはアプリケーション データだと思います。
サーブレットでは、オブジェクトを属性としてリクエスト オブジェクト、セッション オブジェクト、またはサーブレット コンテキスト オブジェクトに追加できます。
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
String shared = "shared";
request.setAttribute("sharedId", shared); // add to request
request.getSession().setAttribute("sharedId", shared); // add to session
this.getServletConfig().getServletContext().setAttribute("sharedId", shared); // add to application context
request.getRequestDispatcher("/URLofOtherServlet").forward(request, response);
}
これをリクエスト オブジェクトに入れると、リクエストが終了するまで、転送先のサーブレットで使用できるようになります。
request.getAttribute("sharedId");
セッションに配置すると、今後すべてのサーブレットで使用できますが、値はユーザーに関連付けられます。
request.getSession().getAttribute("sharedId");
ユーザーの非アクティブに基づいてセッションが期限切れになるまで。
あなたによってリセットされます:
request.getSession().invalidate();
または、1 つのサーブレットがそれをスコープから削除します。
request.getSession().removeAttribute("sharedId");
これをサーブレット コンテキストに配置すると、アプリケーションの実行中に使用できるようになります。
this.getServletConfig().getServletContext().getAttribute("sharedId");
削除するまで:
this.getServletConfig().getServletContext().removeAttribute("sharedId");