Champ bean + récupération d'un fichier
Bonjour,
j'ai la page index.jsp suivante:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="db" class="com.lamelcolor.Database" scope="session"> </jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<LINK rel="stylesheet" type="text/css" href="common/style.css">
</head>
<body>
<div id="menu"><jsp:include page="common/menu.jsp"></jsp:include></div>
<div id="content">
<%-- <jsp:include page="home.jsp"></jsp:include>--%>
<jsp:getProperty name="db" property="statut"></jsp:getProperty>
<jsp:getProperty name="db" property="err"></jsp:getProperty>
</div>
<div id="footer">
A developpement of Lamelcolor SA and Logico SA
</div>
</body>
</html> |
et le bean Database suivant:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package com.lamelcolor;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
public class Database {
Connection conn = null;
private String err="";
private boolean statut = false;
/** Creates a new instance of Database */
public Database() {
System.out.println("connection");
this.initConnection();
}
/** Créer une connexion à partir des propriétés spécifiées dans le fichier database.properties **/
public void initConnection() {
// Load the JDBC-ODBC bridge driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch( ClassNotFoundException ee) {
setErr("Impossible de charger le driver JdbcOdbc");
}
Properties props=new Properties();
try {
props.load(new FileInputStream("database.properties"));
String drivers=props.getProperty("jdbc.drivers");
String url=props.getProperty("jdbc.url");
String username=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
conn = DriverManager.getConnection(url,username,password);
props = null;
if(conn!=null) {
setStatut(true);
}
} catch (FileNotFoundException ex) {
setErr("Impossible de trouver le fichier de properties");
} catch (IOException ioex) {
setErr("Impossible de lire le fichier des propriétés");
} catch (SQLException sqlex) {
setErr("Impossible d'initialiser la connexion à la base de données (utilisateur ou mot de passe erroné)");
}
}
public Connection getConnection() {
return this.getConnection();
}
public void close() {
try {
this.conn.close();
} catch (SQLException sqlex) {
}
}
public boolean isStatut() {
return statut;
}
public void setStatut(boolean statut) {
this.statut = statut;
}
public String getErr() {
return err;
}
public void setErr(String err) {
this.err = err;
}
} |
Le bean Database ne trouve pas mon fichier database.properties malgré qu'il soit dans le répertoire web à la racine?
J'aimerais récupérer la valeur de statut directement à l'intérieur de <% %> afin de générer du code html? Est-ce qu'il y a une autre façon? -> le but étant d'afficher une icône si la connexion a réussie et un autre icòne si celle-ci n'est pas active...
Merci pour votre aide