为什么一直是404
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <img alt="" src="/images/desktop.jpg"> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <img alt="" src="/images/mobile.jpg" style="width:100%"> </body> </html>
package com.zt.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeviceAdapter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
String url=req.getRequestURI();
System.out.println(url);
if(url.startsWith("/desktop") || url.startsWith("/mobile")) {
chain.doFilter(request, response);
}else {
String userAgent=req.getHeader("user-agent").toLowerCase();
String newUrl="";
if(userAgent.indexOf("iPhone")!=-1 || userAgent.indexOf("android")!=-1) {
newUrl="/mobile" + url;
System.out.println("移动设备正在访问,重写跳转URL:"+newUrl);
res.sendRedirect(newUrl);
}else {
newUrl="/desktop" + url;
System.out.println("移动设备正在访问,重写跳转URL:"+newUrl);
res.sendRedirect(newUrl);
}
}
}
}<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>filter</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>filterName</filter-name> <filter-class>com.zt.filter.DeviceAdapter</filter-class> </filter> <filter-mapping> <filter-name>filterName</filter-name> <url-pattern>*.html</url-pattern> </filter-mapping> </web-app>
直接输入locallhost:8080 也是404,这是为什么
源自:过滤器
3-1 多端设备自动适配
29
收起
正在回答 回答被采纳积分+1
3回答
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程





恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星