getPathInfo() getServletPath() getContextPath() getRequestURL() getRequestURI()
2017-11-07 10:29
92 查看
day117为项目名
1.在servlet中访问
(一) web.xml
<servlet> <servlet-name>Aservlet</servlet-name> <servlet-class>day117.Aservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Aservlet</servlet-name> <url-pattern>/test/*</url-pattern> </servlet-mapping>
(二) servlet
package day117; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Aservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String pathInfo = request.getPathInfo(); System.out.println("pathInfo:"+pathInfo); String servletPath = request.getServletPath(); System.out.println("servletPath:"+servletPath); String contextPath = request.getContextPath(); System.out.println("contextPath:"+contextPath); StringBuffer requestURL = request.getRequestURL(); System.out.println("requestURL:"+requestURL); String requestURI = request.getRequestURI(); System.out.println("requestURi:"+requestURI); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
(三) 运行结果
访问 http://localhost/day117/test/hello/world?name=jackpathInfo:/hello/world
servletPath:/test
contextPath:/day117
requestURL:http://localhost/day117/test/hello/world
requestURi:/day117/test/hello/world
2.在jsp中访问
path.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String pathInfo = request.getPathInfo(); System.out.println("pathInfo:"+pathInfo); String servletPath = request.getServletPath(); System.out.println("servletPath:"+servletPath); String contextPath = request.getContextPath(); System.out.println("contextPath:"+contextPath); StringBuffer requestURL = request.getRequestURL(); System.out.println("requestURL:"+requestURL); String requestURI = request.getRequestURI(); System.out.println("requestURi:"+requestURI); %> </body> </html>
(三) 运行结果
访问 http://localhost/day117/path.jsp?name=jackpathInfo:null
servletPath:/path.jsp
contextPath:/day117
requestURL:http://localhost/day117/path.jsp
requestURi:/day117/path.jsp
相关文章推荐
- getContextPath、getServletPath、getRequestURI、getRealPath、getRequestURL、的区别
- request.getContextPath().getRequestURI().getServletPath().getRequestURL().getRealPath() 图解
- getRequestURL、getRequestURI、getContextPath、getServletPath和getRealPath的区别
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区别
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区
- Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区别
- Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath
- Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
- Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
- Request中getContextPath、getServletPath、getRequestURI、getRequestURL、getRealPath的区别
- request.getRequestURI()、getRequestURL()、getContextPath()、getServletPath()、getPathInfo()
- request.getRequestURI() 、request.getRequestURL() 、request.getContextPath()、request.getServletPath()区
- getContextPath、getServletPath、getRequestURI的区别
- getContextPath、getServletPath、getRequestURI的区别
- getContextPath、getServletPath、getRequestURI的区别
- Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
- Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
- Context Path + servlet path + path info = request uri