在过滤器中设置一个应用范围内的路径
生活随笔
收集整理的這篇文章主要介紹了
在过滤器中设置一个应用范围内的路径
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在服務器啟動時,filter過濾器便開始工作,這時可以在過濾器中設置一個通用的路徑,存放在Application范圍中,當我們在JSP超鏈接重定向使用路徑時便可以,直接調用這個路徑,是一種軟實現,省去很多麻煩
過濾器中實現
public class MyFlilter implements Filter {ServletContext sc;public void init(FilterConfig fConfig) throws ServletException {}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {sc = request.getServletContext();if (sc.getAttribute("basePath") == null) {sc.setAttribute("basePath", request.getScheme() + "://" + request.getServerPort()+ ((HttpServletRequest) request).getContextPath());}chain.doFilter(request, response);}public void destroy() {} }在servlet中調用該地址
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String path=(String)this.getServletContext().getAttribute("basePath");}轉載于:https://www.cnblogs.com/wangqilong/p/9417552.html
總結
以上是生活随笔為你收集整理的在过滤器中设置一个应用范围内的路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言 第五章 循环结构
- 下一篇: Kotlin when 流程判断