zoukankan      html  css  js  c++  java
  • servlet 过滤器实现 请求转发(跳转);跨域转发请求;tomcat 环境下。

    一般的文章都有 文本内容 和图片的。我想实现一个图片服务(或服务器)来单独处理图片逻辑,和文章处理逻辑分离。于是我想到一个办法,来尝试。

    背景:

    1. 假如文章的处理在web App,就叫web1 吧。那个图片理应存放在这个app的某个文件夹下,比如路径应该为: http://localhost:8080/web1/img/1.jpg,理应在这个网站的img文件夹下有某个图片,实际是没有的。

    2. 我们实现图片逻辑的分离,新建一个web app,就叫web2吧,web2里处理所有的图片服务。某个图片的路径为:http://localhost:8080/web2/1.jpg。 图片存放在这里。

    注意其中的差别和功能: web1是我们的处理文章的逻辑(可能是你的主网站),web2是图片服务。

    思路:

    1. 写一过滤器。实现转发,当一个路径,比如 http://localhost:8080/web1/img/1.jpg 这样的URL。理应在这个网站的img文件夹下有某个图片,实际是没有的。将来自 img/*下的所有图片请求,转发到 图片服务 下。

    代码:

    package zyf.demo;
    
    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.jasper.tagplugins.jstl.core.Out;
    
    /**
     * Servlet Filter implementation class ImageFilter
     */
    @WebFilter(filterName="ImageFilter",urlPatterns="/img/*")
    public class ImageFilter implements Filter {
    
        /**
         * Default constructor.
         */
        public ImageFilter() {
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see Filter#destroy()
         */
        public void destroy() {
            // TODO Auto-generated method stub
        }
    
        /**
         * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
         */
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            // TODO Auto-generated method stub
            // place your code here
    
            // pass the request along the filter chain
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            String contextPath = req.getContextPath()+"/img/";
            String requestURI = req.getRequestURI();
            String imageName = requestURI.substring(requestURI.indexOf(contextPath)+contextPath.length());
    
            System.out.println("str1: "+imageName);
            System.out.println("getContextPath: "+req.getContextPath());
            System.out.println("getRequestURI: "+req.getRequestURI());
            System.out.println("getRequestURL: "+ req.getRequestURL());
            System.out.println("getPathInfo: "+ req.getPathInfo());
            
            if (imageName != null && !"".equals(imageName)) {
                ServletContext context2 = request.getServletContext().getContext("/web2");
                
                String newPath = "/xxx.jpg";
                RequestDispatcher requestDispatcher;
                requestDispatcher = context2.getRequestDispatcher(newPath);
                requestDispatcher.forward(req, resp);
                return;
            }
            chain.doFilter(request, response);
        }
    
        /**
         * @see Filter#init(FilterConfig)
         */
        public void init(FilterConfig fConfig) throws ServletException {
            // TODO Auto-generated method stub
        }
    
    }
    View Code

    2.在图片服务下,准备图片,处理具体的图片提供(根据业务需要处理缓存,转换等)。

    需要解决的问题:跨域访问问题

       正常情况下,是无法跨域访问的。需要配置 context.xml的跨域访问。方法:

    omcat默认不能跨WebApp进行访问
    
    [解决]:
        %TOMCAT_HOME%/conf/context.xml的Context中的属性crossContext="true"。
    
       完整的context.xml内容如下:
    <Context crossContext="true">
    View Code

    提供演示源代码下载 

    http://yunpan.cn/QN3XnTDxVIJtV 访问密码 3f63

    参考:

    http://blog.csdn.net/qfs_v/article/details/2551762

    http://mn960mn.blog.163.com/blog/static/1141030842011020112410281/

    http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html

  • 相关阅读:
    【算法】HashMap相关要点记录
    【算法】二叉树、N叉树先序、中序、后序、BFS、DFS遍历的递归和迭代实现记录(Java版)
    SpringCloud Openfeign Get请求服务传递对象的报400 Post not support的错误解决办法
    掌握 Promise 的逻辑方法
    JavaScript的执行上下文,真没你想的那么难
    一套标准的ASP.NET Core容器化应用日志收集分析方案
    在IIS中部署前后端应用,多么痛的领悟!
    吐槽一下Abp的用户和租户管理模块
    ant-design-vue中tree增删改
    微服务下的注册中心如何选择
  • 原文地址:https://www.cnblogs.com/vir56k/p/3671354.html
Copyright © 2011-2022 走看看