Pagina 2 di 2 primaprima 1 2
Visualizzazione dei risultati da 11 a 14 su 14
  1. #11
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Session e' un oggetto che serve per mantenere in memoria i dati di un utente durante la visita del sito. Per maggiori info http://java.html.it/guide/lezione/793/session/ poi se cerchi qua e la' sul web trovi parecchie cose. E' una delle funzionalita' base di servlet/jsp.

  2. #12
    in un certo senso mi servirebbe perkè quando fa la login si dovrebbe ricordare ke l'utente ke fa il rateing è proprio quello ke si è appena loggato..
    in pratica questo session va bene in questo scenario? giusto?

  3. #13
    Utente di HTML.it
    Registrato dal
    Apr 2007
    Messaggi
    906
    Si. praticamente e' come il carrello della spesa nei negozi online (che poi in un applicazione di questo tipo sono salvati appunto in session), lei se ne sta li' e memorizza quello che vuoi tu.
    Unico avvertimento, non la riempire di troppa roba perche' questi oggetti finiscono nella memoria del server e ricordati di chiudere la sessione quando l'utente si slogga.

  4. #14
    grazie ho visto anke la documentazione e alcuni esempi on line solo ke mi da un errore di compilazione sia se uso getAttribute e sia se uso setAttribute e stando all'esempio le librerie sono tutte importate non vedo nulla di anomalo boh
    codice:
    /**
     * 
     */
    package projectservlet;
    
    /**
     * @(#)Login.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;
    
    				
    public class Login extends HttpServlet{
    
    	Document documento = null;
    	Element elementoRegistrati = null;
    	String password_inviata,username_inviato;	
    	
    	
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    		
    		HttpSession session = request.getSession(true);
    		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>");
    		
    		//RECUPERO LE INFORMAZIONI INVIATE DAL FORM
    		password_inviata = request.getParameter("password");
    		username_inviato = request.getParameter("username");
            
    		if ((password_inviata != "") && (username_inviato != "")){		
    			
    			//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATI
    			String path = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/UserRegistrated.xml");
    			File file = new File(path);
    			if (file.exists()){
    				//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) && username_inviato.equals(username)){
    						session.setAttribute("Username", username_inviato);
    						buf.append("<td align=\"center\">
    
    Thank you for logging in, " + username_inviato + ".
    If you want to rate some items chose for you, please 
    
    <a href=\"RecommendedItem.htm\">Proceed
    
    </a></p>
    </td>");
    						break;
    					}
    	 			}
    			}
    			else{
    				buf.append("<td align=\"center\">
    
    You are not registered. Please register.
    
    <a href=\"RegistrationAndSendEmail.htm\">Proceed
    
    </a></p>
    </td>");
    			}
    		}
    		else{
    			buf.append("<td align=\"center\">
    
    Missing informations, try again
    
    <a href=\"Login.htm\">Go Back
    
    </a></p>
    </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:
    /**
     * 
     */
    package projectservlet;
    
    /**
     * @(#)Recommendations.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 Recommendations extends HttpServlet {
    
    	Document documento = null; 
    	Document itemDocumento = null;
    	Element elementoRate = null; 
    	Element itemElemento = null;
    	String rating_inviato;	
    		
    	//metodo che permette di aggiungere un item	copiandolo dal file Item.xml
    	public void aggiungiRatedItem(){
    				
    		//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATED
    		String itemPath = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/Item.xml");
    		File itemFile = new File(itemPath);
    			
    		//SE IL FILE NON ESISTE LO CREO E GLI AGGIUNGO L'ELEMENTO RADICE IL PRIMO REGISTRATO
    		if (itemFile.exists()){
    				
    			//SE IL FILE ESISTE LO CARICO IN MEMORIA
     			SAXBuilder saxBuilder = new SAXBuilder();
    			try{
       				itemDocumento = saxBuilder.build(new File(itemPath)); 
     			}
     			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
    			itemElemento = itemDocumento.getRootElement(); 
    			List item_lista = itemElemento.getChildren();
    			// ottengo un iteratore alla lista chiamando il metodo iterator() di Collection (essendo una list una collection) 
     			Iterator itemIteratore = item_lista.iterator();
    			while (itemIteratore.hasNext())
    	 		{ 
    	   			// ottengo l'elemento corrente chiamando next() sull'iteratore 
    	   			Element item_corrente = (Element)itemIteratore.next(); 
    				String id = item_corrente.getAttributeValue("id");
    				
    				//copia il content corrente, impostando i nuovi attributi, in un altro file xml
    				Element rated = new Element("ratedItem");
    		
    				rated.addContent(item_corrente);
    		
    				rated.setAttribute("id", id);
    				rated.setAttribute("rate", rating_inviato);	
    				elementoRate.addContent(rated);
    	 		}	
    		}
    		
    		
    		
    
    	}
    	
    	
    
        
    	
    	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>");
    		
    		//RECUPERO LE INFORMAZIONI INVIATE DAL FORM
    		rating_inviato = request.getParameter("rated");
    		
    		if (rating_inviato != ""){	
    			HttpSession session = request.getSession(true);
    			String username = (String)session.getAttribute("Username");
    			//CONTROLLO SE ESISTE GIA' UN FILE .XML CHE SI CHIAMA REGISTRATI
    			String path = new String ("C:/Programmi/Apache Software Foundation/Tomcat 6.0/webapps/ITemRecommender/xml/" + username + " Recommendations.xml");
    			File file = new File(path);
    			//SE IL FILE NON ESISTE LO CREO E GLI AGGIUNGO L'ELEMENTO RADICE IL PRIMO ITEM CON RATING
    			if (!file.exists()){
    				file.createNewFile();
    				elementoRate = new Element("recommendations");
    				//AGGIUNGO IL RATING CHE RISULTA IL PRIMO DATO CHE IL FILE RECOMMENDATIONS.XML NON ESISTEVA
    				aggiungiRatedItem();
    				//SCRIVO IL DOCUMENT NEL FILE		
    				documento = new Document(elementoRate);
    				XMLOutputter xmlOutputter = new XMLOutputter();
    				try{
       					FileOutputStream fileOutputStream = new FileOutputStream(new File(path)); 
       					xmlOutputter.output(documento, fileOutputStream);
     				}
    	 			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);
     				}					
    				//RISCRIVO L'INTERO DOCUMENTO NEL FILE XML DOPO AVER AGGIUNTO IL RATING
    				aggiungiRatedItem();
    				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\">
    
    Items Rated.
    
    <a href=\"index.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, try again
    
    <a href=\"RecommendedItem.htm\">Go Back
    
    
    </a></p>
    </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);
        }
    }
    l'errore è di questo tipo
    codice:
    cannot resolve symbol method setAttribute (java.lang.String,java.lang.String)
    codice:
    cannot resolve symbol method getAttribute (java.lang.String)
    perkè?
    inoltre se volessi copiare i content e gli attribute presenti in un file .xml si potrebbe fare come ho fatto nel metodo aggiungiRatedItem()? ovvero il content selzionato dall'iteretor lo pongo come content del nuovo file giusto?

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.