请帮我看看这个JSP错误!谢谢

来源:百度知道 编辑:UC知道 时间:2024/05/26 00:37:58
下面是两个实现网页计数器功能的JSP页面,其中application.getAttribute()这个函数在一个页面出现空指针异常,在另一个页不出现空指针异常这是为什么?
<%@ page language="java"contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>
网页计数器
</title>
</head>
<body>
<jsp:include page="count1.jsp">
<jsp:param name="name1"value="test"/>
</jsp:include>
</body>
</html>

<%@ page language="java"contentType="text/html;charset=gb2312"%>
<%
int count=1;
String name=request.getParameter("name1");
String str=null;
str=application.getAttribute(name).toString();//这里为什么有空指针异常?
if(str==null)
{
application.setAttribute(name,String.valueOf(count));
}
else
{
count=Integer.parseInt(str);
out.print("您是第"+count+"访客");
count++;
applic

String str=null;
str=application.getAttribute(name).toString();你这两句表明str是空的
String name=request.getParameter("name1"); 但你这句要去获取name的值,既然你都没有输入怎么能获取name的值呢?

解决办法:提供输入框<input type="text" name="name" />输入name值String name=request.getParameter("name");获取输入值,然后application.jsp来计数。

可能是因为你get的那个属性没得到。
所以就有空指针了

把toString()去掉就不会有空指针了.