Servlet filters implement request Forwarding (redirection); Cross-origin forwarding requests; tomcat environment.

Source: Internet
Author: User

Generally, articles have text content and images. I want to implement an image service (or server) to process the image logic separately, which is separated from the article processing logic. So I came up with a way to try it.

Background:

1. If the article is processed in the web App, it is called web1. The image should be stored in a folder of the app. For example, the path should be http: // localhost: 8080/web1/img/1.jpg, there should be an image in the img folder of this website, but it does not actually exist.

2. We implement the separation of image logic and create a new web app called web2, which processes all image services in web2. The path of an image is http: // localhost: 8080/web2/1.jpg. The image is stored here.

Note the differences and functions: web1 is the logic of our article processing (maybe your main website), and web2 is the image service.

Ideas:

1. Write a filter. Implement forwarding when a path, such as http: // localhost: 8080/web1/img/1.jpg. There should be an image in the img folder of this website, but it does not actually exist. Forward all image requests from img/* to img.

Code:

= "ImageFilter", urlPatterns = "/img/*" ImageFilter HttpServletRequest req = req. getContextPath () + "/img/" = requestURI. substring (requestURI. indexOf (contextPath) + "str1:" + "getContextPath:" + "getRequestURI:" + "getRequestURL:" + "getPathInfo:" + (imageName! = &&! "" = Request. getServletContext (). getContext ("/web2" = "/xxx.jpg" = init (FilterConfig fConfig)View Code

 

2. Prepare images and process specific images under the IMG (process cache and convert Images Based on business needs ).

Problem to be solved: Cross-origin access problem

Under normal circumstances, cross-origin access is not allowed. Cross-origin access to context. xml needs to be configured. Method:

Attribute crossContext = "true" <context crossContext = "true">View Code

 

Demo source code download

Password 3f63 http://yunpan.cn/QN3XnTDxVIJtV

 

Refer:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.