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>