如何在頁面中使用contextPath

 

首先定義一個Listener

public class MyListener implements javax.servlet.ServletContextListener
{

	public void contextDestroyed(ServletContextEvent e)
	{
	}

	public void contextInitialized(ServletContextEvent e)
	{
		//設置web應用路徑
		e.getServletContext().setAttribute("contextPath", 
					e.getServletContext().getContextPath());
		CommonUtil.setWebPath(e.getServletContext().getRealPath("/"));
	}
}

 

然後在web.xml上使用Listener。

<listener>
	 <listener-class>com.market.servlet.MyListener</listener-class>
</listener>

 

這樣程序啓動後就會自動把程序的上下文放到“contextPath"中。

 

<link href="${contextPath}/styles/style.css" rel="stylesheet" type="text/css" />

 

這樣就顯示出contextPath的值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章