Because servlets, like applets, do not take constructors.  They
are "constructed" (instantiated) by their respective containers
within their respective contexts (a browser with an applet, a
container with a serlvet).  Instead of a constructor, use the
servlet init() method to initialize your counter:

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

public class CounterCons extends HttpServlet {
    int count;

    public void init( ServletConfig config) throws
ServletException {
        super.init( config);
        count = 1122;
    }

    public void doGet(HttpServletRequest req,HttpServletResponse
res) throws IOException {
        PrintWriter out = res.getWriter();
        count++;
        out.println( count);
    }
}

Cheers!
Mark

----- Original Message -----
From: "balwinder singh" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 20, 2001 6:00 PM
Subject: Re: [Re: Servlets constructors]


> My question is :
>  If I want to start my counter(see code below) with a default
value (say 1122)
> then if i use constructor of
>  my servlet to do this, what is wrong with the code?why cant i
use construtor
> in this way in servlets?
>
>
>
> import java.io.*;
> import javax.servlet.http.*;
> import javax.servlet.*;
>
> public class CounterCons extends HttpServlet
>  {
>     int count;
>
>
>      CounterCons(int i)
>       {
>         count=i;
>       }
>     public void doGet(HttpServletRequest
req,HttpServletResponse res) throws
> IOException
>      {
>        new CounterCons(1122);
>        PrintWriter out=res.getWriter();
>        count++;
>        out.println(count);
>      }
>
>  }
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> "T.A. Flores" <[EMAIL PROTECTED]> wrote:
> So what exactly is your question?
>
>
> ----- Original Message -----
> From: balwinder singh <[EMAIL PROTECTED]>
> Date: Friday, April 20, 2001 2:03 pm
> Subject: Servlets constructors
>
> > Hi
> > I am new to Servlets and a lil curious about this question:
> >
> > The problem is:
> >
> >     In JDK1.1 servlet Constructor were not allowed.But are
they
> > allowed in
> > JDK1.3?
> >     If no,then why not? and if yes then pls give an example.
> > Regards
> > Balwinder Singh
> > web developer
> >
> >
> >
_________________________________________________________________
___
> > Get free email and a permanent address at
> > http://www.netaddress.com/?N=1
> >
>
_________________________________________________________________
_______
> ___
> > To unsubscribe, send email to [EMAIL PROTECTED] and
include in
> > the body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http:
> > Resources: http://java.sun.com/products/servlet/external-
> > resources.htmlLISTSERV Help:
> > http://www.lsoft.com/manuals/user/user.html
>
>
_________________________________________________________________
__________
> To unsubscribe, send email to [EMAIL PROTECTED] and include
in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives:
http://archives.java.sun.com/archives/servlet-interest.html
> Resources:
http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>
>
_________________________________________________________________
___
> Get free email and a permanent address at
http://www.netaddress.com/?N=1
>
>
_________________________________________________________________
__________
> To unsubscribe, send email to [EMAIL PROTECTED] and include
in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives:
http://archives.java.sun.com/archives/servlet-interest.html
> Resources:
http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to