java:Servlet之路径

Servlet之路径

概述

servllet中与路径相关的操作有:

超链接、表单、重定向、转发、包含、配置url-pattern、ServletContext获取资源、Class获取资源ClassLoader获取资源。

本文通过以上方法的使用,分别区分不同情况下使用路径名的效果。

1、客户端路径(主要有 超链接、表单、重定向)

2、服务器路径

3、转发

4、url-pattern 路径

5、ServletContext获取资源

6、Class获取资源

7、ClassLoader获取资源





1、客户端路径(主要有 超链接、表单、重定向)

客户端路径分为三种方式:绝对路径、以“/”开头的相对路径、不以“/”开头的相对路径;

1)超链接和表单

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

绝对路径:<a href="http://localhost:8080/hello2/index.html">链接1</a>
客户端路径:<a href="/hello2/pages/index.html">链接2</a>
相对路径:<a href="index.html">链接3</a>
<hr/>
绝对路径:
<form action="http://localhost:8080/hello2/index.html">
<input type="submit" value="表单1"/>
</form>
客户端路径:
<form action="/hello2/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/hello2/pages/index.html;

2)重定向

例如:http://localhost:8080/hello/index.html

范例一:以“/”开头的相对路径

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.sendRedirect("/hello/index.html");
        //此种输入方式直接最终路径为http://localhost:8080/hello/index.html
        //若response.sendRedirect("/index.html");则为如下
        //http://localhost:8080/index.html
	}
}

范例二:不以“/”开头的相对路径

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		response.sendRedirect("index.html");
        //此种输入方式直接最终路径为http://localhost:8080/hello/index.html
	}
}

注意:

我们在进行网络编程的时候,建议使用带“/"的路径,后接应用名,再是访问路径,方便我们直观区分应用。

如下路径:

<form action="/hello/index.html">
</form>
<a href="/hello/index.html">链接</a>

其中**/hello**是当前应用名称;那么如果将来修改了应用名称,那么页面中的所有路径也要修改,这是否会很麻烦?

目前如果我们在java中的路径使用,可以通过 request.getContextPath() 来获取应用名称,再通过字符串拼接得到路径。

response.sendRedirect(request.getContextPath() + "/index.html");





2、服务器路径

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

​ 以“/”开头;

​ 不以“/”开头;

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

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

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





3、转发

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

public class AServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		request.getRequestDispatcher("/BServlet").forward(request, response);
	}
}

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





4、< url-pattern>路径

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





5、ServletContext获取资源

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

例如:

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

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
	}
}

path1和path2为相同的结果





6、Class获取资源

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

例如:
Demo的路径为 /hello/WEB-INF/classes/com/xyd/Demo.class

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.xyd包下,所以资源路径为:/hello/WEB-INF/classes/com/xyd/a.txt





7、ClassLoader获取资源

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

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
}

以上fun1()和fun2()方法的资源路径相同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值