简体   繁体   English

如何在Eclipse的web.xml中添加上下文参数?

[英]How to add Context Parameters in web.xml in Eclipse?

I am using Eclipse Mars(Version: Mars Release (4.5.0)). 我正在使用Eclipse Mars(版本:Mars Release(4.5.0))。

Basically i am developing a small Web Application that has login form and a link that goes to registering new user. 基本上,我正在开发一个小型Web应用程序,该应用程序具有登录表单和一个用于注册新用户的链接。

Here i am supposed to interact with my database couple of times for verifying login details and for registering new user details. 在这里,我应该与数据库进行几次交互,以验证登录详细信息并注册新的用户详细信息。

So i want to add database details(like driver,url,username,password) in context parameters in web.xml to avoid HardCoding ? 所以我想在web.xml的上下文参数中添加数据库详细信息(如驱动程序,URL,用户名,密码),以避免HardCoding? How am i supposed to do that?Thank you. 我该怎么办?谢谢。

web.xml is not elegant way to keep that kind of data as login/password/driver etc. web.xml不是将此类数据保留为登录名/密码/驱动程序等的优雅方法。

There are few approaches that you could implement for that kind data. 可以针对此类数据实施的方法很少。

JNDI approach JNDI方法
Configuration depends on what web container/server are you using right now. 配置取决于您当前使用的Web容器/服务器。 As you mentioned this is simple web application propably you are using Tomcat so here please refer to this for more details and example: Tomcat and JNDI 正如您提到的,这可能是一个简单的Web应用程序,可能是您正在使用Tomcat,因此,请在此处参考更多详细信息和示例: Tomcat和JNDI

Configuration stored in property file approach 配置存储在属性文件中
You need to store these data in your property file called for example db.properties 您需要将这些数据存储在名为db.properties属性文件中

#Properties structure
db.url=your_db_url
db.driver=your_db_driver
db.login=your_db_login
db.password=your_db_password

This file should be placed in your src/resources catalog. 该文件应该放在您的src/resources目录中。 So after making webapp package it should be in WEB-INF/classes path of your web application 因此,制作完webapp软件包后,它应该位于WEB-INF/classes应用程序的WEB-INF/classes路径中

db.properties should be load somehow in your code for example: db.properties应该以某种方式加载到您的代码中,例如:

Properties prop = new Properties();
InputStream input = null;

try {

    input = new FileInputStream("db.properties");

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    String url = prop.getProperty("db.url");
    String driver = prop.getProperty("db.driver");
    String login = prop.getProperty("db.login");
    String password = prop.getProperty("db.password");

    //Your code to connect to db
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

More approaches can be defined but everything depends on what framework are you actualy working. 可以定义更多方法,但是所有方法都取决于您实际使用的框架。

Handle this maybe like a guideline not step-by-step. 可能不按部就班地处理这条准则。 It is up to you which way do you use. 取决于您使用哪种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM