Created
December 21, 2015 17:30
-
-
Save Cartman0/5b495295c83f4117021f to your computer and use it in GitHub Desktop.
Java Servlet Scopeのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" %> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Java Scope Sample</title> | |
</head> | |
<body> | |
<h1>インプットフォーム</h1> | |
<form action="./ScopeSetServlet" method="post"> | |
<p>名前を入力してください:<input type="text" name="name_request"></p> | |
<input type="submit" value="送信"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=UTF-8" %> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Java Scope Sample</title> | |
</head> | |
<body> | |
<h1>Java Scope Sample</h1> | |
<h2>アクセス回数</h2> | |
<%-- | |
session, request, applicationは暗黙的オブジェクト | |
今回のrequestはforwardしているので保持されている。 | |
--%> | |
<p><%=session.getAttribute("name_session")%>さん のアクセス回数</p> | |
<p>リクエスト:<%=request.getAttribute("access_request")%>回</p> | |
<p>セッション:<%=session.getAttribute("access_session")%>回</p> | |
<p>アプリケーション:<%=application.getAttribute("access_application")%>回</p> | |
<form> | |
<input type="button" onclick="location.href = './scope.jsp'" value="戻る"> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package scope; | |
import java.io.IOException; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
/** | |
* Java入門 スコープセットクラス.<br> | |
*/ | |
@WebServlet("/ScopeSetServlet") | |
public class ScopeSetServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
public ScopeSetServlet() { | |
super(); | |
} | |
/** | |
* POSTメソッドでリクエストされた場合の処理. | |
* | |
* @param request | |
* @param response | |
* @throws javax.servlet.ServletException | |
* @throws java.io.IOException | |
*/ | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
// 各スコープからアクセス回数を取得 | |
/* | |
getAttribute自体はObject型 | |
初めてのアクセスだとsetAttrしてないので、 | |
すべてnullになる。 | |
*/ | |
// リクエストスコープからアクセス回数を取得し、String型に変換 | |
String request_attr = (String)request.getAttribute("access_request"); | |
System.out.println("request_attr: " + request_attr); | |
// セッションスコープを取得 | |
// セッション情報の文字化け対策 | |
request.setCharacterEncoding("UTF-8"); | |
HttpSession http_session = request.getSession(); | |
// セッションスコープからアクセス回数を取得し、String型に変換 | |
String session_attr = (String) http_session.getAttribute("access_session"); | |
System.out.println("session_attr: " + session_attr); | |
// アプリケーションスコープを取得 | |
ServletContext serv_context = request.getServletContext(); | |
// アプリケーションスコープ(サーブレットコンテキスト)からアクセス回数を取得し、String型に変換 | |
String aApplication = (String) serv_context.getAttribute("access_application"); | |
System.out.println(aApplication); | |
/* | |
それぞれをIntに型変換 | |
*/ | |
int accessRequest = 1; // リクエストスコープ用アクセス回数カウンタ | |
int accessSession = 1; // セッションスコープ用アクセス回数カウンタ | |
int accessApplication = 1; // アプリケーションスコープ用アクセス回数カウンタ | |
// 各スコープのアクセス回数をint型に変換 | |
if (request_attr != null) { | |
// リクエストスコープからアクセス回数が取得できている場合はint型に変換 | |
accessRequest = Integer.parseInt(request_attr); | |
// アクセス回数を更新 | |
accessRequest++; | |
} | |
if (session_attr != null) { | |
// セッションスコープからアクセス回数が取得できている場合はint型に変換 | |
accessSession = Integer.parseInt(session_attr); | |
// アクセス回数を更新 | |
accessSession++; | |
} else { | |
// アクセス回数が取得できなかった場合ははじめてのリクエストをみなす | |
// セッションの有効時間を1分(60秒)に設定 | |
http_session.setMaxInactiveInterval(60); | |
} | |
if (aApplication != null) { | |
// アプリケーションスコープからアクセス回数が取得できている場合はint型に変換 | |
accessApplication = Integer.parseInt((String) aApplication); | |
// アクセス回数を更新 | |
accessApplication++; | |
} | |
// 画面移動(フォワード)の準備 | |
// セッションにユーザ名を格納 | |
String input_name_request = request.getParameter("name_request"); | |
http_session.setAttribute("name_session", input_name_request); | |
// 各スコープにカウンタを格納 | |
/* | |
forwardしているのでrequest.setAttribute()も有効になる。 | |
*/ | |
request.setAttribute("access_request", Integer.toString(accessRequest)); | |
http_session.setAttribute("access_session", Integer.toString(accessSession)); | |
serv_context.setAttribute("access_application", Integer.toString(accessApplication)); | |
// 画面移動 forward | |
RequestDispatcher rd = serv_context.getRequestDispatcher("/scopeResult.jsp"); | |
rd.forward(request, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment