zoukankan      html  css  js  c++  java
  • 在过滤器中设置一个应用范围内的路径

    在服务器启动时,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");
        }
  • 相关阅读:
    Elastic Search(一)
    嵌入式jetty
    mybatis中的#{}和${}的区别
    拦截器和过滤器的区别
    springboot对拦截器的支持
    Springboot对filter的使用
    springboot对监听器Listener的使用
    随机数的基本概念
    hashset和treeset区别
    java中常见的api方法记录
  • 原文地址:https://www.cnblogs.com/wangqilong/p/9417552.html
Copyright © 2011-2022 走看看