properties

Accessing ServletContext outside of a servlet

我的梦境 提交于 2019-12-30 05:19:05
问题 I wonder if anyone can advise on a Java webapp question? I have a set standard Java class which has methods to set and get values of a property file. These methods are used system wide and are called by servlets and non-servlet class methods alike. But what I would like to do is to use a ServletContextListener to set the paths to the property file globally rather than hard code them or store them in the database. A ServletContextListener can be used to set 'global variables' for the servlets

java 读取properties文件

删除回忆录丶 提交于 2019-12-30 05:11:48
一、文件位置 properties文件内容: driver=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/qqonline user=root password= 二、代码 需要注意的是java工程的路径为src\config.properties,java web的路径为WEB-INF\classes\config.properties 。部署的时候tomcat会将config.properties编译到WEB-INF\classes\config.properties目录下。 所以如果在java web工程下仍然使用java工程的路径会无法获取到参数。 public class DBUtil {   private static String driver = ""; private static String url = ""; private static String user = ""; private static String password = ""; private static boolean flag = false; /** * 加载JDBC连接数据库参数 */ public void getParm(){ Properties p = new Properties(); try {

java读取配置文件properties

霸气de小男生 提交于 2019-12-30 05:06:52
此文是从百度上获取: 各位大虾好:最近在学习Java文件,现在项目里面有很多的硬编码的地方。我想将他们都放在Properties文件里面。但是我放上之后总是报找不到路径的错误,我把它放到了src下了。在编译的时候web-inf/classes下面也有。但是总是找不到。是不是我建的时候出问题了。我再建property文件的时候。找不到这种文件。就找了一个txt文件然后重命名了。这样可以吗? 下面是代码: init.properties文件: datasource.driverClassName=com.mysql.jdbc.Driver datasource.url=jdbc:mysql://localhost:3306/bbscs8?useUnicode=true&characterEncoding=UTF-8 datasource.username=root datasource.password=sdie?!3406 这是根目录下的测试文档: TestProperties import java.util.*; import java.io.*; import java.util.Properties; public class TestProperties { public static void main(String args[]) throws Exception {

C# readonly vs Get

青春壹個敷衍的年華 提交于 2019-12-30 03:50:11
问题 Are there any differences between the readonly modifier and get-only properties? Example: public class GetOnly { public string MyProp { get; } } public class ReadOnly { public readonly string MyProp; } Bonus: is there a way to make an interface that works with both? (to use with generics) public interface ISomething { public string MyProp { get; } } public class GetOnly : ISomething { public string MyProp { get; } } public class ReadOnly : ISomething // Cannot implement { public readonly

Prevent visual studio from limiting the setter method to internal

不羁的心 提交于 2019-12-30 02:47:08
问题 Well, I use visual studio 2015 CE, update 2. One productivity hack I usually do is that I create empty model classes like: public class PersonModel { } and then use them in a select expression like: db.People.Where(p => someCondition) .Select(p => new PersonModel { Id = p.Id, Name = p.Name, //set other properties }).ToList(); Then I go to the yet non-existing properties Id and Name , ... and press Control+. to ask visual studio to generate property Id for me. All great, but it will create:

java 读取src下的配置文件

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 02:22:39
很多时候,我们都将配置文件放在eclipse的src目录下,这个位置,相当于,当导出可执行jar包后,配置文件放在和jar同级的目录中,比如jar包放在/opt目录下,则配置文件放在/opt下,则jar包就可以读取配置文件中的内容。此时,java代码中可以通过 String path=CommonOperation.class.getResource("/").getPath(); FileInputStream fin = new FileInputStream(path+"Config.properties"); 来读取配置文件。 但要注意,用这种方法在eclipse下调试程序的时候,会发现使用setProperty(String key ,String value)无法修改配置文件的内容,原因是 eclipse在编译文件时,已经把配置文件复制到工程的bin目录下了,修改其实已经保存在bin目录下的那个配置文件里面了。 java读取配置文件内容的代码如下: String path=CommonOperation.class.getResource("/").getPath(); InputStream fis = new FileInputStream(path+"Config.properties"); Properties prop = new Properties();

javaweb 读取properties配置文件参数

雨燕双飞 提交于 2019-12-30 02:20:16
场景1:在servlet中读取properties配置文件参数 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通过getServletContext来得到流数据 InputStream properties = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties props = new Properties(); props.load(properties); String user = props.getProperty("username"); String pass = props.getProperty("password"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //通过getServletContext来得到文件绝对路径,再用平时读文件的方式得到流数据

读取配置文件参数

时间秒杀一切 提交于 2019-12-30 02:16:49
配置文件代码: java代码: try{ Properties prop = new Properties(); //读取属性文件horizon.properties String params = request.getSession().getServletContext().getRealPath("/")+"WEB-INF\\workflow-conf\\properties\\horizon.properties"; InputStream in = new BufferedInputStream (new FileInputStream(params)); prop.load(in); ///加载属性列表 String basicVacationDay = prop.getProperty("basicVacationDay"); String tallestVacationDay = prop.getProperty("tallestVacationDay"); String base = prop.getProperty("base"); in.close(); }catch(Exception e){ e.printStackTrace(); } 来源: https://www.cnblogs.com/henuyuxiang/p/6500712.html

Load java.util.logging.config.file for default initialization

安稳与你 提交于 2019-12-30 00:55:06
问题 I'm trying to load a custom log.properties file when my application is started. My properties file is in the same package as my main class, so I assumed that the -Djava.util.logging.config.file=log.properties command line parameter should get the properties file loaded. But the properties are only loaded when i specify a full absolute path to the properties file. Any suggestions how to use a relative path? 回答1: Java logging doesn't search your whole hard disk for a file; there are very simple

Why do we use .NET properties instead of plain old get/set functions?

白昼怎懂夜的黑 提交于 2019-12-29 20:57:12
问题 I understand the many benefits of providing an interface to access the members of a class indirectly. My question is: isn't that already something you can accomplish in just about any OO language using (something along the lines of) public int NormalClass::getQuality() { return this->quality; } and protected void NormalClass::setQuality(int q) { this->quality = q; } ? What additional benefits do .NET properties offer, beyond sheer aesthetics? I will accept "readability" if you can make a