How can I run servlet without user interacting

I want to excute a servlet to get server name,server port and ContextPath from request. But I don't want to invoke servlet by user interacting. I want excute this servlet by Java code. I'm not sure it's possible. Please give me recommendation.


Why not just simply make use of URL to request the servlet.

For more sophisticated use try HttpClient.


If you mean you just want to execute some code to extract information from the request, why are you trying to invoke a servlet? Just write a method somewhere, and call it.

You could also use a Servlet Filter to extract this info on every request, add the results to the HttpServletRequest object as an attribute, so that any other servlets that process the request can find it and use it.

If you mean you want to make the servlet load at startup, you can add this within the web.xml configuration:

<servlet>
  ...
  <load-on-startup>1</load-on-startup>
</servlet>

If your servlet is doing something like calling a service every x minutes without a user interaction then you probably want to do something different.

You can use a ServletContextListener to listen to when the web context is started and to then start whatever code you want.

http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html

链接地址: http://www.djcxy.com/p/67808.html

上一篇: 如何在init方法中获得servlet的本地映射?

下一篇: 我如何在没有用户交互的情况下运行servlet