I know the request object has a function to get the server name. (i.e. HttpServletRequest.getServerName() )
What if I need the same functionality inside the initialization of a servlet? How do I do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This information is request based and not strictly application based. It can namely change per request. All you have at hands during servlet initialization is the
ServletContextinstance which in turn offers methods likegetInitParameter(). You could make use of it to access application wide settings.So your best bet is to manually set the server name as a context parameter in
web.xmlso that you can obtain it as follows in servlet’s
init()method:Another (not recommended) alternative is to set it as display name in
web.xmlso that you can obtain it as follows: