----- Original Message -----
From: Chris Gow <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, August 05, 1999 11:33 PM
Subject: Re: Servlet Errors when a Constructor is present


> You have to create a public noargs constructor such as:
>
> public MyServlet(){
>         file://Do your stuff here
> }
>
> This is because, in the absense of *any* constructors, the Java VM
pretends
> that your class has a default (no args) constructor.  But when you supply
> your own Constructors, the VM is no longer smart enough to pretend to call
> the default constructor (why this is I don't know).
>
> You also have to make sure that your no args constructor has public scope.
> This is because another class is creating your Object.
>
>
> Chris Gow
>
> [EMAIL PROTECTED]
>
> > -----Original Message-----
> > From: SERVE 'EM - Dan Coggeshall [SMTP:[EMAIL PROTECTED]]
> > Sent: Thursday, August 05, 1999 1:23 PM
> > To:   [EMAIL PROTECTED]
> > Subject:      Servlet Errors when a Constructor is present
> >
> > I am writing a servlet that runs fine, except when I write a constructor
> > in the same class.
> > I want to do this so that there is another entry point in my class.
> > I've seen/heard it done before, but for some reason the returning HTML
> > page says that no source (HTML source, I am hitting the servlet) was
> > returned.  My service() isn't even using the constructor.  Any help?
> >
> > DanC
> >
> >
__________________________________________________________________________
> > _
> > 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

Hi DanC and Chris,

Dan, why do you need another entry point into your Servlet, servlets that
live together can be
called from within the servlet engine by doing something like this.....

YourServletClass myServlet =
(YourServletClass)this.getServletContext().getServlet("YourServletClassFully
QualifiedName");

This will return a servlet object of the class YourServletClass. By casting
the result to your class you reveal all the public methods
and properties that you declare inside it making them available for use.

There is no need to have a noargs contructor at all, to create an instance
of your servlet you can simply do this

YourServletClass myServlet = (YourServletClass)new HttpServlet();
then use a property method (setXXX) to set the properties of the servlet

Be warned though, using the servlet in this condition is not a good idea,
only methods that have
no part in the handling of requests and responses can be guaranteed to work
and only then when they
are independent of the state of the servlet itself

Chris,
    the reason why you can't use a parameterised constructor is that the
ServletEngine won't know how to handle them
so it doesn't even bother trying. The servlet engine will probably do
something like this..

// Some custom ClassLoader actually loads the actual class file then (in
simplified form)

HttpServlet httpServlet = (HttpServlet)loadedClass.newInstance();

// does the init call to make sure the servlet actually works that much
// catches all kind of weird and wonderful exceptions and so on.

The point is that even if you had a parameterised constructor how would the
servlet engine cope with them? Normally
you would use the init(ServletConfig config) method to set up starting
conditions for a servlet, the engine
has no prior idea what, exactly should be passed to such a contructor. It
could be done but the setting up of servlet
properties and the use of the init method is a much cleaner way of doing
things.

Hope that helps

Andy Bailey

___________________________________________________________________________
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