|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
[JAVA] Meglio usare un applet o una servlet?
Io ho un programma che per ora mi memorizza in 2 classi degli oggetti (Stile, Body)
Per ora ho fatto il parser dom... Io alla fine devo confrontare alcune stringhe della classe Body con quelle della classe Stile. E devo fare dei controlli es. il testo deve essere in Times New Romans e di carattere 12 e 13. Se è tutto a posto devo generare un qualcosa (non so se sia megli un file.html o qualcos'altro) che mi dice che il file in questione rispetta tutti i vincoli. Se invece non li rispetta dico. il file non rispetta questo o quel vincolo... Se avete capito più o meno cosa intendo, ora che ho fatto il parser e prima di passare al confronto delle stringhe dovrei rendere il programma eseguibile da internet tramite una applet o una servlet.... Io non so quale delle 2 soluzioni sia la migliore, potete darmi una mano nella scelta e anche spiegarmi come fare a modificare il mio programma? Questo è per ora il mio programma: Codice:
import java.io.*; import java.util.*; import javax.xml.parsers.*; import org.w3c.dom.*; public class ParserDom { Vector styleNodes = new Vector(); Vector styleObjects = new Vector(); Vector bodyNodes = new Vector(); Vector bodyObjects = new Vector(); public ParserDom() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); System.exit(1); } Document document = null; try { PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Giovedì 17.txt", false))); document = builder.parse(new File("C:\\Documents and settings\\Administrator\\Desktop\\DAVIDE\\tesi.xml")); findStyleNodes(document); createStyleObjects(); printStyles(pw); findBodyNodes(document); createBodyObjects(); printBody(pw); pw.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } private void findStyleNodes(Node startingNode) { NodeList childNodes = startingNode.getChildNodes(); for ( int i = 0; i < childNodes.getLength(); i++) { Node actualNode = childNodes.item(i); if ( isStyleNode(actualNode) ) styleNodes.addElement(actualNode); if ( actualNode.hasChildNodes() ) findStyleNodes(actualNode); } } private boolean isStyleNode(Node node) { return node.getNodeName().equals("w:style"); } private void createStyleObjects() { for ( int i = 0; i < styleNodes.size(); i++) { Node styleNode = (Node)styleNodes.elementAt(i); Style style = new Style(); style.name = getAttributeValue(styleNode, "w:styleId"); Node basedNode = getChildNode(styleNode, "w:basedOn"); if ( basedNode != null ) style.based = getAttributeValue(basedNode, "w:val"); Node pPrNode = getChildNode(styleNode, "w:pPr"); if ( pPrNode != null ) { Node pstileNode = getChildNode(pPrNode, "w:pStyle"); if ( pstileNode != null ) style.pstile = getAttributeValue(pstileNode, "w:val"); Node jcNode = getChildNode(pPrNode, "w:jc"); if ( jcNode != null ) style.jc = getAttributeValue(jcNode, "w:val"); } Node paragraphNode = getChildNode(styleNode, "w:rPr"); if ( paragraphNode != null ) { Node fontNode = getChildNode(paragraphNode, "wx:font"); if ( fontNode != null ) style.font = getAttributeValue(fontNode, "wx:val"); else { Node rfontNode = getChildNode(paragraphNode, "w:rFonts"); if ( rfontNode != null ) style.font = getAttributeValue(rfontNode, "w:cs"); } Node sizeNode = getChildNode(paragraphNode, "w:sz"); if ( sizeNode != null ) style.size = getAttributeValue(sizeNode, "w:val"); Node grassettoNode = getChildNode(paragraphNode, "w:b"); if ( grassettoNode != null ) style.grassetto = getAttributeValue(grassettoNode, "w:val"); Node italicoNode = getChildNode(paragraphNode, "w:i"); if ( italicoNode != null ) style.italico = getAttributeValue(italicoNode, "w:val"); Node sottolineatoNode = getChildNode(paragraphNode, "w:u"); if ( sottolineatoNode != null ) style.sottolineato = getAttributeValue(sottolineatoNode, "w:val"); } styleObjects.addElement(style); } } private String getAttributeValue(Node node, String attributeName) { NamedNodeMap attributes = node.getAttributes(); if ( attributes == null ) return ""; Node item = attributes.getNamedItem(attributeName); if ( item == null ) return ""; return item.getNodeValue(); } private Node getChildNode(Node node, String nodeName) { NodeList childNodes = node.getChildNodes(); for ( int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if ( childNode.getNodeName().equals(nodeName) ) return childNode; } return null; } private void printStyles(PrintWriter pw) { for ( int i = 0; i < styleObjects.size(); i++) { Style style = (Style)styleObjects.elementAt(i); System.out.println(style); pw.println(style); } } public static void main(String args[]) { new ParserDom(); } class Style { String name; String based; String pstile; String jc; String font; String size; String grassetto; String italico; String sottolineato; public String toString() { StringBuffer buffer = new StringBuffer(); if ( name != null ) { buffer.append("Stile: " + name + "\n"); if ( based != null ) buffer.append(" Basato su: " + based + "\n"); if ( pstile != null ) buffer.append(" pStyle: " + pstile + "\n"); if ( jc != null ) buffer.append(" Jc: " + jc + "\n"); if ( font != null ) buffer.append(" Font: " + font + "\n"); if ( size != null ) buffer.append(" Dimensione: " + size + "\n"); if ( grassetto != null ) buffer.append(" Grassetto " + grassetto + "\n"); if ( italico != null ) buffer.append(" Italico " + italico + "\n"); if ( sottolineato != null ) buffer.append(" Sottolineato: " + sottolineato + "\n"); } return buffer.toString(); } } private void findBodyNodes(Node startingNode) { NodeList childNodes = startingNode.getChildNodes(); for ( int i = 0; i < childNodes.getLength(); i++) { Node actualNode = childNodes.item(i); if ( isBodyNode(actualNode) ) bodyNodes.addElement(actualNode); if ( actualNode.hasChildNodes() ) findBodyNodes(actualNode); } } private boolean isBodyNode(Node node) { return node.getNodeName().equals("w:p"); } private void createBodyObjects() { String stile = new String(); String jc = new String(); for ( int i = 0; i < bodyNodes.size(); i++) { Node bodyNode = (Node)bodyNodes.elementAt(i); stile = null; jc = null; Node paragraphNode = getChildNode(bodyNode, "w:pPr"); if ( paragraphNode != null ) { Node stileNode = getChildNode(paragraphNode, "w:pStyle"); if ( stileNode != null ) stile = getAttributeValue(stileNode, "w:val"); Node jcNode = getChildNode(paragraphNode, "w:jc"); if ( jcNode != null ) jc = getAttributeValue(jcNode, "w:val"); } NodeList pNode = getAllChildNode(bodyNode, "w:r"); for ( int j = 0; j < pNode.getLength(); j++) { Body body = new Body(); body.stile = stile; body.jc = jc; Node rNode = (Node)pNode.item(j); Node rPrNode = getChildNode(rNode, "w:rPr"); if ( rPrNode != null ) { Node rstileNode = getChildNode(rPrNode, "w:rStyle"); if ( rstileNode != null ) body.stile = getAttributeValue(rstileNode, "w:val"); Node grassettoNode = getChildNode(rPrNode, "w:b"); if ( grassettoNode != null ) body.grassetto = getAttributeValue(grassettoNode, "w:val"); Node italicoNode = getChildNode(rPrNode, "w:i"); if ( italicoNode != null ) body.italico = getAttributeValue(italicoNode, "w:val"); Node sottolineatoNode = getChildNode(rPrNode, "w:u"); if ( sottolineatoNode != null ) body.sottolineato = getAttributeValue(sottolineatoNode, "w:val"); Node sizeNode = getChildNode(rPrNode, "w:sz"); if ( sizeNode != null ) body.size = getAttributeValue(sizeNode, "w:val"); } Node pictNode = getChildNode(rNode, "w:pict"); if ( pictNode != null ) { Node shapeNode = getChildNode(pictNode, "v:shape"); if ( shapeNode != null ) { Node imagedataNode = getChildNode(shapeNode, "v:imagedata"); if ( imagedataNode != null ) { body.titolopict = getAttributeValue(imagedataNode, "o:title"); body.formatopict = getAttributeValue(imagedataNode, "src"); } } } Node wtNode = getChildNode(rNode, "w:t"); if ( wtNode != null ) { Node testoNode = getChildNode(wtNode, "#text"); if ( testoNode != null ) body.testo = testoNode.getNodeValue(); } bodyObjects.addElement(body); } } } private void printBody(PrintWriter pw) { for ( int i = 0; i < bodyObjects.size(); i++) { Body body = (Body)bodyObjects.elementAt(i); System.out.println(body); pw.println(body); } } class Body { String stile; String jc; String rstile; String grassetto; String italico; String sottolineato; String size; String testo; String titolopict; String formatopict; public String toString() { StringBuffer buffer = new StringBuffer(); if ( stile != null ) buffer.append("Stile body: " + stile + "\n"); if ( jc != null ) buffer.append(" Jc body: " + jc + "\n"); if ( rstile != null ) buffer.append(" Rstile body " + rstile + "\n"); if ( grassetto != null ) buffer.append(" Grassetto body " + grassetto + "\n"); if ( italico != null ) buffer.append(" Italico body " + italico + "\n"); if ( sottolineato != null ) buffer.append(" Sottolineato body: " + sottolineato + "\n"); if ( size != null ) buffer.append(" Dimensione body: " + size + "\n"); if ( testo != null ) buffer.append(" Testo body: " + testo + "\n"); if ( titolopict != null ) buffer.append(" Nome immagine body: " + titolopict + "\n"); if ( formatopict != null ) buffer.append(" Formato immagine body: " + formatopict + "\n"); return buffer.toString(); } } private NodeList getAllChildNode(Node node, String nodeName) { ArrayNodeList arrNodeList = new ArrayNodeList(); NodeList childNodes = node.getChildNodes(); for ( int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if ( childNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && childNode.getNodeName().equals(nodeName) ) { arrNodeList.add(childNode); } } return arrNodeList; } class ArrayNodeList implements NodeList { private ArrayList nodesArray = new ArrayList(); public void add(Node n) { nodesArray.add(n); } public int getLength() { return nodesArray.size(); } public Node item(int index) { try { return (Node)nodesArray.get(index); } catch (IndexOutOfBoundsException e) { return null; } } } }
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
ehmm,...
sono due cose concettualmente opposte ![]() le applet girano lato client (e tra l'altro sono ormai sconsigliate, a meno di requisiti stringenti sull'embedding del risultato in una pagina web, a favore di Java Web Start) mentre le servlet girano lato server. L'utente deve elaborare i dati in locale o è meglio che li mandi al server che li elaborerà e li restituirà in una pagina web opportunamente formattata? (o in quello che vuoi ![]() Una normalissima scelta architetturale che però mi sa che puoi fare solo tu dato che conosci i requisti del problema ![]()
__________________
![]() |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Se come sembra a parer tuo e di altri che ho sentito..dovessi creare una servlet.
Come dovrei comportarmi? C'è molto codice da modificare???
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
La logica del tuo programma ad occhio non dovresti toccarla, però dovresti creare uan pagina JSP che permetta all'utente di interfacciarsi con il tuo programma e che poi gli permetterà di visualizzare il file xml generato dopo aver fatto l'upload del file sul server per essere rielaborato.
In pratica non devi modificare quasi nulla ma devi aggiungere un bel pò di cose..
__________________
![]() |
![]() |
![]() |
![]() |
#5 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Difficile creare una pagina JSP???
QUanto ci vuole per farne una semplice?
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#6 | |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Quote:
Roba di un paio di orette con un ambiente configurato. ...se però non hai mai fatto niente del genere mi sa che un pò di tempo lo perdi per entrare nell'ottica ![]() forse l'ideale è andare nel tutorial di Java enterprise edition e guardare la parte relativa alle JSP. Usare una servlet, se non hai alcun requisito ben preciso che ti istruisce a fare così, è solo tempo sprecato dato che una JSP in realtà viene compilata automaticamente in una servlet in seguito alla prima visita. Ma scrivere una JSP è MOLTO + umano che scrivere una servlet ![]() Quindi concludendo: scaricati tomcat (che per quello che devi fare è anche troppo), leggi il tutorial di Java Enterprise Edition e prova a scrivere la tua JSP che fa l'upload del file da elaborare e restituisce in output una pagina HTML prodotta dalla logica business che hai implementato e che elabora il file appena uploadato ![]()
__________________
![]() |
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Jan 2003
Città: Milano - Udine
Messaggi: 9418
|
|
![]() |
![]() |
![]() |
#8 | |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Quote:
La mia pagina non deve contenere tanta roba...praticamente basta un bottone che faccia partire il mio programma java e basta!!! e poi una pagina finale da aggiungere al mio programma in cui scrivo delle cose io ed alcune me le deve scrivere il programma...
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
|
![]() |
![]() |
![]() |
#9 | |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Quote:
ma per quello che deve fare lui 2 pagine JSP bastano ed avanzano.. E mi pare sicuramente + pulito che scrivere direttamente una servlet ![]()
__________________
![]() |
|
![]() |
![]() |
![]() |
#10 | |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Quote:
__________________
![]() |
|
![]() |
![]() |
![]() |
#11 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
ESATTO! Hai ragione, ma è difficile farle? Ho 1 settimana di tempo...
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#12 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
non credo che avrai problemi.. Casomai prova ad usare netbeans che dovrebbe già avere tutto l'occorrente per ja versione JEE e puoi scegliere in fase di installazione se usare glassfish o tomcat che vengono caricati in maniera embedded. Per quello che devi fare tu tomcat basta ed avanza...
__________________
![]() |
![]() |
![]() |
![]() |
#13 | |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Quote:
E poi mi potresti dare anche un manuale (possibilmente in italiano) per eseguire queste operazioni? Se avrò bisogno di una mano chiederò con te... Per il momento GRAZIE!
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
|
![]() |
![]() |
![]() |
#14 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Www.netbeans.org per il manuale in italiano non ho idea però...
__________________
![]() |
![]() |
![]() |
![]() |
#15 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Mi diresti le librerie dove sono contenuti javax.servlet.*
e dove potrei scaricarle? GRAZIE... Quando io compilo questo: Codice:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import com.oreilly.servlet.*; public class ControllerFiles extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Istanziamo le variabili // Il ServletContext sevirà per ricavare il MIME type del file uploadato ServletContext context = getServletContext(); String forw = null; try { // Stabiliamo la grandezza massima del file che vogliamo uploadare int maxUploadSize = 50000000; MultipartRequest multi = new MultipartRequest(request, ".", maxUploadSize); String descrizione = multi.getParameter("text"); File myFile = multi.getFile("myFile"); String filePath = multi.getOriginalFileName("myFile"); String path = "C:\\files\\"; try { // ricaviamo i dati del file mediante un InputStream FileInputStream inStream = new FileInputStream(myFile); // stabiliamo dove andrà scritto il file FileOutputStream outStream = new FileOutputStream(path + myFile.getName()); // salviamo il file nel percorso specificato while ( inStream.available() > 0 ) { outStream.write(inStream.read()); } // chiudiamo gli stream inStream.close(); outStream.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } forw = "/done.jsp"; // mettiamo nella request i dati così da poterli ricavare dalla jsp request.setAttribute("contentType", context.getMimeType(path + myFile.getName())); request.setAttribute("text", descrizione); request.setAttribute("path", path + myFile.getName()); request.setAttribute("size", Long.toString(myFile.length()) + " Bytes"); RequestDispatcher rd = request.getRequestDispatcher(forw); rd.forward(request, response); } catch (Exception e) { e.printStackTrace(); } } } C:\Documents and Settings\Utente\Desktop\ControllerFiles.java:21: cannot find symbol symbol : method getOriginalFileName(java.lang.String) location: class com.oreilly.servlet.MultipartRequest String filePath = multi.getOriginalFileName("myFile"); ^ 1 error
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. Ultima modifica di xxdavide84xx : 21-01-2008 alle 08:27. |
![]() |
![]() |
![]() |
#16 | |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Quote:
Con netbeans se apri un progetto Java EE hai già tutto l'occorrente, librerie e application server incluso.
__________________
![]() |
|
![]() |
![]() |
![]() |
#17 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Va bene NetBeans 5.5.1?
Mi dici come fare per l'upload e fare funzionare il mio programma??? E poi stampare il risultato col secondo JSP.. GRAZIE!
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#19 |
Member
Iscritto dal: May 2006
Città: Cesenatico
Messaggi: 274
|
Non so...io devo semplicemente caricare un file .xml parsarlo col mio programma java e restituire dopo l'esecuzione del programma una pagina (o finestra) che dica se sono o no rispettati dei parametri STANDARD nello specifico file
__________________
CPU Intel i5-4590, Scheda Madre Asrock H97 Pro4, RAM DDR3 Corsair Vengeance 1600MHz 8GB CL9, Hard Disk WD Caviar Blue 1TB, SSD Crucial MX100 256GB. |
![]() |
![]() |
![]() |
#20 | |
Bannato
Iscritto dal: Jan 2003
Città:
Messaggi: 4421
|
Quote:
...ciao... |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 05:48.