• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to start 'main' of a servlet

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic