javaweb学习9_javaweb中绝对路径跟相对路径的问题

1.绝对路径的问题
  1)开发时建议编写"绝对路径":写绝对路径肯定没问题,但是写相对路径可能会出问题
  在由 Servlet 转到 JSP 页面时,此时浏览器地址栏上显示的是Servlet 的路径,而 若JSP页面的超链接还是相对JSP页面的地址,则会出现路径混乱的问题。

2)编写绝对路径可以避免上述问题:

  ① 在javaWEB中什么叫“就对路径”:相对于当前 web 应用的根路径(contextPath)的路径。即,任何的路径都必须带上contextPath
    如:http://localhost:8080/javaWEB(contextPath,当前web应用的上下文路劲)/a.jsp 
 

  ② 如何编写:若 / 代表站点目录,则在前面加上 contextPath 就可以
      request.getContextPath();
      application.getContextPath();


3)javaWEB 开发中的 / 到底代表什么?
  ① 当前 web 应用的根路径:http://localhost:8080/contextPath/ :若 / 需要交Servlet 容器来处理

  > 请求转发时:request.getRequestDispatcher("/b.jsp").forward(request,response);


  > 在web.xml文件中,映射 servlet 访问路径:
      <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
      </servlet-mapping>

  > 各种定制标签中的 / 

  ② WEB 站点的根路径:http://localhost:8080/ :若 / 交由浏览器来处理

  > 超链接:<a href="/TestServlet">超链接</a>
  > 表单中的action:<form action="/login.jsp"></form>
  > 做请求重定向的时候:response.sendRdirect("/a.jsp")

猜你喜欢

转载自blog.csdn.net/qq_37858042/article/details/81238417
今日推荐