This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to start 'main' of  a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to start Watch "how to start New topic

how to start 'main' of a servlet

Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Yesterdat I succeeded in running my second servlet .
It has no main, because the container cares of it, right?
In the book of CT Arrington, Enterprise ... with UML,
some servlets (to build HTML-pages by HTML-factories) do have a main.
My question: how do I activate such a main?
By the way, I was not able to deploy the 'Timecard' project of the formentioned book with 'j2ee' server and 'cloudscape' but the idea of 'HTML-factories' is very clever.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Can't you just call the method like you call any other method? I hope you main method is not defined like an application's main.

So much trouble in the world -- Bob Marley
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
Since a servlet is just a Java class, it can of course have a main method. When the class is run as if it were any other application the main method will be executed. This is most often done in servlets as a convenient way to test methods in your servlet during development before deployment. Since the code in the main is usually just test code, there is no reason for you to call it from your servlet.
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
You are right, it is meant as 'test case', but I was and am not able to run it. I will check again and will edit this message with the corresponding 'error'-message.
Thanks for trying to help! But I think there has to be a hopefully small amendment (after showing the error message, tomorrow afternoon I hope 24.4 now).
Please see further down, problem solved.

[This message has been edited by Peter Gragert (edited April 25, 2001).]
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
It might be helpful to see some of the code as well. Particularly the main method. I assume that what you are trying to do is run the servlet from the command line, thereby executing the main method (e.g. java MyServlet).
The main method may be calling methods that depend on certain things being setup by the servlet container, in which case the code wouldn't run. You would get an error, for instance, if the code attempted to access an HttpServletRequest.
Anyway, this is only speculation. We will most likely have to see some code and the error generated.
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
People, thanks a lot of helping.
Looking (and debugging) I can confirm your ideas.
A main in a 'servlet' is just executable from the command line.
The reason of my example fails is that using the source as
a servlet all initialization are done. But not if I call
the main. I get a NullPointerException, due to a
HtmlFactory not initialized. And that was the reason I stuck.
Either it is a flaw in the code, or eventually there is the possibility to get necessary initialization done by commandline parameter forcing the JVM to look at the server or ...
At least I saw commandlines (only a scetch not exact) like this
java -D....Factory=java.... MyJavaCode.
My problem is solved , so thanks again!

I agree. Here's the link:
subject: how to start 'main' of a servlet
Similar Threads
how to use servlet output in html
how to call the html form
Doubt in Servlet
Servlets, HTML and GIFs