Hard to say for sure, but you might try calling "trim()" on the value you got from "getInitParameter()".
-----Original Message-----
From: sri sri [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, November 19, 2002 8:10 AM
To: [EMAIL PROTECTED]
Subject: basic counter problem

Hi all,

I am new to servlets and am trying to execute a simple counter which has an initial value.

I have the following web.xml file

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
    <servlet>
        <servlet-name>
            counter
        </servlet-name>
        <servlet-class>
            InitCounter
        </servlet-class>
        <init-param>
            <param-name>
                initial
            </param-name>
            <param-value>
          &n bsp;     1000
            </param-value>
            <description>
                The initial value for the counter  <!-- optional -->
            </description>
        </init-param>
    </servlet>
</web-app>

and my java program is

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InitCounter extends HttpServlet {

  int count;

  public void init() throws ServletException {
    String initial = getInitParameter("initial");
    try {
      count = Integer.parseInt(initial);
     }
    catch (NumberFormatException e) {
      count = 0;
    }
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                           throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;
    out.println("Since loading (and with a possible initialization");
    out.println("parameter figured in), this servlet has been accessed");
    out.println(count + " times.");
  }
}

I was expecting the counter to start printing from 1001 and so on.....but there seems to be a NumberFormat Exception and hence it is taking counter's intial value as zero.

How to get the counter value to start from 1000 instead of 0 ?

Thanks in advance

Sri Sri



Do you Yahoo!?
Yahoo! Web Hosting - Let the expert host your site

Reply via email to