前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >路径相关问题

路径相关问题

作者头像
星哥玩云
发布2022-09-14 20:59:40
7630
发布2022-09-14 20:59:40
举报
文章被收录于专栏:开源部署

一、与路径相关的操作

  • ​ 超链接
  • ​ 表单
  • ​ 转发
  • ​ 包含
  • ​ 重定向
  • <url-pattern>
  • ​ ServletContext获取资源
  • ​ Class获取资源
  • ​ ClassLoader获取资源

二、客户端路径

超链接、表单、重定向都是客户端路径,客户端路径可以分为三种方式:

绝对路径;

​ 以“/”开头的相对路径;

​ 不以“/”开头的相对路径;

​ 例如:http://localhost:8080/hello1/pages/a.html中的超链接和表单如下:

代码语言:javascript
复制
绝对路径:<a href="http://localhost:8080/helloservlet3/index.html">链接1</a>
客户端路径:<a href="/helloservlet3/pages/index.html">链接2</a>
相对路径:<a href="index.html">链接3</a>
<hr/>
绝对路径:
<form action="http://localhost:8080/helloservlet3/index.html">
  <input type="submit" value="表单1"/>
</form>
客户端路径:
<form action="/helloservlet3/index.html">
  <input type="submit" value="表单2"/>
</form>
相对路径:
<form action="index.html">
  <input type="submit" value="表单3"/>
</form>

链接1和表单1:没什么可说的,它使用绝对路径;

链接2和表单2:以“/”开头,相对主机,与当前a.html的主机相同,即最终访问的页面为http://localhost:8080/hello2/index.html;

链接3和表单3:不以“/”开头,相对当前页面的路径,即a.html所有路径,即最终访问的路径为:http://localhost:8080/hello1/pages/index.html;

重定向1:

代码语言:javascript
复制
public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//response.sendRedirect("/hello/index.html");//主机:localhost
        response.sendRedirect("http://localhost:8080/hello/index.html");//主机:localhost
	}
}

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径以“/”开头,所以相对当前主机,即http://localhost:8080/hello/index.html。

重定向2:

代码语言:javascript
复制
public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect("index.html");
	}
}

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径不以“/”开头,所以相对当前路径,即http://localhost:8080/hello/servlet/index.html

建议使用“/”

强烈建议使用“/”开头的路径,这说明在页面中的超链接和表单都要以“/”开头,后面是当前应用的名称,再是访问路径:

代码语言:javascript
复制
<form action="/hello/servlet/AServlet">
</form>
<a href="/hello/b.html">链接</a>

其中/hello是当前应用名称,这也说明如果将来修改了应用名称,那么页面中的所有路径也要修改,这一点确实是个问题。这一问题的处理方案会在学习了JSP之后讲解!

在Servlet中的重定向也建议使用“/”开头。同理,也要给出应用的名称!例如:

代码语言:javascript
复制
response.sendRedirect("/hello/BServlet");

其中/hello是当前应用名,如果将来修改了应用名称,那么也要修改所有重定向的路径,这一问题的处理方案是使用request.getContextPath()来获取应用名称。

代码语言:javascript
复制
response.sendRedirect(request.getContextPath() + "/BServlet");

三、服务器端路径

服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:

以“/”开头;

不以“/”开头;

其中请求转发、请求包含都是服务器端路径,服务器端路径与客户端路径的区别是:

客户端路径以“/”开头:相对当前主机;

服务器端路径以“/”开头:相对当前应用;

转发1:

代码语言:javascript
复制
public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/BServlet").
            forward(request, response);
	}
}

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径以“/”开头,所以相对当前应用,即http://localhost:8080/hello/BServlet。

转发2:

代码语言:javascript
复制
public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("BServlet").
            forward(request, response);
	}
}

假设访问AServlet的路径为:http://localhost:8080/hello/servlet/AServlet

因为路径不以“/”开头,所以相对当前应用,即http://localhost:8080/hello/servlet/BServlet。

四、url-pattern路径

必须使用“/”开头,并且相对的是当前应用。

五、ServletContext获取资源

必须是相对路径,可以“/”开头,也可以不使用“/”开头,但无论是否使用“/”开头都是相对当前应用路径。

例如在AServlet中获取资源,AServlet的路径路径为:http://localhost:8080/hello/servlet/AServlet:

代码语言:javascript
复制
public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path1 = this.getServletContext().getRealPath("a.txt");
		String path2 = this.getServletContext().getRealPath("/a.txt");
		System.out.println(path1);
		System.out.println(path2);
	}
}

path1和path2是相同的结果:http://localhost:8080/hello/a.txt

六、Class获取资源

Class获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。

代码语言:javascript
复制
import java.io.InputStream;

public class Demo {
	public void fun1() {
		InputStream in = Demo.class.getResourceAsStream("/a.txt");
	}
	
	public void fun2() {
		InputStream in = Demo.class.getResourceAsStream("a.txt");
	}
}

其中fun1()方法获取资源时以“/”开头,那么相对的是当前类路径,即/hello/WEB-INF/classes/a.txt文件;

其中fun2()方法获取资源时没有以“/”开头,那么相对当前Demo.class所在路径,因为Demo类在com.tyschool包下,所以资源路径为:/hello/WEB-INF/classes/com/tyschool/a.txt。

七、ClassLoader获取资源

ClassLoader获取资源也必须是相对路径,可以“/”开头,也可以不使用“/”开头。但无论是否以“/”开头,资源都是相对当前类路径。

代码语言:javascript
复制
public class Demo {
	public void fun1() {
		InputStream in = 							Demo.class.getClassLoader().getResourceAsStream("/a.txt");
	}
	
	public void fun2() {
		InputStream in = 	Demo.class.getClassLoader().getResourceAsStream("a.txt");
	}
}

fun1()和fun2()方法的资源都是相对类路径,即classes目录,即/hello/WEB-INF/classes/a.txt

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、与路径相关的操作
  • 二、客户端路径
  • 三、服务器端路径
  • 四、url-pattern路径
  • 五、ServletContext获取资源
  • 六、Class获取资源
  • 七、ClassLoader获取资源
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档