views:

346

answers:

5

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.

+3  A: 

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

For more sophisticated use try HttpClient.

Adeel Ansari
If that's what Chan is trying to do, I think the question would be what makes 'use of the URL' and then opens up the question of what should the parameters be for such a request.
Nick Holt
Yeah, even I tried to guess it intelligently. The first part is still not clear to me, I mean "..to get server name etc..".
Adeel Ansari
So essentially to make a request to a servlet to start it without making a request, you are making a request :)
pjp
Thats what he asked, how on earth I will know if he really means something like load-on-startup. :)
Adeel Ansari
Hey Vinegar - agreed, anything that invokes the servlet will, by definition, already know the URL, port, etc...
Nick Holt
A: 

Redirect a request with welcome page (index.jsp/index.htm etc).

adatapost
+2  A: 

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>
Sean Owen
+1  A: 

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

pjp
You guessed too, isn't it. :)
Adeel Ansari
A: 

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