Ho un problema sui dati che la servlet manda al browser
La mia servlet riceve dati da un form, con questi dati crea o aggiorna un file xml e fin quà tutto ok e funziona.
Ad ogni operazione che fa la servlet visualizzo un messaggio sul browser, tipo quando il file xml è stato scritto comunico che la registrazione è avvenuta con successo oppure se i campi del form obbligatori non sono stati inseriti comunico che ci sono dei campi mancanti...ecc....
Il problema è questo
clicclo sul link registrati e appare il form
io clicco sul tasto invia del form e richiamo così la servlet. Mettiamo che non ho inserito nessun dato nel form, a video mi comparira sul browser "dati mancanti".
Poi rischiaccio sul link registrati, ricompare il form, riempio i campi e sul browser mi compare la scritta "registrazione avvenuta con successo" MA IL PROBLEMA è CHE è RIMASTA ANCHE LA SCRITTA CHE LA SERVER AVEVA INVIATO PRIMA "dati mancanti" E SOTTO AGGIUNGE "registrazione avvvenuta con successo".
Se io continuo a fare altri invi, giusti o sbagliati, i messaggi continuano a susseguirsi ma rimangono tutti.
COME FACCIO A FAR SI CHE RIMANGA SOLO L'ULTIMO????
INSERISCO IL CODICE DELLA SERVLET
[/CODE]codice:import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.io.File; import java.util.*; import java.text.*; import org.jdom.*; import org.jdom.input.SAXBuilder; import org.jdom.output.XMLOutputter; import java.util.Iterator; import java.util.List; public class registrazione extends HttpServlet{ StringBuffer buf = new StringBuffer(); Document documento = null; Element elementoRegistrati = null; String tipologia = new String(""); String nome,cognome,via,numciv,città,area,telefono,provincia,cap,nazione,professione,giorno_nascita,mese_nascita,anno_nascita,etnico,tradizionale,internazionale,winbar,vegetariano,password_inviata,username_inviato; public void aggiungiRegistrato(){ Element registrato = new Element("registrato"); Element element_nome = new Element("nome"); Element element_cognome = new Element("cognome"); Element element_via = new Element("via"); Element element_numciv = new Element("numciv"); Element element_city = new Element("city"); Element element_area = new Element("area"); Element element_telefono = new Element("telefono"); Element element_provincia = new Element("provincia"); Element element_cap = new Element("cap"); Element element_nazione = new Element("nazione"); Element element_professione = new Element("professione"); Element element_giorno = new Element("giorno"); Element element_mese = new Element("mese"); Element element_anno = new Element("anno"); Element element_tipologia = new Element("tipologia"); Element nascita = new Element("data_di_nascita"); if (etnico != null){ tipologia = tipologia + "etnico-"; } if (tradizionale != null){ tipologia = tipologia + "tradizionale-"; } if (winbar != null){ tipologia = tipologia + "winbar-"; } if (internazionale != null){ tipologia = tipologia + "internazionale-"; } if (vegetariano != null){ tipologia = tipologia + "vegetariano"; } element_nome.setText(nome); element_cognome.setText(cognome); element_via.setText(via); element_numciv.setText(numciv); element_city.setText(città); element_area.setText(area); element_telefono.setText(telefono); element_provincia.setText(provincia); element_cap.setText(cap); element_nazione.setText(nazione); element_professione.setText(professione); element_tipologia.setText(tipologia); element_giorno.setText(giorno_nascita); element_mese.setText(mese_nascita); element_anno.setText(anno_nascita); nascita.addContent(element_giorno); nascita.addContent(element_mese); nascita.addContent(element_anno); registrato.addContent(element_nome); registrato.addContent(element_cognome); registrato.addContent(element_via); registrato.addContent(element_numciv); registrato.addContent(element_city); registrato.addContent(element_cap); registrato.addContent(element_provincia); registrato.addContent(element_nazione); registrato.addContent(element_professione); registrato.addContent(nascita); registrato.addContent(tipologia); registrato.addContent(element_telefono); registrato.addContent(element_area); registrato.setAttribute("password",password_inviata); registrato.setAttribute("username",username_inviato); registrato.setAttribute("ruolo","generico"); elementoRegistrati.addContent(registrato); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter responseOutput = response.getWriter(); buf.append("<html><head><LINK REL='stylesheet' TYPE='text/css' HREF='../css/index.css' MEDIA='all' /></head><body>"); String controllo = new String("ok"); //RECUPERO LE INFORMAZIONI INVIATE DAL FORM nome = request.getParameter("nome"); cognome = request.getParameter("cognome"); via = request.getParameter("via"); numciv =request.getParameter("numciv"); città =request.getParameter("città"); telefono =request.getParameter("telefono"); provincia =request.getParameter("provincia"); cap =request.getParameter("cap"); nazione =request.getParameter("nazione"); professione =request.getParameter("professione"); giorno_nascita = request.getParameter("giorno"); mese_nascita = request.getParameter("mese"); anno_nascita = request.getParameter("anno"); etnico = request.getParameter("etnico"); tradizionale = request.getParameter("tradizionale"); vegetariano = request.getParameter("vegetariano"); winbar = request.getParameter("winbar"); internazionale = request.getParameter("internazionale"); password_inviata = request.getParameter("password_registrazione"); username_inviato = request.getParameter("username_registrazione"); if ((nome != "") && (cognome != "") && (via != "") && (numciv != "") && (città != "") && (provincia != "") && (cap != "") && (nazione != "") && (password_inviata != "") && (username_inviato != "")){ //CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATI String path = new String ("../webapps/restaurants/xml/registrati.xml"); File file = new File(path); //SE IL FILE NON ESISTE LO CREO E GLI AGGIUNGO L'ELEMENTO RADICE IL PRIMO REGISTRATO if (!file.exists()){ file.createNewFile(); elementoRegistrati = new Element("registrati"); //AGGIUNGO IL REGISTARTO CHE RISULTA IL PRIMO DATO CHE IL FILE REGISTRATI.XML NON ESISTEVA aggiungiRegistrato(); //SCRIVO IL DOCUMENT NEL FILE documento = new Document(elementoRegistrati); XMLOutputter xmlOutputter = new XMLOutputter(); try{ FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); xmlOutputter.output(documento, fileOutputStream); buf.append("Registrazione avvenuta con successo "); } catch (FileNotFoundException ex){ System.err.println(ex); } catch (IOException ex){ System.err.println(ex); } } else{ //SE IL FILE ESISTE LO CARICO IN MEMORIA SAXBuilder saxBuilder = new SAXBuilder(); try{ documento = saxBuilder.build(new File(path)); } catch (JDOMException ex){ System.err.println(ex); } catch (IOException ex){ System.err.println(ex); } //POI UNA VOLTA CARICATO CONTROLLO CHE NON CI SIA GIA' UN REGISTRATO CON TALE LOGIN E SE NON C'è INSERISCO IL NUOVO REGISTRATO ALTRIMENTI GLI DICO DI CAMBIARE LOGIN elementoRegistrati = documento.getRootElement(); List lista_registrati = elementoRegistrati.getChildren(); // ottengo un iteratore alla lista chiamando il metodo iterator() di Collection (essendo una list una collection) Iterator iteratore = lista_registrati.iterator(); while (iteratore.hasNext()) { // ottengo l'elemento corrente chiamando next() sull'iteratore Element registrato_corrente = (Element)iteratore.next(); String username = registrato_corrente.getAttributeValue("username"); String password = registrato_corrente.getAttributeValue("password"); if (password_inviata.equals(password)){ buf.append("Password già presente, sceglierla diversamente "); controllo = new String("ko"); break; } } if (controllo.equals("ok")){ //RISCRIVO L'INTERO DOCUMENTO NEL FILE XML DOPO AVER AGGIUNTO IL REGISTRATO aggiungiRegistrato(); XMLOutputter xmlOutputter = new XMLOutputter(); //IL METODO SEGUENTE Format.getPrettyFormat() SERVE PER SCRIVERE SUL FILE XML IN MODO BEN FORMATTATO CON GLI A CAPO E LE INDENTAZIONI PER OGNI ELEMENTO, MA NON ME LO ACCETTA //xmlOutputter.setFormat(Format.getPrettyFormat()); try{ FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); xmlOutputter.output(documento, fileOutputStream); buf.append("Registrazione avvenuta con successo "); } catch (FileNotFoundException ex){ System.err.println(ex); } catch (IOException ex){ System.err.println(ex); } } } } else{ buf.append("Informazioni mancanti, i campi con * sono obbligatori "); } buf.append("</body></html>"); responseOutput.println(buf.toString()); responseOutput.close(); } }

Rispondi quotando