zoukankan      html  css  js  c++  java
  • 从0开始学Java——JSP&Servlet——HttpServletRequest相关的几个路径信息

    在HttpServletRequest中有几个获取路径的接口:getRequestURI/getContextPath/getServletPath/getPathInfo

    这些接口互相之间有什么区别,通过下面这段代码就可以分辨清楚了:

    复制代码
    复制代码
     1 @WebServlet("/hello.view")
     2 public class FirstServlet extends HttpServlet {
     3         /*....*/ 4     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     5         // TODO Auto-generated method stub 6         response.setContentType("text/html;charset=UTF-8");
     7         PrintWriter out = response.getWriter();
     8         String name = request.getParameter("name");
     9         out.println("<html>");
    10         out.println("<head>");
    11         out.println("<title>Hello servlet </title>");
    12         out.println("</head>");
    13         out.println("<body>");15         out.println("<br>requestUri: " + request.getRequestURI());
    16         out.println("<br>contextPath: " + request.getContextPath());
    17         out.println("<br>servletPath: " + request.getServletPath());
    18         out.println("<br>pathInfo: " + request.getPathInfo());
    19         out.println("</body>");
    20         out.println("</html>");
    21         out.close();
    22     }
    23         /*....*/
    复制代码
    复制代码

    这个项目的名称是FirstServlet,运行之后其输出内容如下:

    requestUri: /FirstServlet/hello.view ,这个相当于是当前页面的全路径,但是不包括url中参数信息。
    contextPath: /FirstServlet ,这个就相当于iis中的虚拟目录名称
    servletPath: /hello.view ,这个其实就是页面的名称
    pathInfo: null,这个其实就是requestUri - contextPath - servletPath,也就是如果该servlet是在某个子目录下,那么这里显示的就是那个子目录名称。

     



  • 相关阅读:
    hdu 1535 Invitation Cards(spfa)
    hdu 1317 XYZZY
    hdu 1245 Saving James Bond
    hdu 1546 Idiomatic Phrases Game
    hdu 1217 Arbitrage(佛洛依德)
    hdu 1599 find the mincost route(无向图的最小环)
    poj1579
    poj1905
    poj1384
    poj3624
  • 原文地址:https://www.cnblogs.com/strinkbug/p/4885421.html
Copyright © 2011-2022 走看看