gpt4 book ai didi

java - Java中通过web.xml加载项目时如何调用属性文件?

转载 作者:行者123 更新时间:2023-12-02 10:38:13 25 4
gpt4 key购买 nike

我已经制作了一个属性文件并仅从该文件加载我的数据库连接。我正在做的是使用 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>

最佳答案

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 - Java中通过web.xml加载项目时如何调用属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53117095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com