How to get local mapping of a servlet in its init method?
I've a servlet and it's init method must do some HTTP calls to itself. This is because I'm using an embedded app, that starts up and it's main interface it's a RESTful API. I can't and don't really want to use the internal classes, because they are not documented and difficult to use. So I prefere to use the REST API through local HTTP.
So, I extended the servlet that comes with the App and I modified the init method, so that it starts a thread and it does some HTTP calls to itself. For the moment I hardwired " http://localhost:port/servlet/mapping/
" as the path, but I'd like to have something dynamic that could at least detect the port number and the mapping too.
Is there any decent way to do this? I found lots of examples that extract that information from the HttpServletRequest
object, but in the init method you don't have it. All you have is the ServletContext
.
Ah, by the way, I use servlet API 3.0.
In servlets 3 you can do this to get current mappings for a given servlet:
String servletName = servletConfig.getServletName();
ServletRegistration reg = servletConfig.getServletContext().getServletRegistration(servletName);
for(String mapping: reg.getMappings()) {
// Do something with the mapping
}
About the port number, given the fact that a servlet container can be "listening" on more than one port, there is no standard way you can know the port number even if there is only one.
链接地址: http://www.djcxy.com/p/67810.html