大家帮我看下这段application的代码哪里错了,在线等。

gjkmm 2009-01-10 02:55:12
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
<title>使用application对象实现的网页计数器</title>
</head>
<body>
<center>
<h5>使用application对象实现的网页计数器</h5>
<hr />
<%
request.setCharacterEncoding("gb2312");
request.getSession(true);
//通过getAttribute方法获取数据并赋给变量counter
String counter=(String)application.getAttribute("counter");
//判断application对象中用于存储数据值是否为空
if(counter!=""){
//如果不为,将数据从字符串转换为整型
int int_counter=Integer.parseInt(counter);z
//计算器值增1
int_counter+=1;
//将加1后的数值再转化为字符类型进行存储
String str_counter=String.valueOf(int_counter);
application.setAttribute("counter",str_counter);
}
//如果为空,则将数值1保存到application对象名称counter中
else
{
application.setAttribute("counter","1");
}
out.print("你是访问本网站的第"+counter+"位网友!");
%>
</center>
</body>
</html>
...全文
112 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenl1984 2009-01-10
  • 打赏
  • 举报
回复
从session或者application获得的对象如果不存在的话,获得的对象是null的,而不是一个空的字符串""。判断一个空的字符串用if(counter!="")也是不正确的比较方法,应该是if(counter.equals(""))。楼主的Java基础要加强
gjkmm 2009-01-10
  • 打赏
  • 举报
回复
5楼的朋友说对了,是if(counter!=null),那为什么不能"if(counter!="")"这样写哪,有什么区别呀
yztommyhc 2009-01-10
  • 打赏
  • 举报
回复
先判断空指针,再转整形。
gjkmm 2009-01-10
  • 打赏
  • 举报
回复
报的是500
gjkmm 2009-01-10
  • 打赏
  • 举报
回复
那个z是先前不小心搞上去的,现在是去掉z的情况下,报的是我前面所描述的错误
djl66 2009-01-10
  • 打赏
  • 举报
回复
该是 if(counter!=null)
yangfuchao418 2009-01-10
  • 打赏
  • 举报
回复
你是报404还是500?首先打印下你的页面到底取得了值不,你说19行出错估计是没有值,导致你在后面转化使用时就报错了
phyeas 2009-01-10
  • 打赏
  • 举报
回复
应该是由空指针引起的转换异常
phyeas 2009-01-10
  • 打赏
  • 举报
回复
应该是由控制针引起的转换异常
dou_ye 2009-01-10
  • 打赏
  • 举报
回复
String counter=(String)application.getAttribute("counter");
//判断application对象中用于存储数据值是否为空
if(counter!=""){
//如果不为,将数据从字符串转换为整型

修改为

if(application.getAttribute("counter") != null){

}
然后再转换
glglglglglgllll 2009-01-10
  • 打赏
  • 举报
回复
int int_counter=Integer.parseInt(counter);z 多了个Z
String counter=(String)application.getAttribute("counter"); counter空指针

chenl1984 2009-01-10
  • 打赏
  • 举报
回复
应该是 if(counter!=null)
gjkmm 2009-01-10
  • 打赏
  • 举报
回复
它就提示19行错误,但我怎么看也看不出来哪里错了
org.apache.jasper.JasperException: An exception occurred processing JSP page /smp26.jsp at line 19

16: //判断application对象中用于存储数据值是否为空
17: if(counter!=""){
18: //如果不为,将数据从字符串转换为整型
19: int int_counter=Integer.parseInt(counter);
20: //计算器值增1
21: int_counter+=1;
22: //将加1后的数值再转化为字符类型进行存储
phyeas 2009-01-10
  • 打赏
  • 举报
回复


//判断application对象中用于存储数据值是否为空
if(counter!=""){

搂主,这是不是该用equals啊。
yztommyhc 2009-01-10
  • 打赏
  • 举报
回复
//通过getAttribute方法获取数据并赋给变量counter
String counter=(String)application.getAttribute("counter");

第一次访问这个页面的时候会报空指针异常的吧?
phyeas 2009-01-10
  • 打赏
  • 举报
回复
错误信息。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧