I have below code in my application:
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;
}
In the output I can see below messages when I try to access the servlet which this method belongs to:
req.getRequestURI()=/MyApp/test
path=/test
How the ^\\Q
& \\E
works in regular expressions.
Pattern.quote
instead - it's safer