浏览器报错
老师,输入网址http://localhost:8080/index.html 页面正常显示,但浏览器报错 : Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING ,请帮忙看看是什么原因?
package com.imooc.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
// 设备适配 过滤器,输入localhost:8080/index.html首页后,自动适配pc端还是手机端
@WebFilter(filterName = "daf",urlPatterns = "*.html") // 5.我们只希望对uri已.html的页面进行拦截和重新定向
public class DeviceAdapterFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("适配器过滤器已初始化");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse resp=(HttpServletResponse)response;
//3.作为 RequestURI,它是隶属于HttpServletRequest接口,URI是Http通讯才会使用到的一个特性
String uri=req.getRequestURI();//获取uri/index.html -> /mobile/index.html / /desktop/index.html
String userAgent=req.getHeader("User-Agent");//4.获取用户环境
System.out.println("获取到的uri为:"+uri);
System.out.println("获取到的User-Agent为:"+userAgent);
String targetUri=null;
//7.判断如果已经对字符串处理过(适配手机/pc),则将响应向后传递,不做额外处理
if (uri.startsWith("/desktop")||uri.startsWith("/mobile")){
chain.doFilter(req,resp);
}else {
if(userAgent.toLowerCase().indexOf("android")!=-1||userAgent.toLowerCase().indexOf("iPhone")!=-1){
targetUri="/mobile"+uri;//为uri增加前缀,代表手机端 /mobile/index.html
System.out.println("移动端设备正在访问,重新跳转URI:" + targetUri);
}else {
targetUri="/desktop"+uri;//为uri增加前缀,代表pc端
System.out.println("PC端设备正在访问,重新跳转URI:" + targetUri);
}
}
// 6.注意:响应重定向的过程中会重新通过浏览器发起请求,那自然也会被过滤器拦截到,每执行一次,都会在原有uri增加前缀,一直出现循环跳转,导致浏览器无法正常显示;
resp.sendRedirect(targetUri);
}
@Override
public void destroy() {
System.out.println("适配器过滤器已摧毁");
}
}<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <!-- 1.模拟pc端主页 --> <body> <img src="/image/pc.png" style="width: 100%"> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <!-- 2.模拟手机端主页 --> <body> <img src="/image/mobile.png" style="width: 100%"> </body> </html>
26
收起
正在回答 回答被采纳积分+1
2023版Java工程师
- 参与学习 人
- 提交作业 8788 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星