以访问的jsp为:http://localhost:8080/servlet_01_war/index.jsp,
工程名为/servlet_01_war
为例:
getScheme()
返回的是当前连接使用的协议,一般应用返回的是http、SSL返回的是https
System.out.println(req.getScheme()); // http
getServerName()
返回当前页面所在的服务器的名字
System.out.println(req.getServerName()); // localhost
getServerPort()
返回当前页面所在的服务器使用的端口
System.out.println(req.getServerPort()); // 8080
getContextPath()
返回当前页面所在的应用的名字(工程名)
System.out.println(req.); // /servlet_01_war
getServletPath()
返回当前页面所在目录下全名称
System.out.println(req.getServletPath()); // /login
getRequestURL()
返回IE地址栏地址
System.out.println(req.getRequestURL()); // http://localhost:8080/servlet_01_war/login
getRequestURI()
返回包含工程名的当前页面全路径
System.out.println(req.getRequestURI()); // /servlet_01_war/login