項目在本地軟件和在服務器上的項目路徑如果寫死,有可能會出現項目在本機上可以訪問,架設在服務器上后就不能訪問
這兒介紹在ssm框架中使用
@WebServlet(urlPatterns = {},loadOnStartup = 2)
public class WebPathInitServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
config.getServletContext().setAttribute("ctx",config.getServletContext().getContextPath());
super.init(config);
}
}
這個java文件放在這個位置

就可以在前端的jsp頁面使用了,使用方式${ctx}

這樣使用就不會出現項目架設在服務器之后,訪問路徑出錯的情況了,這種方式本人只在ssm框架中使用過
