1

How can you run a servlet on a server without putting it in the web.xml. I want to start a server from main and pass what servlet it should run Thank you

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
user410911
  • 65
  • 2
  • 12
  • 1
    Weird! Write a main method in Servlet and run it ! why need a server? – Gopi Aug 05 '10 at 12:52
  • What exactly are you trying to do? Why does it have to be a servlet, and like Gopi mentioned, why even a server? – Java Drinker Aug 05 '10 at 13:02
  • I'm working with MVC, the controller is needs to be able to start the server and run the servlet – user410911 Aug 05 '10 at 13:02
  • I need a to be able to fill a queue of method invocations on the server. I either need and instance of a running server(I already posted this) or start it myself – user410911 Aug 05 '10 at 13:05

1 Answers1

1

That depends entirely on the server you're using. If it's an embedded server like Jetty, then you can easily do that in flavor of a ServletHolder.

context.addServlet(new ServletHolder(new HelloServlet()),"/*");

If it's other, then you need to consult the documentation of the server in question. It's usually not possible in non-embeddable servers like Tomcat, Glassfish and so on.

See also:


Update: as per the comments on your question, you're after all looking for a solution in the wrong direction. Reread the MVC pattern and more specifically the front controller pattern. You shouldn't use servlets as domain objects, but just simple constructable Java classes which don't extend HttpServlet. Finally you just end up with a single servlet which constructs/picks the right domain object based on the current request. You can find some insights and a basic kickoff example in this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm using GWT's GAE, which is Jetty. – user410911 Aug 05 '10 at 13:11
  • I know I should end up with one servlet. I didn't understand ur update? I cannot change the controller. It calls an instance of a view. This view needs to start the server(I can do this in GWT) I don't know how to access the java servlet running on it. – user410911 Aug 05 '10 at 13:20