获取应用初始化参数
1.配置servlet的初始化参数
<servlet>
<servlet-name>Aservlet</servlet-name>
<servlet-class>test_web.Servlet1</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
</servlet>
2.获取servlet的初始化参数
String v=this.getInitParameter("name");
System.out.println(v);
3.配置全局初始化参数
<context-param>
<param-name>paramname1</param-name>
<param-value>paramvalue1</param-value>
</context-param>
<context-param>
<param-name>paramname2</param-name>
<param-value>paramvalue2</param-value>
</context-param>
4.获取全局初始化参数
/**
* 获取配置文件参数
* 1.获取上下文对象
* 2.直接通过参数名称获取参数值
* 3.通过枚举获取全部参数名然后获取参数值
*/
//获取上下文对象
ServletContext context = this.getServletContext();
//获取参数名称
Enumeration names = context.getInitParameterNames();
while (names.hasMoreElements()) {
String str = (String) names.nextElement();
String param=context.getInitParameter(str);
System.out.println(str+" and "+param);
}
获取相关资源
1.获取项目路径
ServletContext context = this.getServletContext();
//获取项目路径
String Path = context.getContextPath();
System.out.println(Path);
///test_web
2.获取项目下某个文件的路径
//获取项目中的某个文件路径
String realPath = context.getRealPath("a.txt");
System.out.println(realPath);
//F:\test\.metadata\.me_tcat7\webapps\test_web\a.txt
String webinfrealPath = context.getRealPath("/webroot/meta-inf/manifest.mf");
System.out.println(webinfrealPath);
//F:\test\.metadata\.me_tcat7\webapps\test_web\webroot\meta-inf\manifest.mf
3.获取资源流
InputStream in = context.getResourceAsStream("a.txt");
4.获取某文件夹下的所有目录
Set set = context.getResourcePaths("/WEB-INF");
System.out.println(set);
//[/WEB-INF/lib/, /WEB-INF/a.txt, /WEB-INF/classes/, /WEB-INF/web.xml]
注意这里的路径要区分大小写并且以‘/’开头
5.获取类路径
aa.txt与当前类文件在同一个位置
bb.txt在src下
//这是获取在src下的文件 加‘/’
InputStream inputStream = this.getClass().getResourceAsStream("/bb.txt");
int len =inputStream.available();
byte[] buff=new byte[len];
inputStream.read(buff);
System.out.println(new String(buff));
//这是获取在当前类路径下的文件 不加‘/’
InputStream inputStream = this.getClass().getResourceAsStream("aa.txt");
int len =inputStream.available();
byte[] buff=new byte[len];
inputStream.read(buff);
System.out.println(new String(buff));
//获取src下文件的另一种方法
InputStream inputStream2 = this.getClass().getClassLoader().getResourceAsStream("bb.txt");
int len1 =inputStream2.available();
byte[] buff1=new byte[len1];
inputStream2.read(buff1);
System.out.println(new String(buff1));
6.获取java项目下的文件路径
aa.txt在bin目录下
//获取src下的文件
File file =new File("src/test/Demo02.java");
System.out.println(file.getAbsolutePath());
System.out.println(file.exists());
//获取当前类所在路径的字节码文件
URL u=this.getClass().getResource("Demo02.class");
System.out.println(u.getPath());
File f= new File(u.getPath());
System.out.println(f.exists());
//获取bin目录下的文件
URL uu = this.getClass().getClassLoader().getResource("aa.txt");
System.out.println(uu.toString());
f=new File(uu.getFile());
System.out.println(f.exists());
System.out.println(f.isFile());
7.浏览器客户端路径
<a href="http://127.0.0.1:8080/test_web/servlet1"> 链接1</a>绝对路径
<a href="/test_web/servlet1"> 链接2</a>浏览器路径
<a href="servlet1">链接3</a>相对路径 ---相对于当前文件的路径
<%
//response.sendRedirect("/test_web/servlet1");//相对当前主机
response.sendRedirect("count.jsp");//相对当前文件
%>
建议使用带‘/’的路径但是则会出现应用名称改变需要改代码问题可以使用
response.sendRedirect(request.getContextPath() + "/servlet1");
对于表单或者超链接里的可以在文档开头加上
`
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
然后使用el表达式来拼接路径`
8.服务器端路径
服务器端路径必须是相对路径,不能是绝对路径。但相对路径有两种形式:
* 以“/”开头;
* 不以“/”开头;
其中请求转发、请求包含都是服务器端路径
服务器端路径与客户端路径的区别是:
* 客户端路径以“/”开头:相对当前主机;
* 服务器端路径以“/”开头:相对当前应用;
9.<url-pattern>路径
<url-pattern>
必须使用“/”开头,并且相对的是当前应用。