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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM