Java Web中跟path相关的21种情况
请求地址为http://192.168.1.4:8080/07_servlet/parameterServlet?username=long&password=123
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("RequestURL:"+req.getRequestURL()); System.out.println("RequestURI:"+req.getRequestURI()); System.out.println("ContextPath:"+req.getContextPath()); System.out.println("ContextPath:"+req.getServletContext().getContextPath()); System.out.println("ServletPath:"+req.getServletPath()); System.out.println("QueryString:"+req.getQueryString()); System.out.println("Scheme:"+req.getScheme()); System.out.println("Protocol:"+req.getProtocol()); System.out.println("ServerName:"+req.getServerName()); System.out.println("ServerPort:"+req.getServerPort()); System.out.println("RealPath:"+req.getRealPath("/")); System.out.println("RealPath:"+req.getServletContext().getRealPath("/")); System.out.println("RemoteHost:"+req.getRemoteHost()); System.out.println("RemoteAddr:"+req.getRemoteAddr()); System.out.println("RemotePort:"+req.getRemotePort()); System.out.println("LocalName:"+req.getLocalName()); System.out.println("LocalAddr:"+req.getLocalAddr()); System.out.println("LocalPort:"+req.getLocalPort()); }
结果:
RequestURL:http://192.168.1.4:8080/07_servlet/parameterServlet RequestURI:/07_servlet/parameterServlet ContextPath:/07_servlet ContextPath:/07_servlet ServletPath:/parameterServlet QueryString:username=long&password=123 Scheme:http Protocol:HTTP/1.1 ServerName:192.168.1.4 ServerPort:8080 RealPath:D:\code-library\JavaWeb\out\artifacts\07_servlet_war_exploded\ RealPath:D:\code-library\JavaWeb\out\artifacts\07_servlet_war_exploded\ RemoteHost:192.168.1.4 RemoteAddr:192.168.1.4 RemotePort:58388 LocalName:DESKTOP-HJQ03GQ LocalAddr:192.168.1.4 LocalPort:8080
请求地址为http://192.168.1.4:8080/07_servlet/parameterServlet/pathinfo?username=long&password=123
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("PathInfo:"+req.getPathInfo()); System.out.println("PathTranslated:"+req.getPathTranslated()); System.out.println("ServletPath:"+req.getServletPath()); }
结果:
PathInfo:/pathinfo
PathTranslated:D:\code-library\JavaWeb\out\artifacts\07_servlet_war_exploded\pathinfo
ServletPath:/parameterServlet
request.getHeader("Referer"); 服务器询问此请求是从哪个页面链接过来的 防止非法请求 如盗链,如未登录 统计流量 如百度 如广告链接 注意 通过window.open或设置location无效
引申:Referrer-Policy <meta name="referrer" content="origin-when-crossorigin">
浙公网安备 33010602011771号