【发布时间】:2015-08-26 21:59:54
【问题描述】:
我的应用程序中有以下代码:
private String getRequestPath(HttpServletRequest req) {
String path = req.getRequestURI();
path = path.replaceFirst( "^\\Q" + req.getContextPath() + "\\E", "");
path = URLDecoder.decode(path);
System.out.println("req.getRequestURI()="+req.getRequestURI());
System.out.println("path="+path);
return path;
}
当我尝试访问此方法所属的 servlet 时,我可以在输出中看到以下消息:
req.getRequestURI()=/MyApp/test
path=/test
^\\Q 和 \\E 在正则表达式中的工作原理。
【问题讨论】:
-
你不应该那样使用它们。改用
Pattern.quote- 更安全