session只有在调用request.getSession()或request.getSession(true)时才会创建。注意jsp由于默认指令:<%@ page session=“true”%>,导致在访问jsp时也会创建session,置为false可关闭。
如:
启动一个项目,浏览器中输入localhost:8088来访问该项目,进入到欢迎页面,第一次时请求和相应如下:
由于第一次访问,cookie里没有sessionId,而访问的jsp页面由于有使用了默认的<%@ page session=“true”%>,也就是会创建一个session,并把sessionId通过cookie返回给浏览器,即响应中的set-Cookie字段。
第二次再访问时请求和响应如图:
可见这时请求中就把sessionId带上了。
另外,至于为什么输入localhost:8088就可以访问到项目中的index.jsp,是由于在tomcat安装目录下的conf目录中web.xml文件中的如下配置决定的
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
如果想更改欢迎页,也可以在项目中的web.xml中添加配置:
<welcome-file-list>
<welcome-file>index2.jsp</welcome-file>
</welcome-file-list>