1、request.getRequestURL()
返回的是完整的url,包括Http协议,端口号,工程名,servlet名
2、request.getRequestURI()
得到的是request URL的部分值,不包括Http协议,端口号
3、request.getContextPath()
返回的是当前工程名的url
4、request.getServletPath()
返回servlet名的url.
请求地址为:http://localhost:8888/Javaee2servlet/login.do
System.out.println("getRequestURL: "+request.getRequestURL());
System.out.println("getRequestURI: "+request.getRequestURI());
System.out.println("getContextPath: "+request.getContextPath());
System.out.println("getServletPath: "+request.getServletPath());
结果:
2.response.senRedirect(url)
当前request请求地址为:http://localhost:8888/Javaee2servlet/login.do
相对路径:url="lll.do". response.senRedirect("lll.do") 重定向到http://localhost:8888/Javaee2servlet/lll.do
根路径:url="/lll.do" response.senRedirect("/lll.do") 从tomcat根路径开始找,重定向到http://localhost:8888/lll.do
绝对路径:url="http://www.baidu.com" 重定向到http://www.baidu.com"
有时重定向根路径,相对路径(多一层或几层)都不能准确定向, 可用: response.sendRedirect("/"+request.getServletContext().getServletContextName()+"/lll.jsp");