浏览器向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
原文链接
https://blog.csdn.net/qq_36667885/article/details/93127029?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
request.getRequestURL() 返回全路径
request.getRequestURI() 返回除去host(域名或者ip)部分的路径
request.getContextPath() 返回工程名部分,如果工程映射为/,此处返回则为空
request.getServletPath() 返回除去host和工程名部分的路径
例如:
request.getRequestURL() http://localhost:8080/jqueryLearn/resources/request.jsp
request.getRequestURI() /jqueryLearn/resources/request.jsp
request.getContextPath()/jqueryLearn
request.getServletPath()/resources/request.jsp