最近玩jsp用tomcat作为服务器,结果每次tomcat都 在cookie里自动创建了sessionid ,那么怎么才能让tomcat不自动创建sessionid,让cookie更加简洁呢?其实方法很简单,只要在第一次访问jsp的页面上加上这么一句 <%@ page session="false"%> ,就行了。下面给出例子:
info.jsp:
<%@ page language = "java" contentType = "text/html;charset=GBK" %>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page session="false"%>
<html>
<body>
<%
Cookie nc = new Cookie("info","ok");
nc.setMaxAge(60);
response.addCookie(nc);
response.sendRedirect("showCookie.jsp");
%>
</body>
</html>
showCookie.jsp:
<%@ page language = "java" contentType = "text/html;charset=GBK" %>
<%@ page import="javax.servlet.http.Cookie" %>
<%@ page session="false"%>
<html>
<body>
<%
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(int i = 0;i<cookies.length;i++){
out.println(cookies[i].getName());
if(cookies[i].getName().equals("JSESSIONID")){
out.println("读取JSESSIONID的值:"+cookies[i].getValue());
}
}
}else{
out.println("超过Cookie有效期");
}
%>
</body>
</html>
启动tomcat服务后,在浏览器里访问 info.jsp 页面就行了,可以看到以前那烦人的jsessionid不见了!