问题描述
刚学jsp想用session做一个站点计数器,代码都是按书上打的,但我的运行出来后却总是:“您是第null个访问的人。”这是为什么??上代码!:<body><%!intnumber=0;synchronizedvoidcountpeople()/*{number++;}%><%if(session.isNew()){countpeople();Stringstr=String.valueOf(number).toString();session.setAttribute("count",str);}%><p>您是第<%=(String)session.getAttribute("count")%>个访问的人。</body></html>
解决方案
解决方案二:
你确定书上是这么写的?在同一个页面?
解决方案三:
打断点调试看看。
解决方案四:
<%@pagelanguage="java"contentType="text/html;charset=GBK"pageEncoding="utf-8"%><body><%!intnumber=0;synchronizedvoidcountpeople(){number++;}%><%if(session.isNew()){countpeople();Stringstr=String.valueOf(number).toString();session.setAttribute("count",str);}%><p>你是第<%=(String)session.getAttribute("count")%>个访问该网站的人。</p></body></html>
解决方案五:
我的可以啊,跟你的代码完全一样啊!!!我的代码:<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>MyJSP'index.jsp'startingpage</title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"><!--<linkrel="stylesheet"type="text/css"href="styles.css">--></head><body><%!intnumber=0;publicsynchronizedvoidcountpeople(){number++;}%><%if(session.isNew()){countpeople();Stringstr=String.valueOf(number);session.setAttribute("people",str);}%>您是今天第<%=session.getAttribute("people")%>个访问者</body></html>
解决方案六:
是否禁用了浏览器的cookiesession是存在服务器,但是需要浏览器在访问时传递一个sessionId所以本地是要启用cookie才能保存sessionId的。
解决方案七:
看看本機的ie設置