1.配置文件
没有关系的用properties,有关系的用xml。
Servlet读取配置文件,当填写web工程的目录的时候不能天真的填写eclipse里的目录,我们要填写的是真实的目录比如"/WEB-INF/classes"。
以下是servlet读取web中properties的模板代码
package hyl;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.xml.internal.fastinfoset.sax.Properties;
public class Servlet01 extends HttpServlet {
//读取资源文件
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes");//把资源当作流返回的方法,第一个斜杠就代表web工程,只要找到对应的对应位置的对应文件
//换句话说eclipse里的web目录是虚假目录,与实际不符
Properties props = new Properties();//map
props.load(in);
String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password");
//这就是读取properties资源文件的示例代码
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
//有点小状况但是必须要记住
整体步骤是:1.拿到servletcontext拿到流,
2.创建properties的对象调用load方法
3.调用get方法分别得到各个数据,赋予String变量
需要注意的是,我们还有一种取资源的方式,就是FileInputStream这种传统的方法,但是我们在目录里的是java的工程和访问dom工程一样我们在后面的路径里访问的是相对路径,这样会带来诸多麻烦不宜使用,还是得用ServletSontext来读取资源文件,如果非要使用的话那么就得得到他的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes");
FileInputStream in1 = new FileInputStream(path);//但是这样也用到了ServletContext,但是有一个好处是能够得到资源名称
以下代码:String filename = path.substring(path.lastIndexOf("//"));
tips:真的“/”,在编程里面是“//”。
没有关系的用properties,有关系的用xml。
Servlet读取配置文件,当填写web工程的目录的时候不能天真的填写eclipse里的目录,我们要填写的是真实的目录比如"/WEB-INF/classes"。
以下是servlet读取web中properties的模板代码
package hyl;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.xml.internal.fastinfoset.sax.Properties;
public class Servlet01 extends HttpServlet {
//读取资源文件
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes");//把资源当作流返回的方法,第一个斜杠就代表web工程,只要找到对应的对应位置的对应文件
//换句话说eclipse里的web目录是虚假目录,与实际不符
Properties props = new Properties();//map
props.load(in);
String url = props.getProperty("url");
String username = props.getProperty("username");
String password = props.getProperty("password");
//这就是读取properties资源文件的示例代码
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
//有点小状况但是必须要记住
整体步骤是:1.拿到servletcontext拿到流,
2.创建properties的对象调用load方法
3.调用get方法分别得到各个数据,赋予String变量
需要注意的是,我们还有一种取资源的方式,就是FileInputStream这种传统的方法,但是我们在目录里的是java的工程和访问dom工程一样我们在后面的路径里访问的是相对路径,这样会带来诸多麻烦不宜使用,还是得用ServletSontext来读取资源文件,如果非要使用的话那么就得得到他的绝对路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes");
FileInputStream in1 = new FileInputStream(path);//但是这样也用到了ServletContext,但是有一个好处是能够得到资源名称
以下代码:String filename = path.substring(path.lastIndexOf("//"));
tips:真的“/”,在编程里面是“//”。