PDA

View Full Version : Problemi Tomcat e accesso a file xml


boysna
02-06-2006, 15:28
Ciao a tutti raga.

Ho un problema. Devo creare un sito e devo controllare se la password e la login inserite dall'utente sono corrette. Per questo c'è un file xml che raccoglie le login. Ecco l'xml e il dtd:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE root SYSTEM "account.dtd">

<root>
<account>
<tipo>amministratore</tipo>
<login>leggero</login>
<password>05071983</password>
</account>

<account>
<tipo>amministratore</tipo>
<login>FireFoxII(</login></login>
<password>ilfenomeno</password>
</account>

<account>
<tipo>utente</tipo>
<login>mariorossi</login>
<password>forzainter</password>
</account>

<account>
<tipo>utente</tipo>
<login>giovannibianchi</login>
<password>winter</password>
</account>

<account>
<tipo>utente</tipo>
<login>riccardoverdi</login>
<password>interforever</password>
</account>

<account>
<tipo>utente</tipo>
<login>marcogialli</login>
<password>intercapolista</password>
</account>

<account>
<tipo>utente</tipo>
<login>misterbrown</login>
<password>interprimasquadradimilano</password>
</account>

</root>



<!ELEMENT root (account)>

<!ELEMENT account (tipo, login, password)>

<!ELEMENT tipo (amministratore|utente)>
<!ELEMENT login (#PCDATA)>
<!ELEMENT password (#PCDATA)>


Questa è la pagina dove vengono inseriti login e password:

<html>
<head><title>Negozio On-Line - Home Page</title>

<body>

Benvenuto in questo sito di Pulcinella!!!<br>
<form name="loginutente" ACTION="ServletLogin">
entra come utente registrato: <br>
login <input type="text" name="login"></input>
password <input type="text" name="password"></input>
<input type="hidden" name="tipologin" value="loginutente"></input>
<input type="submit" value="ok">
</form>

<form name="loginadmin" ACTION="servlet/ServletLogin">
entra come amministratore del sito:<br>
login <input type="text" name="login"></input>
password <input type="text" name="password"></input>
<input type="hidden" name="tipologin" value="loginamministratore"></input>
<input type="submit" value="ok">
</form>


</body>
</html>


La servlet controlla login e password:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ServletLogin extends HttpServlet {

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String tipologin = request.getParameter("tipologin");
String login = request.getParameter("login");
String password = request.getParameter("password");
GestioneAccount verificaLogin = new GestioneAccount();

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>login="+login+"</body></html>");

if(tipologin.equals("loginamministratore")){
String esitoverifica = verificaLogin.verificaAccountUtente(login, password);

if(esitoverifica.equals(null)){
gotoPage("/areaAmministratore.jsp",request,response);
}
else {
request.setAttribute("erroreLogin", response);
gotoPage("/index.jsp",request,response);
}
}


}

private void gotoPage( String address,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(address);
dispatcher.forward(request,response);
}
}


La classe GestioneAccount che vedete nella servlet è questa, serve ad accedere al file Xml tramite DOM:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.SAXException;
import javax.servlet.*;


public class GestioneAccount{

public Document document;
/*public String account = System.getProperty("user.dir") + "\\" + "account.xml";*/
public String account = "E:\\Programmi\\Apache Software Foundation\\Tomcat 5.5\\webapps\\progetto\\WEB-INF\\classes\\account.xml";

//Costruttore
public GestioneAccount(){
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();

//Creao un Node che si riferisce al file "account.xml"
document = builder.parse(new File(account));
builder.setErrorHandler(new MyErrorHandler());


}
catch(ParserConfigurationException e){}
catch(SAXException r){r.printStackTrace();}
catch(FileNotFoundException me){}
catch(IOException t){}}


//Metodi
public String verificaAccountUtente(String loginInput, String passwordInput){

Element root = document.getDocumentElement();
String response="Login amministratore non valido";

NodeList login = root.getElementsByTagName("login");
NodeList password = root.getElementsByTagName("password");
NodeList tipi = root.getElementsByTagName("tipo");

for(int i=0; i<login.getLength() && response.equals("Login amministratore non valido"); i++) {
if(login.item(i).getTextContent().equals(loginInput)){
if(tipi.item(i).getTextContent().equals("amministratore")){
if (password.item(i).getTextContent().equals(passwordInput)) {
response=null;}
else { response="Password sbagliata";
}
}
else{response="Non sei un amministratore e non puoi entrare"; }
}

}
return response;
}

}


Purtroppo mi da questo errore Tomcat:


exception

java.lang.NullPointerException
GestioneAccount.verificaAccountUtente(GestioneAccount.java:36)
ServletLogin.doGet(ServletLogin.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
filters.ExampleFilter.doFilter(ExampleFilter.java:102)


Se mi aiutate mi fate un grande piacere. Ciao a tutti.

boysna
02-06-2006, 23:19
up