开发者

Starting a servlet from a main in java

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


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:

  • Embedding Jetty tutorial - ServletContext.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜