getRequestURI()、getRequestURL()、getContextPath()、request.getServletPath()的区别

浏览器向tomcat发出http请求:http://localhost:8080/web项目名/servlet的url-pattern(与在login.jsp的form表单中的action=“&&&”,&&&的名称需要与servlet配置中的url-pattern一致,主要是产生映射)

假设web application的名称为test,需要在浏览器中输入请求路径:http://localhost:8080/test/login.jsp  或  http://localhost:8080/test/url-pattern    

则执行下列代码后打印出如下结果:

1、System.out.println(request.getRequestURI());

        打印结果:/test/login.jsp

2、System.out.println(request.getRequestURL());

       打印结果:http://localhost:8080/test/login.jsp

3、 System.out.println(request.getContextPath());

       打印结果:/test

4、System.out.println(request.getServletPath());

     打印结果:/login.jsp

发布了9 篇原创文章 · 获赞 4 · 访问量 1524

猜你喜欢

转载自blog.csdn.net/qq_36667885/article/details/93127029