manish thakur
manish thakur

Reputation: 820

How to call property file when project is loaded through web.xml in Java?

I have made a property file and loading my database connection from that file only. What I am doing is using Servlet context, I am getting the resource and this code I have written in my login page (login.jsp) like this

    <%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]);

     }
 }
%>

So it is taking the values from my property file whenever the user is logging in, but what I want is to load my property file through web.xml so whenever the projects load or server starts it loads the connection from that property file only once not whenever the user login it calls the property file.

So here is my Java connection class where I am assigning the values from property file:

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 below

    <?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>

Upvotes: 0

Views: 877

Answers (1)

Zied Hamdi
Zied Hamdi

Reputation: 2662

ServletContextListener is tha answer for you,

You can check this blog post for a better understanding https://www.journaldev.com/1945/servletcontextlistener-servlet-listener-example

put your config strings in the web.xml file then get notified on server startup through the servletContextListener

here's an extract from that blog post:

<?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>

You need to create a new class that will listen to the event of server startup:

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");
}}

Upvotes: 1

Related Questions