半角空格 全角空格 不间断空格 通过过滤器解决
半角空格:\u0020 英文符号 代码中常用
全角空格:\u3000 中文符号 中文文章中常用
不间断空格:\u00A0 不间断空格 主要在 office 中使用,让一个单词在结尾处不会换行显示,快捷键 Ctrl + Shift + Space
半角空格和不间断空格肉眼是分辨不出来的,但是程序能够分辨出来。
public class Test { public static void main(String[] args) { System.err.println("Hello\u00A0World"); // Hello World System.err.println("Hello\u0020World"); // Hello World 常用 System.err.println("Hello\u3000World"); // Hello World String spaceA = " "; // 不间断空格 String spaceB = " "; // 半角空格 String spaceC = " "; // 全角空格 System.err.println(spaceA.equals(spaceB)); // false System.err.println(spaceA.equals(spaceC)); // false System.err.println(spaceB.equals(spaceC)); // false System.err.println("\u00A0".equals(spaceA)); // true System.err.println("\u0020".equals(spaceB)); // true System.err.println("\u3000".equals(spaceC)); // true } }
将字符串中的不连续转变成半角空格
String str = "Hello\u00A0world" str = str.replace('\u00A0', '\u0020'); // 将不间断空格替换为半角空格
在 Java Web 开发中遇到时可通过添加过滤器的方式来解决
过滤器代码如下:
package pathogen.filter; import java.io.IOException; import java.util.HashMap; import java.util.Map; 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.HttpServletRequestWrapper; public class SpecialCharacterFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 采用 Request 包装类包装 request 的方法,拦截获取参数的方法,替换特殊字符。 chain.doFilter(new SpecialCharacterRequestWrapper((HttpServletRequest)request), response); } } class SpecialCharacterRequestWrapper extends HttpServletRequestWrapper { public SpecialCharacterRequestWrapper(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { return replaceSpecialCharacter(super.getParameter(name)); } @Override public Map<String, String[]> getParameterMap() { Map<String, String[]> originalParameters = super.getParameterMap(); Map<String, String[]> newParameters = new HashMap<>(originalParameters.size()); // Lambda 表达式 + forEach() 遍历 Map originalParameters.forEach((k, v) -> newParameters.put(k, replaceSpecialCharacter(v))); // v 的数据类型是 String [] return newParameters; } @Override public String[] getParameterValues(String name) { return replaceSpecialCharacter(super.getParameterValues(name)); } private String replaceSpecialCharacter(String str) { return str == null ? null : str.replace("\u00A0", "\u0020"); // 将不连续空格替换为半角空格 } private String [] replaceSpecialCharacter(String[] str) { if (str == null) { return str; } for (int i = 0; i < str.length; i++) { str[i] = replaceSpecialCharacter(str[i]); } return str; } }
在部署描述符(web.xml)中配置过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>DataEntryServlet</servlet-name> <servlet-class>pathogen.controller.DataEntryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DataEntryServlet</servlet-name> <url-pattern>/entry</url-pattern> </servlet-mapping> <servlet> <display-name>QueryServlet</display-name> <servlet-name>QueryServlet</servlet-name> <servlet-class>pathogen.controller.QueryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>QueryServlet</servlet-name> <url-pattern>/query</url-pattern> </servlet-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>pathogen.controller.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>SpecialCharacterFilter</filter-name> <filter-class>pathogen.filter.SpecialCharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>SpecialCharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>