public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream in = context.getResourceAsStream("/database.properties");
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
ServletContext context = this.getServletContext();
String filePath = context.getRealPath("/database.properties");
FileInputStream fis = new FileInputStream(filePath);
Properties prop = new Properties();
prop.load(fis);
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
ServletContext context = this.getServletContext();
String filePath = context.getRealPath("/WEB-INF/web.xml");
System.out.println(filePath);
if(filePath == null) {
System.out.println("所找文件不存在!");
}
String fileName = filePath.substring(filePath.lastIndexOf("\\"));
System.out.println("文件为:"+fileName);
ServletContext context = this.getServletContext();
URL fileUrl = context.getResource("/database.properties");
InputStream in = fileUrl.openStream();
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
ServletContext context = this.getServletContext();
InputStream in = context.getResourceAsStream("/WEB-INF/classes/database.properties");
Properties prop = new Properties();
prop.load(in);
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
System.out.println(url);
System.out.println(username);
System.out.println(password);
public class Student {
public void getStudent() throws IOException {
ClassLoader loader = Student.class.getClassLoader();
InputStream in = loader.getResourceAsStream("student.properties");
Properties prop = new Properties();
prop.load(in);
String studentName = prop.getProperty("name");
String studentAge = prop.getProperty("age");
System.out.println(studentName+":"+studentAge);
}
}
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Student student = new Student();
student.getStudent();
}
}
public class Student {
public void getStudent() throws IOException {
ClassLoader loader = Student.class.getClassLoader();
URL fileUrl = loader.getResource("student.properties");
String filePath = fileUrl.getPath();
FileInputStream fis = new FileInputStream(filePath);
Properties prop = new Properties();
prop.load(fis);
String studentName = prop.getProperty("name");
String studentAge = prop.getProperty("age");
System.out.println(studentName+":"+studentAge);
}
}
public class ResourceUtils {
public static void main(String[] args) throws IOException {
getResource();
}
@Test
public static void getResource() throws IOException {
ClassLoader loader = ResourceUtils.class.getClassLoader();
URL url = loader.getResource("student.properties");
String path = url.getPath();
System.out.println(path);
}
}
public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ResourceUtils.getResource();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有