Get display name from web.xml file
I am trying to ge开发者_StackOverflowt the Display name (Context Root) from web.xml file to avoid hardcoding a context root.
Any help would be appreciated.
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.
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).
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());
}
精彩评论