0
을이 세션을 생성하고 현재 세션보기는 현재 세션
@WebServlet("/ShowSession.do")
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession();
String header;
Integer accessCount = (Integer)request.getAttribute("accessCount");
if(accessCount == null){
accessCount = new Integer(0);
header = "Welcome New Comer";
}else{
header = "Welcome Back!";
accessCount = new Integer(accessCount.intValue() + 1);
}
//Integer is immutable data structure. so we cannot
//modify the old one in-place,Instead, you have to
//allocate a new one and redo setAttribute
session.setAttribute("accessCount", accessCount);
session.setAttribute("heading", header);
RequestDispatcher view = getServletContext().getRequestDispatcher("/showsession.jsp");
view.forward(request, response);
}
}
에 accessCount를 저장하고이 세션의 내용을 출력보기 내 서블릿되어 읽는되지
<body>
<%
HttpSession clientSession = request.getSession();
String heading = (String) clientSession.getAttribute("heading");
Integer accessCount = (Integer) clientSession
.getAttribute("accessCount");
%>
<h1>
<center>
<b><%=heading%></b>
</center>
</h1>
<table>
<tr>
<th>Info type</th>
<th>Value</th>
<tr>
<tr>
<td>ID</td>
<td><%=clientSession.getId()%></td>
</tr>
<tr>
<td>Creation Time</td>
<td><%=new Date(clientSession.getCreationTime())%></td>
</tr>
<tr>
<td>Time of last Access</td>
<td><%=new Date(clientSession.getLastAccessedTime())%></td>
</tr>
<tr>
<td>Number OF Access</td>
<td><%=accessCount%></td>
</tr>
</table>
</body>
문제는 이미 많은 시간에 대한 접근에도 불구하고 여전히 나에게 accessCount == 0,
내가 로컬 호스트에 접근하고를 반환한다는 것입니다 : 8080/someFolderName/ShowSession.do
OH SHOOT !! 잘 잡으세요! 세션보다는 오히려 사용 된 요청! 감사! – user962206
당신은 환영합니다 :) –
하지만 기다리십시오, 그것은 여전히 null을 반환합니다 – user962206