How to find the host and port on which the servlet is running

How can I find the host and port that Servlet running on without using HttpServletRequest .

I need to know this on time when my servlet is initialized, i.e. in the Servlet#init method.

+4
source share
1 answer

http://docstore.mik.ua/orelly/java-ent/servlet/ch04_01.htm#ch04-33108

The servlet uses the getInitParameter () method to access its init parameters:

 public String ServletConfig.getInitParameter(String name) 

you define host init-param in web.xml :

 <web-app> <servlet> <servlet-name>MyServletName</servlet-name> <servlet-class>com.mycompany.MyServlet</servlet-class> <init-param> <param-name>host</param-name> <param-value>myhost.mycompany.com</param-value> </init-param> </servlet> </web-app> 

and get it from Servlet.init() as follows:

 public void init() throws ServletException { getServletContext().log("init"); // Get the value of an initialization parameter String value = getServletConfig().getInitParameter("host"); 
0
source

Source: https://habr.com/ru/post/1401685/


All Articles