【问题标题】:How to call property file when project is loaded through web.xml in Java?Java中通过web.xml加载项目时如何调用属性文件?
【发布时间】:2018-11-02 10:46:48
【问题描述】:

我创建了一个属性文件并仅从该文件加载我的数据库连接。我正在做的是使用 Servlet 上下文,我正在获取资源 我在我的登录页面 (login.jsp) 中编写的这段代码就像这样

    <%ServletContext servletContext = getServletContext();
InputStream input = getServletContext().getResourceAsStream("/WEB-INF/db.properties");
 if (input != null) {
     InputStreamReader isr = new InputStreamReader(input);
     BufferedReader reader = new BufferedReader(isr);
     PrintWriter writer = response.getWriter();
     String text;
     while ((text = reader.readLine()) != null) {
/*      System.out.println(text + "\n"); */
         servletContext.setAttribute(text.split("=")[0], text.split("=")[1]);

     }
 }
%>

因此,每当用户登录时,它都会从我的属性文件中获取值,但我想要的是通过 web.xml 加载我的属性文件,因此每当项目加载或服务器启动时,它都会从该属性文件加载连接只在用户登录时调用属性文件一次。

所以这是我的 Java 连接类,我从属性文件中分配值:

ServletContext servletContext = getServletContext();
        String driverDB = servletContext.getAttribute("driver").toString();
        String conn_urlDB = servletContext.getAttribute("conn_url").toString();
        String userNameDB = servletContext.getAttribute("userName").toString();
        String passwordDB = servletContext.getAttribute("password").toString();
DBConnection.getDataBaseProperty(driverDB, conn_urlDB, userNameDB, passwordDB);

下面的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>TouchPoint</display-name>

  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.touchpoint.controller.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LogoutServlet</servlet-name>
    <servlet-class>com.touchpoint.controller.LogoutServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LogoutServlet</servlet-name>
    <url-pattern>/LogoutServlet</url-pattern>
  </servlet-mapping>
</web-app>

【问题讨论】:

    标签: java servlets web.xml


    【解决方案1】:

    ServletContextListener 是你的答案,

    您可以查看这篇博文以获得更好的理解 https://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example

    将您的配置字符串放入 web.xml 文件中,然后通过 servletContextListener 在服务器启动时获得通知

    这是该博客文章的摘录:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>ServletListenerExample</display-name>
    
      <context-param>
        <param-name>DBUSER</param-name>
        <param-value>pankaj</param-value>
      </context-param>
      <context-param>
        <param-name>DBPWD</param-name>
        <param-value>password</param-value>
      </context-param>
      <context-param>
        <param-name>DBURL</param-name>
        <param-value>jdbc:mysql://localhost/mysql_db</param-value>
      </context-param>
    
      <listener>
        <listener-class>com.journaldev.listener.AppContextListener</listener-class>
      </listener>
    

    你需要创建一个新的类来监听服务器启动的事件:

    public class AppContextListener implements ServletContextListener {
    
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            ServletContext ctx = servletContextEvent.getServletContext();
    
            String url = ctx.getInitParameter("DBURL");
            String u = ctx.getInitParameter("DBUSER");
            String p = ctx.getInitParameter("DBPWD");
    }}
    

    【讨论】:

    • 我需要对我的 java 类进行任何更改吗?
    • 只需要新建一个实现ServletContextListener的类,开始监听服务器启动事件即可。我更新了答案,告诉你怎么做。请不要忘记在您的 web.xml 中注册您的监听器
    • com.journaldev.listener.AppContextListener
    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2014-09-18
    • 2019-09-29
    • 2014-12-15
    • 2015-08-09
    • 2014-04-10
    • 2012-02-08
    相关资源
    最近更新 更多