Sevlet et fichier XMl

Fermé
hichh Messages postés 2 Date d'inscription vendredi 6 juin 2014 Statut Membre Dernière intervention 8 juin 2014 - 6 juin 2014 à 19:47
hichh Messages postés 2 Date d'inscription vendredi 6 juin 2014 Statut Membre Dernière intervention 8 juin 2014 - 8 juin 2014 à 13:06
Bonjour
J'ai un petit soucis, voila j'ai voulu realiser une authentification(Login, pass) avec un fichier XML. voila le code:
Code :
public boolean trouverutilisateur(Personne p) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            File file = new File("personnes.xml");
            if (file.exists()) {
                Document doc = db.parse(file);
                System.out.println("Dans la liste");
                Element docEle = doc.getDocumentElement();
 
                // Print root element of the document
 
 
                NodeList studentList = docEle.getElementsByTagName("personne");
 
                // Print total student elements in document
                Boolean rech=false;
                if (studentList != null && studentList.getLength() > 0) {
 
                    for (int i = 0; i < studentList.getLength(); i++) {
 
                        Node node = studentList.item(i);
 
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                           Element e = (Element) node;
                            NodeList nodeList = e.getElementsByTagName("nom");
                           Personne pp=new Personne();
                           pp.setLogin(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                           nodeList = e.getElementsByTagName("pass");
                           pp.setPassfromxml(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                           if (pp.getLogin().equals(p.getLogin()) && pp.getPass().equals(p.getPass())){
 
                        	   return true;
                           }
 
 
 
                        }
                    }
                } else {
                    System.exit(1);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return false;
    }


J'ai testé avec void main:
public static void main(String[] args){
		Personne p=new Personne("francois","francois");
		LoginDao d=new LoginDao();
		if (d.trouverutilisateur(p)){
			System.out.println("je suis la");
		}
		else{
			System.out.println("je ne suis pas la!!!");
		}
	}


'ai realisé une servlet:
Code :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		LoginDao dao=new LoginDao();
		Personne p=new Personne();
		p.setLogin(request.getParameter("[/download/telecharger-34086600-login login]").toString());p.setPass(request.getParameter("pass").toString());
		if (dao.trouverutilisateur(p)){
			HttpSession session = request.getSession() ;
			session.putValue("personne",p);
			request.setAttribute("p",p);
	        request.getRequestDispatcher("/llll").forward(request, response);
		}
		else{
 
			HttpSession session = request.getSession() ;
			session.putValue("personne",p);
			request.setAttribute("p",p);
	        request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
 
	}


Coté jsp voila le code:
Code :

<form class="form-horizontal mt10" action="login" method="post">
          <div class=form-group>
            <div class=col-lg-12>
              <%
            @SuppressWarnings("unchecked") 
            Personne per = (Personne)request.getAttribute("p");
              %> 
              <input name=login id=email class="form-control left-icon" 
              <%
 
              if (per != null) {
            	  %> value="<%= per.login %> -- <%= per.getPass() %>"
              <%}%> placeholder="Your email ...">
              <i class="ec-user s16 left-input-icon"></i></div>
          </div>
          <div class=form-group>
            <div class=col-lg-12>
 
              <input type=password name=pass id=password class="form-control left-icon" placeholder="Your password">
            </div>
          <div class=form-group>
 
            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-4">
              <!-- col-lg-12 start here --><br>
              <button class="btn btn-success pull-right" type=submit>Login</button>
            </div>
            <!-- col-lg-12 end here -->
          </div>
        </form>


la personne est redirigé vers la page d'authentification, le login et mot de passe sont aussi affiché dans le champs login.
Si vous avez une solution a ce probleme
A voir également:

1 réponse

hichh Messages postés 2 Date d'inscription vendredi 6 juin 2014 Statut Membre Dernière intervention 8 juin 2014
8 juin 2014 à 13:06
Bonjour,
J'ai essayé d'afficher tout le contenu du fichier XML.
public ArrayList<Personne> trouverutilisateur(Personne p) {
        ArrayList<Personne> pers=new ArrayList<Personne>();
		try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            
            File file = new File("personnes.xml");
            if (file.exists()) {
                Document doc = db.parse(file);
                Element docEle = doc.getDocumentElement();
 
                // Print root element of the document
               
 
                NodeList studentList = docEle.getElementsByTagName("personne");
 
                // Print total student elements in document
                Boolean rech=false;
                if (studentList != null && studentList.getLength() > 0) {
                	
                    for (int i = 0; i < studentList.getLength(); i++) {
                    	
                        Node node = studentList.item(i);
 
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                           Element e = (Element) node;
                            NodeList nodeList = e.getElementsByTagName("nom");
                           Personne pp=new Personne();
                           pp.setLogin(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                           nodeList = e.getElementsByTagName("pass");
                           pp.setPassfromxml(nodeList.item(0).getChildNodes().item(0).getNodeValue());
                           pers.add(pp);
                          
 
                            
                            
                        }
                    }
                } else {
                    System.exit(1);
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return pers;
    }


Mais rien ne s'affiche, Qlq un à une solution à ce probleme. Je pense qu'avec glassfish (ou n'importe quel serveur web) le fichier xml est inaccessible avec la methode que j'ai mis. Merci de me venir une aide, c'est tres urgent.
0