ciao a tutti
vorrei creare all'interno di una mia servlet un file .htm
io ho provato a mettere il codice in questo modo ma mi da il seguente errore
codice:
/**
* @(#)AddItems.java
*
*
* @author Raffaella
* @version 1.00 2007/5/30
*/
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 java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
public class AddItems {
Document documento = null;
Element elementoItems = null;
String id, author, description;
public void aggiungiItem(){
Element added = new Element("item");
Element element_id = new Element("id");
Element element_author = new Element("author");
Element element_description = new Element("description");
element_id.setText(id);
element_author.setText(author);
element_description.setText(description);
added.addContent(element_id);
added.addContent(element_author);
added.addContent(element_description);
elementoItems.addContent(added);
}
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
id = request.getParameter("id");
author = request.getParameter("author");
description = request.getParameter("description");
if ((id != "") && (author != "") && (description != "")){
//creiamo un oggetto FileWriter per poter creare una pagina web inserendo in un combobox
//gli id degli item aggiunti
try {
//se il file non esiste lo crea e provvede a inserirvi l'elemento
BufferedWriter webPage = new BufferedWriter( new FileWriter("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/ChooseItem.htm"));
String text1 = "<html><head><title>Choose Item</title></head><body><h2>Choose An Item</h2><FORM id=\"form_item_chose\" METHOD=\"post\" ACTION=\"ChooseItem\"><b ID=\"ItemChose\">Choose an item to rate to a customer.
Choose one of these
<SELECT NAME=\"chose\"><OPTION>";
String text2 = "</OPTION></SELECT></p>
<INPUT id = \"send\" TYPE=\"submit\" VALUE=\"Send\" /></p>
</FORM></body></html>";
webPage.write(text1 + id + text2);
}
catch (IOException ex) {
System.err.println(ex);
}[/b]
//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATED
String path = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/Item.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();
elementoItems = new Element("itemAdded");
//AGGIUNGO IL REGISTARTO CHE RISULTA IL PRIMO DATO CHE IL FILE REGISTRATI.XML NON ESISTEVA
aggiungiItem();
//SCRIVO IL DOCUMENT NEL FILE
documento = new Document(elementoItems);
XMLOutputter xmlOutputter = new XMLOutputter();
try{
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
xmlOutputter.output(documento, fileOutputStream);
buf.append("<td align=\"center\">Item, " + id + " added.
<a href=\"AddItem.htm\">Go Back</a></p>
<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
elementoItems = documento.getRootElement();
List lista_items = elementoItems.getChildren();
// ottengo un iteratore alla lista chiamando il metodo iterator() di Collection (essendo una list una collection)
Iterator iteratore = lista_items.iterator();
while (iteratore.hasNext())
{
// ottengo l'elemento corrente chiamando next() sull'iteratore
Element id_corrente = (Element)iteratore.next();
if (id.equals(id_corrente)){
buf.append("<td align=\"center\">Item Already Added.
<a href=\"AddItem.htm\">Go Back</a></p></td>");
controllo = new String("ko");
break;
}
}
if (controllo.equals("ok")){
//RISCRIVO L'INTERO DOCUMENTO NEL FILE XML DOPO AVER AGGIUNTO IL REGISTRATO
aggiungiItem();
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 lo accetta
//xmlOutputter.setFormat(Format.getPrettyFormat());
try{
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
xmlOutputter.output(documento, fileOutputStream);
buf.append("<td align=\"center\">Item Successfull Added.
<a href=\"AddItem.htm\">Go Back</a></p>
<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 align=\"center\">Missing informations, fields with * are compulsory.</td></p><a href=\"AdminRegistration.htm\">Go Back</a></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);
}
}
codice:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error allocating a servlet instance
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:584)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.NoClassDefFoundError: projectservlet/AddItems (wrong name: AddItems)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1817)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1325)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:584)
org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508)
java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.13
perkè cosa ho sbagliato??
e poi è possibile leggere con un BufferedReader una parola e sostituirla con un altra una volta ke è stata trovata all'interno della linea appena letta?io ho visto nella doc della sun ma non ho trovato nessun metodo ke sia equivalente a find() o replace()
non so la cosa è fattibile?
grazie mille