Sessionタイムアウトを発生させるためには、以下の記述をweb.xmlに記述するだけです。
今「
60」となっているところが、その実際の時間です。
ここは分単位なので、「
1」と記述すれば1分になります。
【web.xml】
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
</web-app> Sessionをサーブレット側で切りたい場合は、以下のように
「session.invalidate()」を呼びます。
【Servlet】
//HTTP リクエストの処理
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// セッションを取得します
HttpSession session = request.getSession(false);
// セッションが存在する場合
if (session!=null) {
// セッションを無効にします
session.invalidate();
}
response.setContentType("text/html; charset=Shift_JIS");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>セッション終了</title>");
out.println("<meta http-equiv=\"Pragma\" content=\"no-cache\">");
out.println("<meta http-equiv=\"Cache-Control\" content=\"no-cache\">");
out.println("<meta http-equiv=\"Expires\" content=\"-1\">");
out.println("</head>");
out.println("<body onLoad=\"focus()\">");
out.println("セッションを終了しました");
out.println("</body>");
out.println("</html>");
}