QUESTO è IL FILE .HTML CHE CONTIENE IL FORM
<HTML>
<HEAD>
<LINK REL="stylesheet" TYPE="text/css" HREF="css/index.css" MEDIA="all" />
<TITLE>
PAGINA DI INSERIMENTO RISTORANTE
</TITLE>
</HEAD>
<BODY bgcolor="#99FF99">
<DIV ID="titolo_inserimento_ristorante">
INSERISCI LE INFORMAZIONI DEL RISTORANTE DA AGGIUNGERE ALL'ARCHIVIO
</DIV>
<FORM id="form_inserimento_ristorante" METHOD="post" ACTION="servlets/inserimento_ristorante" TARGET="iframe_content" enctype= "multipart/form-data" >
Nome: <INPUT TYPE="text" name="nome" id = "nome"/>
Via: <INPUT TYPE="text" name="via" id="via" />
Numero civico: <INPUT TYPE="text" name="numciv" id = "numciv"/>
Città: <INPUT TYPE="text" name="città" id="città" />
Area: <INPUT TYPE="text" name="area" id = "area"/>
Telefono: <INPUT TYPE="text" name="telefono" id="telefono" />
Sito internet: <INPUT TYPE="text" name="sito" id = "sito"/>
Numero coperti: <INPUT TYPE="text" name="numcop" id="numcop" />
Carte di credito accettate: <INPUT TYPE="text" name="carte" id = "carte"/>
Foto: <INPUT TYPE="file" name="file" id="file" />
<DIV ID="tipologia">
<DIV ID="titoletto_tipologia">Tipologia</DIV>
<INPUT TYPE="radio" name="tipologia" value="etnico"/>Etnico
<INPUT TYPE="radio" name="tipologia" value="winbar"/>Win bar
<INPUT TYPE="radio" name="tipologia" value="vegetariano"/>Vegetariano
<INPUT TYPE="radio" name="tipologia" value="tradizionale"/>Tradizionale
<INPUT TYPE="radio" name="tipologia" value="internazionale"/>Internazionale
</DIV>
<INPUT id = "invio_inserimento_ristorante" TYPE="submit" VALUE="Invia" />
</FORM>
</BODY>
</HTML>
QUESTO SOTTO INVECE è IL CODICE DELLA SERVLET
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 inserimento_ristorante extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter responseOutput = response.getWriter();
StringBuffer buf = new StringBuffer();
//RECUPERO LE INFORMAZIONI INVIATE DAL FORM MA SONO TUTTE NULLE
String nome = request.getParameter("nome");
String via = request.getParameter("via");
String numciv =request.getParameter("numciv");
String città =request.getParameter("città");
String area =request.getParameter("area");
String telefono =request.getParameter("telefono");
String sito =request.getParameter("sito");
String numcop =request.getParameter("numcop");
String carte =request.getParameter("carte");
String foto =request.getParameter("foto");
String tipologia = request.getParameter("tipologia");
//CONTROLLO SE ESISTE GIA' UN FILE .XML CON IL NOME DEL RISTORANTE INSERITO
String pathdir = new String ("../webapps/restaurants/xml/" + città);
String pathfile = new String ("../webapps/restaurants/xml/" + città + "/" + nome +".xml");
buf.append(pathfile);
File file = new File(pathdir);
if (!file.exists()){
file.mkdir();
}
file = new File(pathfile);
if (file.exists()){
buf.append("Un ristorante con tale nome è già presente in archivio dei ristoranti di questa città");
}
else{
file.createNewFile();
Element elementoRistorante = new Element("ristorante");
Element elementoNome = new Element("nome");
elementoNome.setText(nome);
elementoRistorante.addContent(elementoNome);
Element elementoVia = new Element("via");
elementoNome.setText(via);
elementoRistorante.addContent(elementoVia);
Element elementoNumciv = new Element("numciv");
elementoNome.setText(numciv);
elementoRistorante.addContent(elementoNumciv);
Element elementoCity = new Element("city");
elementoNome.setText(città);
elementoRistorante.addContent(elementoCity);
Element elementoArea = new Element("area");
elementoNome.setText(area);
elementoRistorante.addContent(elementoArea);
Element elementoTelefono = new Element("telefono");
elementoNome.setText(telefono);
elementoRistorante.addContent(elementoTelefono);
Element elementoSito = new Element("sito");
elementoNome.setText(sito);
elementoRistorante.addContent(elementoSito);
Element elementoNumcop = new Element("numcop");
elementoNome.setText(numcop);
elementoRistorante.addContent(elementoNumcop);
Element elementoCarte = new Element("carte");
elementoNome.setText(carte);
elementoRistorante.addContent(elementoCarte);
Element elementoFoto = new Element("foto");
elementoNome.setText(foto);
elementoRistorante.addContent(elementoFoto);
Element elementoTipologia = new Element("tipologia");
elementoNome.setText(tipologia);
elementoRistorante.addContent(elementoTipologia);
Document documento = new Document(elementoRistorante);
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(pathfile));
xmlOutputter.output(documento, fileOutputStream);
}
catch (FileNotFoundException ex){
System.err.println(ex);
}
catch (IOException ex){
System.err.println(ex);
}
}
responseOutput.println(buf.toString());
responseOutput.close();
}
}

Rispondi quotando