Grazie per la risposta.
Questa č la servlet:
codice:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.lang.String;
public class ServletRegistrati extends HttpServlet{
public void goToPage(String url,HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
RequestDispatcher dispatcher= getServletContext().getRequestDispatcher(url);
dispatcher.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response )
throws ServletException, IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
HttpSession session= req.getSession(true);
// otteniamo la path del server
ServletContext p = getServletContext();
String pathserver=p.getRealPath("/");
String pathdef=pathserver+"database/clienti.xml";
String[] valoreParam = new String[2];
valoreParam[0] = req.getParameter("login");
valoreParam[1] = req.getParameter("password");
// memorizzo anche tali parametri in una stringa per il riepilogo
String iscrizioneCliente="<tr> <td> Login: "+valoreParam[0]+"</td>"+
"<tr> <td> Password: "+valoreParam[1]+"</td>";
// crea un oggetto GestioneXML
GestioneXML gestione = new GestioneXML();
// invoco il metodo aggiungiClientiXML
String xml=gestione.aggiungiClientiXML(pathdef,valoreParam);
// setto un attributo per l'oggetto session
session.setAttribute("iscrcliente",iscrizioneCliente);
// salvataggio database clienti su file clienti.xml
File file = new File(pathdef);
if (file!=null)
{
try{
FileOutputStream fs = new FileOutputStream(file);
PrintStream ps = new PrintStream(fs);
ps.println(xml);
fs.close();
} catch(Exception e){ e.printStackTrace();}
}
goToPage("/riepilogo_cliente.jsp",req,res);
}
}
e questo č la classe GestioneXML:
codice:
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
import java.io.*;
public class GestioneXML {
public GestioneXML(){}
// metodo x aggiungere clienti al file clienti.xml
public String aggiungiClientiXML(String pathin,String[] valori){
Document doc=null;
Element root=null;
String outxml=null;
SAXBuilder sbuilder = new SAXBuilder(true);
try{
doc = sbuilder.build(pathin);
System.out.println(pathin+" č valido");
}
catch(JDOMException e){
System.out.println(pathin+"non č valido");
System.out.println(e.getMessage());
}
catch(IOException e){
System.out.println("Non Posso controllare "+pathin);
System.out.println(e.getMessage());
}
root =doc.getRootElement();
/* ISTANZIAMO ALTRI OGGETTI Element PASSANDOGLI LE STRINGHE
CHE SARANNO I NOMI DEI NOSTRI TAG */
Element cliente = new Element("cliente");
Element elogin = new Element("login");
Element epassword = new Element("password");
// ASSEGNAMO DEI VALORI AI TAG
elogin.addContent(valori[0]);
epassword.addContent(valori[1]);
// AGGIUNGIAMO I TAG AL TAG cliente
cliente.addContent(elogin);
cliente.addContent(epassword);
// AGGIUNGIAMO IL TAG cliente AL SUPERTAG root
root.addContent(cliente);
//ISTANZIAMO LA CLASSE XMLOutputter CHE CI PERMETTE DI FORMATTARE IL FILE XML. */
XMLOutputter xout = new XMLOutputter(" ",true);
// SCRIVIAMO IL DOCUMENTO IN UNA STRINGA
outxml =xout.outputString(doc);
return outxml;
}// fine metodo aggiungiClientiXML
}
Grazie. CIAO