计数器源程序cnfig.jsp<%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" errorPage=""%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<%
int org=0;
int count=0;
try
{
org=Integer.parseInt(config.getInitParameter("counter"));
}
catch(Exception e)
{
out.println("org:"+e);
}
try
{
count=Integer.parseInt(application.getAttribute("config_counter").toString());
}
catch(Exception e)
{
out.println("config_counter"+e);
}
if(count<org)count=org;
out.println("此页面已经访问了"+count+"次");
count++;
application.setAttribute("config_counter",new Integer(count));
%>
</body>
</html>建了个虚拟目录叫jsp,然后把config.jsp放在虚拟目录中,同时在jsp目录中建了个文件叫WEB-INF,在WEB-INF里面新建了个web.xml文件,文件内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
  Copyright 2004 The Apache Software Foundation  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at      http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--><web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">  <display-name>Welcome to Tomcat</display-name>
  <description>
     study jsp
  </description><!-- JSPC servlet mappings start -->    <servlet>
        <servlet-name>config_counter</servlet-name>
        <jsp-file>/config.jsp</jsp-file>
        <init-param>
           <param-name>counter</param-name>
           <param-value>1000</param-value>
        </init-param>
    </servlet>    <servlet-mapping>
        <servlet-name>config_counter</servlet-name>
        <url-pattern>/config_counter</url-pattern>
    </servlet-mapping><!-- JSPC servlet mappings end --></web-app>重启服务器后,在浏览器中输入http://localhost:8080/jsp/config_counter,结果为config_counterjava.lang.NullPointerException 此页面已经访问了1000次 刷新一次后为:此页面已经访问了1001次 可是如果关闭服务器重启后,计数次数又是从1000开始了,我是照书打的程序,书里说是关了服务器后计数值也可以保持的,到底应该怎么办呢,怎么样才能把计数保存在web.xml里面呢???

解决方案 »

  1.   

    怎么能把计数保存在web.xml里面呢???
      

  2.   

    同意楼上!
    为什么要把计数保存在web.xml里面?
    关注!
      

  3.   

    application.setAttribute("config_counter",new Integer(count));
    把这句改成下面的:
    application.getContext("config_counter").setAttribute("counter",new Integer(count));
      

  4.   

    哈哈,我是初学servlet,书里是那样做的我就照着试一下的
      

  5.   

    application.getContext("config_counter").setAttribute("counter",new Integer(count));
    搞不定啊,有空指针异常HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: Exception in JSP: /config.jsp:3027: if(count<org)count=org;
    28: out.println("此页面已经访问了"+count+"次");
    29: count++;
    30: application.getContext("config_counter").setAttribute("counter",new Integer(count));
    31: %>
    32: </body>
    33: </html>
    Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:504)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    root cause java.lang.NullPointerException
    org.apache.jsp.config_jsp._jspService(org.apache.jsp.config_jsp:72)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.15 logs.
    --------------------------------------------------------------------------------Apache Tomcat/5.5.15