Reputation: 286
I am trying to get the Display name (Context Root) from web.xml file to avoid hardcoding a context root.
Any help would be appreciated.
Upvotes: 7
Views: 9450
Reputation: 4736
En nombre puede obtener de la clase ServletContext. Con JSF
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
ServletContext servletContext= (ServletContext) externalContext.getContext();
System.out.println("Context Name: "+servletContext.getServletContextName());
Or Within a Servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
ServletContext servletContext= getServletContext();
System.out.println("Context Name: "+servletContext.getServletContextName());
}
Upvotes: 1
Reputation: 1108922
There's some ambiguity in your question. The "display name" is not the same as "context root". To get the <display-name>
value from web.xml
, look at skaffman's answer in this question. To get the <Context path>
value from context.xml
, use ServletContext#getContextPath()
. This is often referenced as "context root" (which you also see in the URL, that part immediately after domain).
Upvotes: 12
Reputation: 403501
ServletContext.getServletContextName()
Returns the name of this web application corresponding to this ServletContext as specified in the deployment descriptor for this web application by the display-name element.
Upvotes: 12