“计算页面的访问量,刷新页面,访问量加1” 代码出现异常
鱼琪儿 2014-08-04 08:03:37 这里用的是application对象,但是代码运行出来有异常,检查好多次,不知道问题出在哪。请大家找下错误。谢谢!
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>application对象2</title>
</head>
<body><br>
<%
String str=application.getAttribute("count").toString();
int i=0;
if(str==null)
application.setAttribute("count","1");
else
i=Integer.parseInt(str);
application.setAttribute("count",++i+"");
%>
<p>您是第<%=application.getAttribute("count")%>个访问本站的人
</body>
</html>
异常如下:
HTTP Status 500 - An exception occurred processing JSP page /application2.jsp at line 11
type Exception report
message An exception occurred processing JSP page /application2.jsp at line 11
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /application2.jsp at line 11
8:
9: <body><br>
10: <%
11: String str=application.getAttribute("count").toString();
12: int i=0;
13: if(str==null)
14: application.setAttribute("count","1");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
root cause
java.lang.NullPointerException
org.apache.jsp.application2_jsp._jspService(application2_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.41 logs.
Apache Tomcat/6.0.41