Forum Moderators: phranque

Message Too Old, No Replies

Invoking a servlet

main not found

         

gbergeron

3:51 am on May 27, 2003 (gmt 0)

10+ Year Member



If I invoke a servlet, does it have to have the main function defined in it. I want to override the servlet.jar definition of doGet:

public class CurrencyConverter extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {

From my html, I'm invoking CurrencyConverter.

It always errored out so I ran it from the command prompt(JAVA CurrencyConverter 100) and I get this back:

Exception in thread "main" java.lang.NoSuchMethodError: main

Do I have to have a public static void main in there? If so how would I call doGet from main.

Thanks for any replys.

txbakers

3:57 am on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I'm very new to Java, but I think a standalone Java program needs a main to be called from the prompt.

In the main, I would try just the one call to doGet(); and see what happens.

I'm spending most of my time learning beans and JSP. So much to learn.

bcc1234

4:08 am on May 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your servlets do not need main, but if you want to run it as a stand-alone then you have to create it.

If you do so and then call doGet, you won't be able to pass vald request and referer objects so I don't really see the point.

Other than that, a servlet is just a class like any other.

gbergeron

10:08 pm on May 27, 2003 (gmt 0)

10+ Year Member



I took an exising java program an ran it. Then I went back and changed the name from main to something else. e.g.

public static void xxx(String[] args)

This time when I ran it, I got the following error

Exception in thread "main" java.lang.NoSuchMethodError: main

the only thing i changed was main to xxx. Should I be declaring something eles? Thanks so much

garann

11:06 pm on May 27, 2003 (gmt 0)

10+ Year Member



It sounds like you ran it from the command line? If so, and you renamed the main method to xxx, it didn't find a main method and so didn't know where to begin executing.

If you want to test your servlet from the command line, you'll have to have a main method that calls a modified doGet() with no arguments (since, as bcc1234 says, you can't get a new HttpServletRequest or -Response). That will test the logic, but if you're counting on some input from the Request, you'll have to find another way to pass it in.

Also, I'd think that you wouldn't want to invoke your Servlet from your page scriplets - it's been months since I've done Java, but shouldn't you be using a Bean? I thought I remembered that doGet() and doPost() were generally called as the action of an HTML form..

Best of luck,
g.

bcc1234

3:15 am on May 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can run doGet(null,null), but as I said it's useless.
A servlet that does not interact with the container is in fact a useless servlet.

If you need to do some testing, then add System.err.print statements that would evaluate and display whatever you need and then check the error stream log of your servlet engine.