简体   繁体   中英

Servlet Session

I have written a small piece of code to understand session...here is the code

public class SessionServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws    IOException,ServletException
{
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
if(session.isNew())
{
out.println("Client has not responded");

}
else out.println("Client has responded");
}
}

With my understand while running i program for the first time it should "Client has not responded" however its its printing the first statement...can one explain why its doing that.

您的代码似乎还可以,要进行测试,请确保清除浏览器缓存/ Cookie ,以确保删除了旧会话。

If its JSP your accessing before the request reaches the servlet , you should probably check for a setting

<%@ page session="true" %>

in JSP.

Change it to <%@ page session="false" %> , because having it true , means JSP will participate in a session and no session found it will create one , even before the request reaches the servlet

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM