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
Thanks for your answers Actually, my problem is : I have an web application and a WebService (on diferent server). I want web application invoke webservice (after specific duration) to send data (domain, port,context path..) to webservice so that I can know where my web application is deployed.
To solve this problem I have created a servlet to call WebService and by using this servlet I have HttpServletRequest object and I can get domain, port, context path.Then I can send data to webservice. But I don't know how to execute this servlet(after specific duration) without user interaction