0

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.

4

1 に答える 1

0

サーブレット 3 では、これを実行して、特定のサーブレットの現在のマッピングを取得できます。

String servletName = servletConfig.getServletName();
ServletRegistration reg = servletConfig.getServletContext().getServletRegistration(servletName);
for(String mapping: reg.getMappings()) {
    // Do something with the mapping
}

ポート番号については、サーブレット コンテナが複数のポートで「リッスン」できるという事実を考えると、ポート番号が 1 つしかない場合でも、ポート番号を知る標準的な方法はありません。

于 2013-08-28T10:36:51.137 に答える