您的位置:首页 > 其它

【ServletConfig、ServletContext】使用ServletContext、类加载器读取资源文件

2014-04-28 09:50 501 查看
1、在实际开发中有一些参数不适合在servlet中写死,这时候就可以在servlet中的init中配置。

<init-param>
<param-name>username</param-name>
<param-value>jingtian</param-value>
</init-param>


2、在dopost/doget方法中调用this.getServletConfig()分析

 查看GenericServlet代码

private transient ServletConfig config;

@Override
public ServletConfig getServletConfig() {
return config;
}

@Override
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}


使用ServletConfig得到参数

this.getServletConfig().getInitParameter("username");


得到所有参数:
Enumeration e= this.getInitParameterNames();
while(e.hasMoreElements()){
String a=(String) e.nextElement();
String v=this.getServletConfig().getInitParameter(a);
System.out.println(a+"="+v);
}


3、web容器在启动的时候,他会为每一个web应用程序创建一个ServletContext对象,它代表当前的web应用。因此ServletContext对象也被称为context域对象。

<context-param>
<param-name>data</param-name>
<param-value>xxxx</param-value>
</context-param>


4、ServletContext的两种得到方式:

ServletContext context=this.getServletConfig().getServletContext();

context=this.getServletContext();


5、ServletContext实现请求转发

context.getRequestDispatcher("/index.html").forward(request, response);

6、ServletContext实现多个servlet之间通讯

setAttribute()
getAttribute()

7、使用ServletContext读取资源文件

package com.xiaozhi.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xiaozhi.dao.UserDao;

public class ServletDemo extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
UserDao dao=new UserDao();
dao.test2();

}

//下面讲解的是Servlet读取资源文件
public void test3() throws FileNotFoundException, IOException
{
//FileINputStream使用相对路径读取classes下的db.properties
FileInputStream fileInputStream=new FileInputStream("../webapps/HelloJavaWeb/WEB-INF/classes/db.properties");
Properties properties=new Properties();
properties.load(fileInputStream);

System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));
}

public void test2() throws FileNotFoundException, IOException
{
//FileINputStream使用绝对路径读取classes下的db.properties
String path=this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
FileInputStream fileInputStream=new FileInputStream(path);
Properties properties=new Properties();
properties.load(fileInputStream);

System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));
}

public void test1() throws IOException
{
//使用ServletContext读取classes下的db.properties
InputStream inputStream=this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties=new Properties();
properties.load(inputStream);

System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

}

}
8、使用类加载器读取资源文件

package com.xiaozhi.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class UserDao
{

private static Properties properties=new Properties();
static{
//类加载器以类的方式读取资源文件,资源文件会永驻内存,如果资源文件发生改变,服务器不重启,则不能实时更新
//UserDao.class也是永驻内存
InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties");
try
{
properties.load(inputStream);
} catch (IOException e)
{
throw new ExceptionInInitializerError(e);
}

}

//资源资源发生改变,实时更新
public void test2() throws IOException
{
//使用类加载器去读资源文件的位置
String path=UserDao.class.getClassLoader().getResource("db.properties").getPath();
FileInputStream inputStream=new FileInputStream(path);
Properties properties=new Properties();
properties.load(inputStream);

System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));
}

//使用类加载器读取资源文件
public void test1() throws IOException
{
//classe下的所有类都使用一个加载器,这个类加载器可以直接加载classes下的文件
InputStream inputStream=UserDao.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties=new Properties();
properties.load(inputStream);

System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("password"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: