questo è il contenuto della servlet relativa alla registrazione di un utente
codice:
/**
*
*/
package projectservlet;
/**
* @(#)RegistrationAndSendEmail.java
*
*
* @author Raffaella
* @version 1.00 2007/4/24
*/
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;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class RegistrationAndSendEmail extends HttpServlet{
Document documento = null;
Element elementoRegistrati = null;
String fullname,email,password_inviata,username_inviato;
public void aggiungiRegistrato(){
Element registrato = new Element("registrated");
Element element_nome = new Element("fullname");
Element element_email = new Element("email");
element_nome.setText(fullname);
element_email.setText(email);
registrato.addContent(element_nome);
registrato.addContent(element_email);
registrato.setAttribute("password",password_inviata);
registrato.setAttribute("username",username_inviato);
elementoRegistrati.addContent(registrato);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
StringBuffer buf = new StringBuffer();
response.setContentType("text/html");
PrintWriter responseOutput = response.getWriter();
buf.append("<html><head></head><body><table border=\"1\" width=\"40%\" align=\"center\"><tbody><tr>");
String controllo = new String("ok");
//RECUPERO LE INFORMAZIONI INVIATE DAL FORM
fullname = request.getParameter("fullname");
email = request.getParameter("email");
password_inviata = request.getParameter("password");
username_inviato = request.getParameter("username");
if ((fullname != "") && (email != "") && (password_inviata != "") && (username_inviato != "")){
//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATED
String path = new String ("../webapps/ITemRecommender/xml/registrated.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("registrated");
//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("<td align=\"center\">Thank you for registering, " + username_inviato + ". An email has been dispatched to " + email + " with details on how to activate your account.
<a href=\"Recommendations.htm\">Proceed</a></p></td>");
}
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 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("<td>Password existing, choose a different password.</td>");
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();
try{
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
xmlOutputter.output(documento, fileOutputStream);
buf.append("<td>Registration Successfull
<a href=\"Recommendations.htm\">Proceed</a></p></td>");
}
catch (FileNotFoundException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
}
}
}
else{
buf.append("<td>Missing informations, fields with * are compulsory</td>");
}
buf.append("</tr></tbody></table></body></html>");
responseOutput.println(buf.toString());
responseOutput.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
sarebbe sbagliato questo percorso String path = new String ("../webapps/ITemRecommender/xml/registrated.xml"); ?