私はjspとサーブレットの初心者です。その中でセッション処理を学んでいます。
私は、1 つの jsp ページが jsp ページ 2 へのハイパーリンクを持っている 3 つの jsp ページを持つ単純なプログラムを実行しました。ただし、セッション オブジェクトが null の場合は、新しいセッションを作成して属性を設定し、ディスパッチャーを使用して制御を jsp ページ 3 にディスパッチします。
以下は、3 つの jsp ページすべてのコードです。
test1.jsp (jsp ページ 1 のコード)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="test2.jsp"> start here</a>
</body>
</html>
test2.jsp (jsp ページ 2 のコード)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession(false);
if(ses==null){
System.out.println("Session is null creating new session ");
%>
Session is null creating new session.
<%
//Usersession objusersession=new Usersession();
ses=request.getSession(false);
request.setAttribute("a", "This");
ses.setAttribute("name", "sandip");
System.out.println("Session created and attributes are set now dispatching");
%>
Session created and attributes are set now dispatching
<%
response.sendRedirect("test3.jsp");
//dispatch.forward(request, response);
}else{
System.out.println("Session is old then dispatching");
%>
Session is old then dispatching
<%
response.sendRedirect("test3.jsp");
//RequestDispatcher dispatch=request.getRequestDispatcher("test3.jsp");
//dispatch.forward(request, response);
}
%>
<a href="test.jsp"> Click here</a>
</body>
</html>
test3.jsp (jsp ページ 3 のコード)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession();
if(!session.isNew()){
//Usersession objusersession=new Usersession();
//ses.setAttribute("name", "sandip");
//request.getAttribute("a");
//System.out.println(request.getAttribute("a"));
System.out.println(ses.getAttribute("name"));
%>
printed the session attribute value on console.
<%
}else{
response.sendRedirect("test1.jsp");
}
%>
</body>
</html>
上記のコードでシーケンスを直接呼び出すと、次のようになります
1) test2.jsp へのハイパーリンクを持つ test1.jsp を呼び出します。 2) ハイパーリンクをクリックすると、test2.jsp が呼び出されます。test2.jsp file3 では、既存のセッションをチェックします。見つかった場合は test3.jsp を直接呼び出す必要がありますが、既存のセッションが存在しない場合は、新しいセッションを作成して属性を設定し、この属性値をコンソールに出力する test3.jsp を呼び出す必要があります。
私の場合、最初に test1.jsp を呼び出してハイパーリンクをクリックすると、test2.jsp が呼び出され、セッションが既に存在することがわかり、test3.jsp を直接呼び出します。しかし実際には、test2.jsp の if ブロックに入らない限り、test1.jsp でも test2.jsp でもセッションは開始されません。私の質問は、アプリケーションでセッションがどのように自動的に作成されるかです。
私は間違ったコーディングを行っているか、概念を間違って理解していると確信しています。
また、test2.jsp ページを、test2.jsp ページの用量と同じタスクを実行するサーブレットに置き換えましたが、それでも同じ結果が得られます。
専門家に聞きたいのですが、正確に何が間違っているのか教えてください。ありがとう!