<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4223459690803294564</id><updated>2026-08-15T22:02:26.163+02:00</updated><category term="Eclipse"/><category term="immagini"/><category term="Java Reflection"/><category term="Security"/><category term="Servlet"/><category term="Annotations"/><category term="Apache Tomcat"/><category term="JFrame"/><category term="JOptionPane"/><category term="JPanel"/><category term="Spring"/><category term="codifica"/><category term="password"/><category term="print"/><category term="try-with-resources"/><category term="Apache Commons"/><category term="Apache Poi"/><category term="Application Scope"/><category term="Base64"/><category term="BufferedImage"/><category term="Build path"/><category term="Date"/><category term="GET"/><category term="ImageIcon"/><category term="JAX-RS"/><category term="JSP"/><category term="JVM"/><category term="Java EE 7"/><category term="Java Mail"/><category term="Jersey"/><category term="Printable"/><category term="REST"/><category term="RESTful"/><category term="Range di valori rappresentabili"/><category term="Response"/><category term="Runnable"/><category term="SSL"/><category term="Servlet 3.0"/><category term="Struttura dati"/><category term="Thread"/><category term="Tomcat"/><category term="caricare immagini"/><category term="commons-codec.jar"/><category term="compilazione"/><category term="compressione"/><category term="configurazione Spring Security"/><category term="confrontare oggetti"/><category term="conversione"/><category term="decodifica"/><category term="eccezioni"/><category term="email"/><category term="errore"/><category term="eseguibile"/><category term="finestra"/><category term="hash"/><category term="hibernate"/><category term="immagine"/><category term="interfacce Java"/><category term="jar"/><category term="java"/><category term="java.awt.print"/><category term="java.io.Closeable"/><category term="java.io.File"/><category term="java.lang.AutoCloseable"/><category term="java.lang.Reflect"/><category term="javax.mail"/><category term="paintComponent"/><category term="pdf"/><category term="reindirizzare"/><category term="ricorsione"/><category term="sendredirect"/><category term="sha-256"/><category term="sicurezza"/><category term="stampa"/><category term=".class"/><category term=".doc"/><category term=".docx"/><category term=".java"/><category term=".xls"/><category term=".xlsx"/><category term="=="/><category term="AND"/><category term="ActionListener"/><category term="Apache FOP"/><category term="Autoboxing"/><category term="Bridge Java .Net"/><category term="Buffer"/><category term="BufferedReader"/><category term="Bug ThreadLocalRandom"/><category term="Bytecode"/><category term="Class Loader"/><category term="ClassCastException"/><category term="Collection"/><category term="Comparable"/><category term="Comparator"/><category term="Compress"/><category term="Custom Annotation"/><category term="Data Structure"/><category term="DecimalFormat"/><category term="Design Pattern"/><category term="Dispatcher"/><category term="Event"/><category term="EventListeners"/><category term="EventObject"/><category term="Eventi personalizzati"/><category term="Exception"/><category term="ExecutorService"/><category term="Executors"/><category term="FTP"/><category term="FeatureNotFoundException"/><category term="File edit"/><category term="FileFilter"/><category term="Forbidden reference"/><category term="Hello World"/><category term="HttpClient"/><category term="HttpComponents"/><category term="HttpParams"/><category term="HttpServletRequest"/><category term="IMAP"/><category term="IllegalStateException"/><category term="ImageWriter"/><category term="InetAddress"/><category term="InputStreamReader"/><category term="JAVA_OPT"/><category term="JBoss Application Server"/><category term="JButton"/><category term="JDK"/><category term="JFreeChart"/><category term="JIT"/><category term="JRE"/><category term="JRE System Libraries"/><category term="Java Development Kit"/><category term="Java Runtime Environment"/><category term="Java System Properties"/><category term="Java Virtual Machine"/><category term="Java metadata"/><category term="Jni4Net"/><category term="Just In Time"/><category term="List"/><category term="Mail"/><category term="Manifest"/><category term="Manifest.tf"/><category term="Map"/><category term="Math.ceil()"/><category term="Math.floor()"/><category term="Math.random()"/><category term="Math.rint()"/><category term="Math.round()"/><category term="Message"/><category term="MessageDialog"/><category term="Microsoft Excel"/><category term="Modificare file"/><category term="MultipartConfig"/><category term="NetworkInterface"/><category term="NumberFormat"/><category term="OR"/><category term="ObjectNotFoundException"/><category term="OutOfMemoryError"/><category term="Override"/><category term="POST"/><category term="Paradigmi"/><category term="PermGen space"/><category term="PermSize"/><category term="PreAuthorize"/><category term="PreAuthorize."/><category term="Proprietà di sistema"/><category term="Proprietà oggetti"/><category term="Proxy Java .Net"/><category term="Queue"/><category term="Referer"/><category term="Reflect"/><category term="Reindirizzare output"/><category term="Request"/><category term="ResponseFacade"/><category term="Runtime.exec()"/><category term="RuntimeException"/><category term="SMTP"/><category term="SMTPs"/><category term="SOA"/><category term="SXSSFSWorkbook"/><category term="Scanner"/><category term="ServletContext"/><category term="Set"/><category term="Shortcut"/><category term="Stack"/><category term="Standard output"/><category term="StandardPasswordEncoder"/><category term="SuppressedExceptions"/><category term="System.getProperty()"/><category term="System.in"/><category term="System.out.format()"/><category term="System.out.println"/><category term="System.setOut"/><category term="ThreadLocalRandom"/><category term="Throwable"/><category term="Unboxing"/><category term="UnsupportedClassVersionError"/><category term="WebServlet"/><category term="Xms"/><category term="Zip"/><category term="ZipOutputStream"/><category term="[Ljava.lang.String;"/><category term="access restrictions"/><category term="argomenti variabili"/><category term="array"/><category term="array di classi"/><category term="arrotondamento"/><category term="asterisco"/><category term="autenticazione"/><category term="binding dinamico"/><category term="bit a bit"/><category term="caching"/><category term="campi privati"/><category term="caratteristiche interfacce di rete"/><category term="caratteristiche java"/><category term="caricare icone"/><category term="casting"/><category term="casting automatico"/><category term="catalina.sh"/><category term="check versione Java Virtual Machine"/><category term="checked"/><category term="click"/><category term="close()"/><category term="comparatore"/><category term="compilatore"/><category term="configurare Path"/><category term="configurare percorsi"/><category term="configurazione"/><category term="configurazione Servlet"/><category term="configurazione xml"/><category term="configure build path"/><category term="controlli d&#39;accesso"/><category term="conversione binario decimale"/><category term="coordinate del puntatore"/><category term="corto circuito"/><category term="cos&#39;è java"/><category term="costruttori"/><category term="costruttori multipli"/><category term="crc32"/><category term="creare PDF"/><category term="crittografia"/><category term="data ultima modifica file"/><category term="decomprimere"/><category term="definizione"/><category term="diagrammi"/><category term="differenza comparator e comparable"/><category term="dimensione font"/><category term="direttive"/><category term="enigmi java"/><category term="enum"/><category term="enum type"/><category term="enumerations"/><category term="enumerazioni"/><category term="equals()"/><category term="errore forward JSP"/><category term="errore sendRedirect JSP"/><category term="errori di range"/><category term="errori di rappresentazione"/><category term="errori path"/><category term="esecuzione processo"/><category term="estrarre file zip"/><category term="feature not found"/><category term="fibonacci"/><category term="file"/><category term="file .zip"/><category term="file upload"/><category term="file word"/><category term="filtrare input"/><category term="finally"/><category term="fop"/><category term="forward"/><category term="getLocation()"/><category term="getPointerInfo()"/><category term="grafici"/><category term="iText"/><category term="ibrido"/><category term="image"/><category term="image scaling"/><category term="imaps"/><category term="import"/><category term="impostare in automatico variabili d&#39;ambiente"/><category term="include"/><category term="inclusione"/><category term="initial memory pool"/><category term="input"/><category term="interfacce di rete"/><category term="internal error"/><category term="interprete"/><category term="introspezione delle classi"/><category term="ispezionare"/><category term="java 7"/><category term="java.awt.Desktop"/><category term="java.awt.MouseInfo"/><category term="java.lang.Comparable;"/><category term="java.lang.IllegalAccessException"/><category term="java.lang.Math"/><category term="java.math.BigInteger"/><category term="java.text"/><category term="java.text.SimpleDateFormat"/><category term="java.util.Comparator"/><category term="java.util.Concurrent"/><category term="javac"/><category term="javax.swing.JFileChooser"/><category term="jpeg"/><category term="jpg"/><category term="jsp error"/><category term="jsp:include"/><category term="keyword"/><category term="lastModified()"/><category term="librerie"/><category term="listener"/><category term="listeners"/><category term="load"/><category term="logico"/><category term="major.minor version"/><category term="maximum memory pool"/><category term="md5"/><category term="memoria insufficiente"/><category term="message box"/><category term="metodi privati"/><category term="microsoft word"/><category term="modificare dimensione caratteri eclipse"/><category term="modificare output"/><category term="modificare porta default"/><category term="mouse"/><category term="multicatch"/><category term="multithreading"/><category term="numeri casuali"/><category term="numeri casuali Java7"/><category term="numeri in virgola"/><category term="numeri pseudo-randomici"/><category term="numeri random"/><category term="numero programmatori Java"/><category term="oggetti autochiudenti"/><category term="oggetti non misurabili"/><category term="one-way"/><category term="operatore di confronto"/><category term="operatori"/><category term="ordinamento"/><category term="org.apache.commons.lang.time.DateFormatUtils"/><category term="org.jfree.util.PublicCloneable error"/><category term="out of memory"/><category term="overloading"/><category term="overloading di costruttori"/><category term="overriding"/><category term="pagina di provenienza"/><category term="parametri"/><category term="parametri variabili"/><category term="parole chiave java"/><category term="parsing"/><category term="polimorfismo"/><category term="porta HTTP"/><category term="porta HTTPS"/><category term="process.waitFor()"/><category term="processo in stallo"/><category term="progetto"/><category term="programmazione parallela"/><category term="proxy"/><category term="punto esclamativo rosso"/><category term="regular expression"/><category term="request.getHeader()"/><category term="resize immagini"/><category term="restrictions"/><category term="return"/><category term="reverse proxy"/><category term="richiamare DLL da Java"/><category term="risorse autochiudenti"/><category term="runtime"/><category term="scatenare eventi java"/><category term="schedule"/><category term="scorciatoie da tastiera"/><category term="script batch"/><category term="scrittura file"/><category term="scrittura remota"/><category term="sec:authorize"/><category term="selezione File"/><category term="server.xml"/><category term="setCompressTempFiles(true)"/><category term="sha-1"/><category term="sha-384"/><category term="sha-512"/><category term="share NAS"/><category term="sort"/><category term="stampa su carta"/><category term="standard input"/><category term="statistiche"/><category term="statistiche Java"/><category term="stderr"/><category term="stdout"/><category term="svuotare buffer"/><category term="tastiera"/><category term="this()"/><category term="this(args)"/><category term="tipi primitivi Java"/><category term="tre puntini"/><category term="troncamento"/><category term="try/catch"/><category term="unchecked"/><category term="unzip"/><category term="utenti connessi"/><category term="utenti online"/><category term="validazione"/><category term="valore di ritorno"/><category term="varargs"/><category term="variabili d&#39;ambiente"/><category term="versione Java"/><category term="visualizzare immagini"/><category term="volatile"/><category term="web.xml"/><category term="wildcard"/><category term="xml"/><category term="xsl"/><category term="zippare"/><title type='text'>Domande e Risposte su Java</title><subtitle type='html'>Blog dedicato alla risoluzione delle più disparate problematiche che possono presentarsi ad un programmatore Java</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>106</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-5576042470768556242</id><published>2013-10-07T09:00:00.000+02:00</published><updated>2013-10-07T09:00:15.754+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="HttpClient"/><category scheme="http://www.blogger.com/atom/ns#" term="HttpComponents"/><category scheme="http://www.blogger.com/atom/ns#" term="HttpParams"/><category scheme="http://www.blogger.com/atom/ns#" term="Servlet"/><title type='text'>HttpComponents - Invocazione di una Servlet da un Client Java</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Posso invocare una servlet da un client Java?&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E&#39; possibile stabilire una connessione con un endpoint attraverso le famose API del framework&amp;nbsp;&lt;a href=&quot;http://hc.apache.org/httpclient-3.x/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;HttpClient&lt;/b&gt;&lt;/a&gt;. Purtroppo, come si può notare dalla seguente frase riportata nella home page ufficiale della libreria&lt;/span&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;i&gt;&lt;span style=&quot;background-color: white; font-family: Verdana, Helvetica, Arial, sans-serif; font-size: xx-small; line-height: 16.890625px; text-align: start;&quot;&gt;&quot;The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://hc.apache.org/&quot; style=&quot;background-color: white; color: blue; font-family: Verdana, Helvetica, Arial, sans-serif; font-size: small; line-height: 16.890625px; text-align: start;&quot;&gt;Apache HttpComponents&lt;/a&gt;&lt;span style=&quot;background-color: white; font-family: Verdana, Helvetica, Arial, sans-serif; font-size: xx-small; line-height: 16.890625px; text-align: start;&quot;&gt;&amp;nbsp;project &quot;&lt;/span&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;HttpClient è stato sostituito da &lt;a href=&quot;http://hc.apache.org/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;HttpComponents&lt;/b&gt;&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per garantire vita lunga al nostro software, in questo post descriveremo un esempio di utilizzo proprio utilizzando la nuova libreria &lt;b&gt;HttpComponents&lt;/b&gt; per eseguire una richiesta &lt;i&gt;POST &lt;/i&gt;verso una &lt;i&gt;Servlet&lt;/i&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Innanzitutto eseguiamo il download dei &lt;i&gt;jar &lt;/i&gt;di &lt;b&gt;HttpComponents &lt;/b&gt;dal link &lt;a href=&quot;http://hc.apache.org/downloads.cgi&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;HttpComponents v4.3&lt;/b&gt;&lt;/a&gt;.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Apriamo quindi Eclipse, creiamo un nuovo progetto Java e, prima di scrivere codice, aggiungiamo al &lt;i&gt;build path&lt;/i&gt; i &lt;i&gt;jar &lt;/i&gt;appena&amp;nbsp;scaricati.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo la classe &lt;b&gt;MyHttpClient&lt;/b&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.GregorianCalendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class MyHttpClient {
 
 private static final String PROTOCOL=&quot;http&quot;;
 private static final String HOST=&quot;localhost&quot;;
 private static final int PORT=8080;
 
 public static void main(String [] args){
  
  HttpClient httpclient = null;
  HttpPost httpPost = null;
  
  System.out.println(&quot;Preparo il metodo POST&quot;);
  URIBuilder builder = new URIBuilder();//Costruzione endpoint servlet con parametri
  builder.setScheme(PROTOCOL).setHost(HOST).setPort(PORT).setPath(&quot;/MyHttpServlet/MyServlet&quot;)
      .setParameter(&quot;name&quot;, MyHttpClient.class.getName())
      .setParameter(&quot;date&quot;, String.valueOf(GregorianCalendar.getInstance().getTimeInMillis()));
  
  try {
   URI uri = builder.build();
   httpPost = new HttpPost(uri);
   
   System.out.println(&quot;Instanzio un http client&quot;);
   httpclient = HttpClients.createDefault();

   System.out.println(&quot;Invoco la servlet: &quot;+httpPost.getURI());
   
   HttpResponse response = httpclient.execute(httpPost);
   HttpEntity entity = response.getEntity();
   
   System.out.println(&quot;Risposta: &quot;+EntityUtils.toString(entity));
  } catch (IOException e) {
   e.printStackTrace();
  } catch (URISyntaxException e) {
   e.printStackTrace();
  } finally {
            httpPost.releaseConnection();
        }
 }
}

&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La servlet &lt;b&gt;MyServlet &lt;/b&gt;può essere realizzata in questo modo:&lt;/span&gt; &lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(&quot;/MyServlet&quot;)
public class MyServlet extends HttpServlet {
 

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String name = request.getParameter(&quot;name&quot;);
  System.out.println(&quot;Ricevuto: &quot;+name);
  String dateParam = request.getParameter(&quot;date&quot;);
  
  long date = Long.parseLong(dateParam);
  
  response.setContentType(&quot;text/plain&quot;);
  PrintWriter out = response.getWriter();
  out.write(&quot;Ciao &quot;+name+&quot;\n&quot;);
  out.write(&quot;Richiesta ricevuta il: &quot;+new Date(date));
  
  out.flush();
  out.close();
 }

 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
 }

}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Avviando opportunamente un Servlet Container (ad esempio Tomcat) in cui verrà effettuato il &quot;deploy&quot; della nostra semplice servlet, il client &lt;b&gt;MyHttpClient &lt;/b&gt;potrà invocare &lt;b&gt;MyServlet &lt;/b&gt;in qualsiasi momento.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Infatti, dopo l&#39;avvio, l&#39;output di &lt;b&gt;MyHttpClient &lt;/b&gt;sarà:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div class=&quot;codice&quot;&gt;
Preparo il metodo POST&lt;br /&gt;
Instanzio un http client&lt;br /&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Invoco la servlet: http://localhost:8080/MyHttpServlet/MyServlet?name=MyHttpClient&amp;amp;date=1372361403328&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Risposta: Ciao MyHttpClient&lt;/div&gt;
Richiesta ricevuta il: Thu Oct 03 20:10:03 CEST 2013  &lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; text-align: justify;&quot;&gt;Mentre l&#39;output di&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif; text-align: justify;&quot;&gt;MyServlet&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; text-align: justify;&quot;&gt;sarà:&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;div class=&quot;codice&quot;&gt;
Ricevuto: MyHttpClient &lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Riferimenti&lt;/span&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;HttpComponents fundamentals&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://hc.apache.org/downloads.cgi&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;HttpComponents download&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://hc.apache.org/httpclient-3.x/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;HttpClient dismissed&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;GitHub&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/francescoerre/httpclient&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Http client&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/5576042470768556242/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/10/httpcomponents-invocazione-di-una.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5576042470768556242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5576042470768556242'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/10/httpcomponents-invocazione-di-una.html' title='HttpComponents - Invocazione di una Servlet da un Client Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-7321548189460124963</id><published>2013-07-02T09:00:00.000+02:00</published><updated>2013-09-10T16:09:32.506+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java Reflection"/><category scheme="http://www.blogger.com/atom/ns#" term="Proprietà oggetti"/><category scheme="http://www.blogger.com/atom/ns#" term="ricorsione"/><title type='text'>Recuperare programmaticamente i campi di un oggetto</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt; Come posso recuperare tutti i campi di un oggetto programmaticamente?&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per recuperare tutti gli attributi/campi di un oggetto è sufficiente usare la &lt;b&gt;Reflection &lt;/b&gt;di Java con un po&#39; di ricorsione.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Definiamo una classe &lt;b&gt;User&lt;/b&gt; con delle proprietà:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.objectfields.pojo;

import java.util.List;

public class User extends Person{
 
 private String mail;
 private String userid;
 private List&amp;lt;String&amp;gt; contacts;
 private List&amp;lt;User&amp;gt; friends;
 
 public User(){}
 
 public User(String name){
  super(name);
 }
 
 public String getMail() {
  return mail;
 }
 public void setMail(String mail) {
  this.mail = mail;
 }
 public String getUserid() {
  return userid;
 }
 public void setUserid(String userid) {
  this.userid = userid;
 }
 public List&amp;lt;String&amp;gt; getContacts() {
  return contacts;
 }
 public void setContacts(List&amp;lt;String&amp;gt; contacts) {
  this.contacts = contacts;
 }
 public List&amp;lt;User&amp;gt; getFriends() {
  return friends;
 }
 public void setFriends(List&amp;lt;User&amp;gt; friends) {
  this.friends = friends;
 }
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La classe&amp;nbsp;&lt;b&gt;User &lt;/b&gt;estende la classe &lt;b&gt;Person&lt;/b&gt; qui di seguito:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.objectfields.pojo;

public class Person {
 
 private String name;
 private String surname;
 private String age;
 
 public Person(){}
 
 public Person(String name) {
  this.name=name;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSurname() {
  return surname;
 }
 public void setSurname(String surname) {
  this.surname = surname;
 }
 public String getAge() {
  return age;
 }
 public void setAge(String age) {
  this.age = age;
 }

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Partiamo con un semplice algoritmo per recuperare i campi base dell&#39;oggetto &lt;b&gt;User&lt;/b&gt;; perfezioneremo poi l&#39;algoritmo per recuperare i campi ereditati e gli oggetti contenuti nelle liste.&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import lancill.objectfields.pojo.User;

public class PrintBaseObjectFields {

 public static void main(String[] args) {

  User user = getUser();
  printBaseUserField(user, &quot;user&quot;);
 }

 
 private static void printBaseUserField(Object object, String aFieldName) {

  String parentFieldName = aFieldName;

  try {
   //recupero tutti i campi della classe del mio ogetto
   Field[] classFields = object.getClass().getDeclaredFields();
 
   for (Field field : classFields) {
    //imposto il campo accessibile altrimenti JAva solleva una eccezione
    field.setAccessible(true);
 
    String fieldName = field.getName();
    Object value;
    try {
     //recupero il valore del campo per l&#39;istanzia della classe
     value = field.get(object);
     System.out.println(&quot;FIELD UTENTE: Nome attributo: &quot;
       + parentFieldName + &quot;.&quot; + fieldName + &quot;, Valore attributo: &quot;
       + value);
    } catch (Exception e) {
     System.out.println(&quot;Impossobile recuperare una proprieta&#39;&quot;);
     e.printStackTrace();
    }
 
   }
  } catch (Exception e) {
   System.out.println(&quot;Errore recupero field di &quot;+aFieldName);
   e.printStackTrace();
  }

 }
 
 private static User getUser() {
  User user = new User();
  user.setName(&quot;Peter&quot;);
  user.setSurname(&quot;Pan&quot;);
  user.setAge(&quot;12&quot;);
  user.setMail(&quot;peterpan@isolachenonce.net&quot;);
  user.setUserid(&quot;pp&quot;);
  
  List&amp;lt;String&amp;gt; contacts = new ArrayList&amp;lt;&amp;gt;();
  contacts.add(&quot;555010101&quot;);
  contacts.add(&quot;333000000&quot;);
  
  user.setContacts(contacts);
  
  List&amp;lt;User&amp;gt; friends = new ArrayList&amp;lt;User&amp;gt;();
  friends.add(new User(&quot;Campanellino&quot;));
  friends.add(new User(&quot;Mary&quot;));
  
  user.setFriends(friends);
  
  return user;
 }
 
}
&lt;/pre&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;output della classe &lt;b&gt;PrintBaseObjectFields&lt;/b&gt; sarà:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;
FIELD UTENTE: Nome attributo: user.mail, Valore attributo: peterpan@isolachenonce.net&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.userid, Valore attributo: pp&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.contacts, Valore attributo: [555010101, 333000000]&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends, Valore attributo: [lancill.objectfields.pojo.User@6fafc4c2, lancill.objectfields.pojo.User@7c9ed5d6]&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come potete notare, i campi ereditati (della classe &lt;b&gt;Person&lt;/b&gt;) non sono stati recuperati e le liste di oggetti complessi non sono stampate correttamente.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Perfezioniamo il nostro algoritmo per gestire anche i campi ereditati e le liste.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dovremo manipolare degli array, quindi per eseguire il prossimo algoritmo è necessario scaricare la libreria &lt;a href=&quot;http://commons.apache.org/proper/commons-lang/download_lang.cgi&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Apache Commons Lang&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo la classe &lt;b&gt;PrintAllObjectFields&lt;/b&gt; che utilizzerà il metodo&amp;nbsp;&lt;i&gt;print&lt;b&gt;All&lt;/b&gt;UserField&lt;/i&gt; (diversamente da quanto fatto prima con&amp;nbsp;&lt;i&gt;print&lt;b&gt;Base&lt;/b&gt;UserField&lt;/i&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;).&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il nuovo metodo questa volta fa uso della ricorsione per eseguire il parsing in profondità di tutti i tipi di campo complessi.&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;

import lancill.objectfields.pojo.User;

public class PrintAllObjectFields {

 public static void main(String[] args) {

  User user = getUser();
  printAllUserField(user, &quot;user&quot;);
 }

 private static void printAllUserField(Object object, String aFieldName) {

  
  String parentFieldName = aFieldName;

  try{
   
  
   Field[] classFields = object.getClass().getDeclaredFields();
 
   //definisco un identificatore delle mie classi per discriminare in seguito il tipo di parsing
   String PACKAGE = &quot;lancill.objectfields.pojo&quot;;
   
   Field [] superClassFields;
   //Creo un array con tutti i campi dell&#39;oogetto
   Field [] allClassFields = ArrayUtils.addAll(classFields);
   
   //Recupero i campi della classe Person
   Class superClass = object.getClass().getSuperclass();
   if(superClass.getCanonicalName().startsWith(PACKAGE)){
    superClassFields = superClass.getDeclaredFields();
    
    allClassFields = ArrayUtils.addAll(superClassFields, classFields);
   }
   
   
   for (Field field : allClassFields) {
    field.setAccessible(true);
 
    String fieldName = field.getName();
    Object value;
    try {
     //recupeo il tipo del campo
     String canonicalName =field.getType().getCanonicalName();
     
     //controllo se è un campo di un tipo primitivo Java
     if(!canonicalName.startsWith(&quot;java.util.List&quot;) &amp;amp;&amp;amp; !canonicalName.startsWith(PACKAGE)){
 
      value = field.get(object);
      System.out.println(&quot;FIELD UTENTE: Nome attributo: &quot;
        + parentFieldName + &quot;.&quot; + fieldName + &quot;, Valore attributo: &quot;
        + value);
     }else{
      /**
       * Eseguo il parsing di un campo complesso:
       * Liste
       * Classi del mio package
       **/
      Object complexObj = field.get(object);
      
      if(complexObj!=null){
       if(complexObj instanceof List){//GESTIONE LISTE
        System.out.println(&quot;FIELD UTENTE: START Attributo complesso:&quot;+parentFieldName+&quot;.&quot;+fieldName);
        //Recupeo l&#39;istanza della lista
        List&amp;lt;Object&amp;gt; list = (List&amp;lt;Object&amp;gt;)complexObj;
        int count=1;
        //Ciclo su tutti gli elementi della lista
        for(Object objInList : list){
         String objCanonicalName =objInList.getClass().getCanonicalName(); 
         if(!objCanonicalName.startsWith(PACKAGE)){//se il tipo dell&#39;oggetto non è complesso
          System.out.println(&quot;FIELD UTENTE: Nome attributo: &quot;
            + parentFieldName + &quot;.&quot; + fieldName +&quot;.[elemento-&quot;+count+&quot;], Valore attributo: &quot;
            + objInList);
         }else{
          //uso la ricorsione per eseguire gli stessi calcoli in profondita
          printAllUserField(objInList, parentFieldName+&quot;.&quot;+fieldName+&quot;.[elemento-&quot;+count+&quot;]&quot;);
         }
         count++;
        }
        System.out.println(&quot;FIELD UTENTE: END Attributo complesso:&quot;+fieldName);
        
       }else{//GESTIONE OGGETTO COMPLESSO DEL MIO PACKAGE
        System.out.println(&quot;FIELD UTENTE: START Attributo complesso:&quot;+parentFieldName+&quot;.&quot;+fieldName);
        //uso la ricorsione per eseguire gli stessi calcoli in profondita
        printAllUserField(complexObj, parentFieldName+&quot;.&quot;+fieldName);
        System.out.println(&quot;FIELD UTENTE: END Attributo complesso:&quot;+fieldName);
       }
      }
     }
    } catch (Exception e) {
     System.out.println(&quot;Impossobile recuperare una proprieta&#39;&quot;);
     e.printStackTrace();
    }
 
   }
  } catch (Exception e) {
   System.out.println(&quot;Errore recupero field di &quot;+aFieldName);
   e.printStackTrace();
  }

 }
 
 private static User getUser() {
  User user = new User();
  user.setName(&quot;Peter&quot;);
  user.setSurname(&quot;Pan&quot;);
  user.setAge(&quot;12&quot;);
  user.setMail(&quot;peterpan@isolachenonce.net&quot;);
  user.setUserid(&quot;pp&quot;);
  
  List&amp;lt;String&amp;gt; contacts = new ArrayList&amp;lt;&amp;gt;();
  contacts.add(&quot;555010101&quot;);
  contacts.add(&quot;333000000&quot;);
  
  user.setContacts(contacts);
  
  List&amp;lt;User&amp;gt; friends = new ArrayList&amp;lt;User&amp;gt;();
  friends.add(new User(&quot;Campanellino&quot;));
  friends.add(new User(&quot;Mary&quot;));
  
  user.setFriends(friends);
  
  return user;
 }
 
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;output della classe&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;PrintAllObjectFields&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;sarà:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;
FIELD UTENTE: Nome attributo: user.name, Valore attributo: Peter&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.surname, Valore attributo: Pan&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.age, Valore attributo: 12&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.mail, Valore attributo: peterpan@isolachenonce.net&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.userid, Valore attributo: pp&lt;br /&gt;
FIELD UTENTE: START Attributo complesso:user.contacts&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.contacts.[elemento-1], Valore attributo: 555010101&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.contacts.[elemento-2], Valore attributo: 333000000&lt;br /&gt;
FIELD UTENTE: END Attributo complesso:contacts&lt;br /&gt;
FIELD UTENTE: START Attributo complesso:user.friends&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-1].name, Valore attributo: Campanellino&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-1].surname, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-1].age, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-1].mail, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-1].userid, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-2].name, Valore attributo: Mary&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-2].surname, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-2].age, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-2].mail, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: Nome attributo: user.friends.[elemento-2].userid, Valore attributo: null&lt;br /&gt;
FIELD UTENTE: END Attributo complesso:friends&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;GitHub&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;https://github.com/francescoerre/lancill/tree/master/GetAllObjectFields&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;GetAllObjectFields Project&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Riferimenti&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://lancill.blogspot.it/2012/10/java-reflection-accesso-metodi-e-campi.html&quot;&gt;Java Reflection: accesso e metodi a campi privati&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/7321548189460124963/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/07/recuperare-programmaticamente-i-campi.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7321548189460124963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7321548189460124963'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/07/recuperare-programmaticamente-i-campi.html' title='Recuperare programmaticamente i campi di un oggetto'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-8554290715857888603</id><published>2013-05-30T17:52:00.000+02:00</published><updated>2013-05-30T17:52:00.048+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Annotations"/><category scheme="http://www.blogger.com/atom/ns#" term="Custom Annotation"/><category scheme="http://www.blogger.com/atom/ns#" term="Java metadata"/><title type='text'>Come definire e utilizzare a runtime delle custom Annotation</title><content type='html'>&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si definiscono delle Annotation custom per utilizzarle nel mio programma?&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Le &lt;b&gt;Annotation &lt;/b&gt;introdotte in &lt;b&gt;Java 5&lt;/b&gt; consentono di definire dei metadata per tre diversi livelli di &lt;i&gt;scoping&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;&lt;ol&gt;&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;SOURCE&lt;/b&gt; - Annotation disponibile &lt;u&gt;solo&lt;/u&gt; nello scope del sorgente e viene ignorato dal compilatore&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;CLASS&lt;/b&gt; - Annotation disponibile nello scope&amp;nbsp;dal compilatore in fase di compilazione, ma viene ignorata dalla Java Virtual Machine (JVM)&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;RUNTIME&lt;/b&gt; - Annotation disponibile nello scope&amp;nbsp;della JVM in modo che possa essere utilizzato da l&#39;ambiente di runtime&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Le &lt;b&gt;Annotation &lt;u&gt;NON&lt;/u&gt; &lt;/b&gt;aggiungono logica all&#39;applicazione e non sono uno strumento per programmare componenti. Sono invece in grado di fornire delle informazioni (metadati) per capire meglio il tipo di codice che stiamo eseguendo ed eventualmente intraprendere delle azioni o &lt;a href=&quot;http://lancill.blogspot.it/2012/07/servlet-30-webservlet.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;eseguire delle configurazioni&lt;/b&gt;&lt;/a&gt;).&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In questo post ci concentreremo sulle &lt;b&gt;Annotation&lt;/b&gt; di tipo &lt;u&gt;RUNTIME&lt;/u&gt; per riuscire ad eseguire delle azioni durante l&#39;esecuzione dell&#39;applicazione.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Innanzitutto realizziamo un file di properties e lo chiamiamo (ad esempio) &lt;b&gt;city.properties&lt;/b&gt;. Il contenuto sarà semplice:&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;border: 1px solid; text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;name=Roma&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;country=Italia&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Adesso realizziamo un bean &lt;b&gt;City&lt;/b&gt;&amp;nbsp;che legge delle informazioni dal file di properties appena creato:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush: java&quot;&gt;package lancill.test.pojo;

import java.io.FileInputStream;
import java.util.Properties;
import lancill.annotation.ReadPropertiesAnnotation;

public class City {
 
 private Properties props = new Properties();
 private final String PROPERTIES_FILE_NAME = &quot;city.properties&quot;;
 public City(){
  try {
   props.load(new FileInputStream(PROPERTIES_FILE_NAME));
  } catch (Exception e) {
   props.setProperty(&quot;name&quot;,&quot;N/A&quot;);
   props.setProperty(&quot;country&quot;,&quot;N/A&quot;);
   e.printStackTrace();
  }
 }
 
 public String getName(){
  return props.getProperty(&quot;name&quot;);
 }
 
 public String getCountry(){
  return props.getProperty(&quot;country&quot;);
 }
}

&lt;/pre&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il prossimo passo è realizzare una &lt;b&gt;Annotation custom&lt;/b&gt; che ci consentirà di capire qual è il file di properties su cui si basa il bean &lt;b&gt;City&lt;/b&gt; e quali sono i valori delle property che utilizzerà il bean a runtime.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Definiamo la nostra &lt;i&gt;Annotation&lt;/i&gt;&amp;nbsp;&lt;b&gt;ReadPropertiesAnnotation &lt;/b&gt;nel seguente modo:&lt;/span&gt; &lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReadPropertiesAnnotation {
 
 String fileName();
 String propertyName() default &quot;N/A&quot;;

}&lt;/pre&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Vediamo quali sono le parole chiave che descrivono l&#39;&lt;i&gt;Annotation&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;/div&gt;&lt;ul&gt;&lt;li&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;@interface &lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;consente di definire la classe &lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;ReadPropertiesAnnotation&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; di tipo &lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Annotation&lt;/i&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@Target&lt;/b&gt; indica che l&#39;&lt;i&gt;Annotation &lt;/i&gt;che stiamo definendo può essere applicata solo ai metodi&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@Retention &lt;/b&gt;definisce il livello di scope (in questo RUNTIME)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;blockquote class=&quot;tr_bq&quot;&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;la @Retention deve essere obbligatoriamente RUNTIME se vogliamo che le informazioni della nostra &lt;i&gt;Annotation&lt;/i&gt; siano &amp;nbsp;disponibili durante l&#39;esecuzione dell&#39;applicazione&lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/blockquote&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Applichiamo ora la nostra Annotation ai metodi del bean &lt;b&gt;City&lt;/b&gt;:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.test.pojo;

import java.io.FileInputStream;
import java.util.Properties;
import lancill.annotation.ReadPropertiesAnnotation;

public class City {
 
 private Properties props = new Properties();
 private final String PROPERTIES_FILE_NAME = &quot;city.properties&quot;;
 public City(){
  try {
   props.load(new FileInputStream(PROPERTIES_FILE_NAME));
  } catch (Exception e) {
   props.setProperty(&quot;name&quot;,&quot;N/A&quot;);
   props.setProperty(&quot;country&quot;,&quot;N/A&quot;);
   e.printStackTrace();
  }
 }
 
 @ReadPropertiesAnnotation (fileName=PROPERTIES_FILE_NAME, propertyName=&quot;name&quot;)
 public String getName(){
  return props.getProperty(&quot;name&quot;);
 }
 
 @ReadPropertiesAnnotation (fileName=PROPERTIES_FILE_NAME, propertyName=&quot;country&quot;)
 public String getCountry(){
  return props.getProperty(&quot;country&quot;);
 }
}

&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;A questo punto dobbiamo definire una classe che sia in grado di recuperare le informazioni delle nostra Annotation e interpretarle correttamente.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Definiamo quindi la classe &lt;b&gt;AnnotationAnalyzer &lt;/b&gt;che, attraverso il metodo analyze(),&amp;nbsp;recupera il bean &lt;b&gt;City &lt;/b&gt;e i relativi metodi con le annotation di tipo ReadPropertiesAnnotation.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Una volta individuati i metodi, carica l&#39;annotation ed esegue il &lt;i&gt;parsing &lt;/i&gt;delle informazioni:&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.annotation.parser;

import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.Properties;
import lancill.annotation.ReadPropertiesAnnotation;

public class AnnotationAnalyzer {
    
 public void analyze() {

  try {
   Class annotationClass = AnnotationAnalyzer.class.getClassLoader()
     .loadClass((&quot;lancill.test.pojo.City&quot;));
   
   for (Method method : annotationClass.getMethods()) {
    if (method
      .isAnnotationPresent(lancill.annotation.ReadPropertiesAnnotation.class)) {

     try {
      ReadPropertiesAnnotation metodoAnnotation = method
        .getAnnotation(ReadPropertiesAnnotation.class);

      if (!metodoAnnotation.fileName().isEmpty()) {
       System.out.println(&quot;Annotation @ReadPropertiesAnnotation:\n&quot;);
       System.out.println(&quot;\t- Metodo: &quot;+ method);
       
       System.out.println(&quot;\t- Nome file di properties: &quot;+ metodoAnnotation.fileName());
       System.out.println(&quot;\t- Property associata al metodo: &quot;+  metodoAnnotation.propertyName());
       System.out.println(&quot;\t- Valore restituito dal metodo: &quot;+ getPropertyValue(metodoAnnotation.fileName(),metodoAnnotation.propertyName())+&quot;\n&quot;);
      }

     } catch (Throwable ex) {
      ex.printStackTrace();
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private String getPropertyValue(String fileName, String propertyName) {
  Properties props=new Properties();
  
  try {
   props.load(new FileInputStream(fileName));
   return props.getProperty(propertyName);
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  return &quot;Impossibile calcolare il valore&quot;;
 }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Possiamo ora definire una semplice applicazione che lancia il metodo analyze():&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package lancill.test;

import lancill.annotation.parser.AnnotationAnalyzer;

public class TestAnnotation {

 public static void main(String[] args) {
  AnnotationAnalyzer aa = new AnnotationAnalyzer();
  aa.analyze();
 }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; sarà il seguente:&lt;/span&gt;  &lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;Annotation @ReadPropertiesAnnotation:&lt;br /&gt;
&lt;blockquote&gt;- Metodo: public java.lang.String lancill.test.pojo.City.getName()&lt;br /&gt;
- Nome file di properties: city.properties&lt;br /&gt;
- Property associata al metodo: name&lt;br /&gt;
- Valore restituito dal metodo: Roma&lt;/blockquote&gt;Annotation @ReadPropertiesAnnotation:&lt;br /&gt;
&lt;blockquote&gt;- Metodo: public java.lang.String lancill.test.pojo.City.getCountry()&lt;br /&gt;
- Nome file di properties: city.properties&lt;br /&gt;
- Property associata al metodo: country&lt;br /&gt;
- Valore restituito dal metodo: Italia&lt;/blockquote&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Riferimenti&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://docs.oracle.com/javase/tutorial/java/annotations/index.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Oracle Lessons: Annotation&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://www.javacodegeeks.com/2012/11/java-annotations-tutorial-with-custom-annotation.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Java Annotations Tutorial with Custom Annotation&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/8554290715857888603/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/come-definire-e-utilizzare-runtime.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8554290715857888603'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8554290715857888603'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/come-definire-e-utilizzare-runtime.html' title='Come definire e utilizzare a runtime delle custom Annotation'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-6757784694600661474</id><published>2013-05-27T09:00:00.000+02:00</published><updated>2013-05-27T09:00:03.746+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Tomcat"/><category scheme="http://www.blogger.com/atom/ns#" term="Application Scope"/><category scheme="http://www.blogger.com/atom/ns#" term="Servlet"/><category scheme="http://www.blogger.com/atom/ns#" term="ServletContext"/><category scheme="http://www.blogger.com/atom/ns#" term="Tomcat"/><title type='text'>Abilitazione Cross Application Scope in Tomcat per condivisione dati tra webapp</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso condividere dati tra diverse webapp utilizzando l&#39;Application Scope?&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Di default l&#39;&lt;b&gt;Application Scope&lt;/b&gt; è utilizzato per memorizzare oggetti a livello di web application. Questo livello di scope è il più alto rispetto ai classici scope di &lt;i&gt;Request &lt;/i&gt;e &lt;i&gt;Session&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;table border=&quot;2&quot; cellpadding=&quot;2&quot; cellspacing=&quot;2&quot; style=&quot;background-color: white; border: 1px solid rgb(153, 153, 153); color: #333333; font-family: &#39;Lucida Grande&#39;, Arial, Helvetica, sans-serif; font-size: 13px; margin-bottom: 2em;&quot;&gt;&lt;colgroup&gt;&lt;col width=&quot;20%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;30%&quot;&gt;&lt;/col&gt;&lt;col width=&quot;50%&quot;&gt;&lt;/col&gt;&lt;/colgroup&gt;&lt;tbody&gt;
&lt;tr valign=&quot;TOP&quot;&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Nome&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Scope&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Durata&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;TOP&quot;&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Request&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Singola richiesta, verso il server&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Fino a quando la risposta non viene inviata al client&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;TOP&quot;&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Session&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Tutte le richieste inviate dallo stesso browser&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Fino a quando non scade il timeout della sessione o l&#39;ID di sessione non viene invalidato (ad es. chiusura del browser)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;tr valign=&quot;TOP&quot;&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Application&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Tutte le richieste inviate a una stessa Web Application&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;td colspan=&quot;1&quot; rowspan=&quot;1&quot; style=&quot;border: 1px solid rgb(153, 153, 153); padding: 0.5em; vertical-align: top;&quot; valign=&quot;TOP&quot;&gt;&lt;div style=&quot;line-height: 1.5em; margin-bottom: 1em; padding: 0px;&quot;&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Container&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per poter recuperare un oggetto memorizzato a livello di Application Scope è necessario utilizzare il &lt;b&gt;ServletContext&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Se vogliamo recuperare un oggetto memorizzato nell&#39;Application Scope della &lt;b&gt;WebAppA &lt;/b&gt;dalla &lt;b&gt;WebAppB &lt;/b&gt;è necessario fare attenzione ai seguenti aspetti:&lt;/span&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Abilitare il &lt;i&gt;crossContext &lt;/i&gt;nel file &lt;b&gt;context.xml&lt;/b&gt; presente nella cartella &lt;b&gt;config &lt;/b&gt;di Tomcat:&lt;/span&gt;&lt;/li&gt;
&lt;pre class=&quot;brush: xml; highlight:1&quot;&gt;&amp;lt;context crosscontext=&quot;true&quot;&amp;gt;

    ...

&amp;lt;/context&amp;gt;&lt;/pre&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Recuperare dalla &lt;b&gt;WebAppA&lt;/b&gt; il &lt;i&gt;ServletContext &lt;/i&gt;della &lt;b&gt;WebAppB&lt;/b&gt; in questo modo:&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(&quot;/ServletWebAppA/CrossApplicationScopeServlet&quot;)
public class CrossApplicationScopeServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
  ServletContext sc = request.getServletContext().getContext(&quot;/WebAppB&quot;);
  
  String valore = (String)sc.getAttribute(&quot;CHIAVE&quot;);
  
 }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Quando si utilizzano dati condivisi, bisogna stare attenti ai possibili errori di ClassCastException. Potete consultare il post &lt;a href=&quot;http://lancill.blogspot.it/2013/05/classcastexception-con-oggetto-in.html&quot;&gt;&lt;b&gt;ClassCastException con oggetto in ClassLoader diversi&lt;/b&gt;&lt;/a&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;per prendere le giuste precauzioni.&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/6757784694600661474/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/abilitazione-cross-application-scope-in.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6757784694600661474'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6757784694600661474'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/abilitazione-cross-application-scope-in.html' title='Abilitazione Cross Application Scope in Tomcat per condivisione dati tra webapp'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-9221278920762408681</id><published>2013-05-16T09:00:00.000+02:00</published><updated>2013-05-16T09:00:08.552+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="fibonacci"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="ricorsione"/><title type='text'>La ricorsione in Java</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Che cos&#39;è la ricorsione?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La &lt;b&gt;ricorsione &lt;/b&gt;è forse una delle prime cose che si studiano in programmazione e, diciamoci la verità, non è così intuitiva per chi è alle prime armi. A cosa serve la &lt;b&gt;ricorsione&lt;/b&gt;? A creare dei cicli (o chiamate cicliche) in maniera alternativa al classico for. In questo post cerchiamo di semplificarne il concetto utilizzando Java, ma è praticamente uguale in tutti i linguaggi di programmazione.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo subito un &lt;b&gt;esempio &lt;/b&gt;a scopo puramente didattico:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package ricorsione;

public class RicorsioneTest {

 public static void main(String[] args) {

  int variabile = metodoRicorsivo(); 
  
 }
 
 public static int metodoRicorsivo(){
  return metodoRicorsivo();
 }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Cosa succede eseguendo questo frammento di codice? Innanzitutto&amp;nbsp;&lt;u&gt;sconsiglio&lt;/u&gt; di eseguirlo dato che sarà un&#39;inconclusiva &lt;i&gt;Exception &lt;/i&gt;a &lt;i&gt;Runtime &lt;/i&gt;(&lt;i&gt;java.lang.StackOverflowErrorloop&lt;/i&gt;)! In pratica l&#39;unica riga di codice del &lt;i&gt;main &lt;/i&gt;richiama un metodo che richiama se stesso che a sua volta richiama nuovamente se stesso e così via all&#39;infinito (in questo caso)!&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Anche se in modo errato, abbiamo già realizzato una &lt;b&gt;ricorsione&lt;/b&gt;, in parole povere un &quot;qualcosa&quot; che richiama se stessa.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come rendere utile (e soprattutto funzionante) questo concetto? Guardiamo subito l&#39;esempio &lt;b&gt;RicorsioneTest&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package ricorsione;

public class RicorsioneTest {

 public static void main(String[] args) {

  metodoRicorsivo(10); 
  
 }
 
 public static int metodoRicorsivo(int numero){
  System.out.println(&quot;Numero valutato: &quot; +numero);
  numero--;  
  if(numero == 1)  
   return 1;  
    
  return metodoRicorsivo(numero);
 }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; in questo caso è finito e corretto:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 10&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 9&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 8&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 7&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 6&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 5&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 4&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 3&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Numero valutato: 2&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Cosa è cambiato? &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Abbiamo inserito una &lt;b&gt;condizione base&lt;/b&gt; (numero == 1) che permette al metodo di terminare man mano che le chiamate si annidano l&#39;una con l&#39;altra e, soprattutto, abbiamo aggiunto &lt;b&gt;una variante&lt;/b&gt; che modifica il valore della variabile ad ogni chiamata (numero--). Queste due condizioni garantiscono che arriveremo (prima o poi) ad una base in cui il metodo non richiamerà più se stesso e comincerà la risalita e la restituzione dei valori al chiamante. &lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per chiarire i dubbi sulla ricorsione guardiamo un esempio leggermente più complesso. Il classico &lt;b&gt;&lt;a href=&quot;http://it.wikipedia.org/wiki/Successione_di_Fibonacci&quot; target=&quot;_blank&quot;&gt;algoritmo di Fibonacci&lt;/a&gt;&lt;/b&gt;. L&#39;algoritmo seguente calcola il numero della sequenza di fibonacci nella posizione i-esima:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package ricorsione; 

public class FibonacciRicorsivo {

  public static long fibonacci(long i) {
    if(i == 0) 
      return 0;
  
    else if(i == 1) 
      return 1;
  
    else 
      return fibonacci(i-1) + fibonacci(i-2);
  }

  public static void main(String[] args){
    long i = 4; //numero su cui calcolare la funzione di fibonacci
    System.out.println(&quot;INIZIO CALCOLO DELLA FUNZIONE DI FIBONACCI(&quot;+i+&quot;)&quot;);
    System.out.print(&quot;RISULTATO = &quot; + fibonacci(i));
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt;&amp;nbsp;sarà banalmente il numero della sequenza di Fibonacci indicato dall&#39;indice i (nell&#39;esempio &quot;4&quot;):&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
INIZIO CALCOLO DELLA FUNZIONE DI FIBONACCI(4):&lt;br /&gt;
RISULTATO: 3&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In questo caso (molto semplice) la sequenza di chiamate è questa:&lt;/span&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYNtqahJCdKVRxVhGRDjhZKCanAGS_GRZpHxrJWff4tauQcGPzbwjyYmN_IG_Iwp9JnPYm3TFiMlJWjWEjUoQScekRSpnANDsskXAlrYiCTeFZ0bqSRS_81IOhb7Zn2bGyeIBhW0_FqZNd/s1600/fib(4)-1.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;77&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYNtqahJCdKVRxVhGRDjhZKCanAGS_GRZpHxrJWff4tauQcGPzbwjyYmN_IG_Iwp9JnPYm3TFiMlJWjWEjUoQScekRSpnANDsskXAlrYiCTeFZ0bqSRS_81IOhb7Zn2bGyeIBhW0_FqZNd/s320/fib(4)-1.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Prima scomposizione di fib(4)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoAtLGqK4-CZoiOFxDNMOwEg5uylpXEGBo8OfWfgXP6ntimjIbMhRPJqRQtC0YMtJqymlFORxEEbM4YypqyUGdOPM_JN8u4ETqyzDMQm7e60Dwiz-nnVhbJu0jnwRDRgILY0Ly5WV-7_i1/s1600/fib(4)-2.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;105&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoAtLGqK4-CZoiOFxDNMOwEg5uylpXEGBo8OfWfgXP6ntimjIbMhRPJqRQtC0YMtJqymlFORxEEbM4YypqyUGdOPM_JN8u4ETqyzDMQm7e60Dwiz-nnVhbJu0jnwRDRgILY0Ly5WV-7_i1/s320/fib(4)-2.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Seconda scomposizione di fib(3);&lt;br /&gt;
fib(1) termina le sottochiamate in quanto caso base&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNA-E0Q0-oOr1qLzNreKLri_DnYVzoSK9gM7CjkZa_QhnqqW-NSxr413cLV8i8m6jOwv0oc3p5h491-oPvtS75cqVgVLUSxyOuyPKzn7-8TI6Mhg8I-BPLp8TmumJs7G02hjQ1Xd2y-iWp/s1600/fib(4)-3.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;187&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhNA-E0Q0-oOr1qLzNreKLri_DnYVzoSK9gM7CjkZa_QhnqqW-NSxr413cLV8i8m6jOwv0oc3p5h491-oPvtS75cqVgVLUSxyOuyPKzn7-8TI6Mhg8I-BPLp8TmumJs7G02hjQ1Xd2y-iWp/s320/fib(4)-3.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Terza scomposizione di fib(2), fib(1) e fib(0) sono casi base&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMknaPbeeZR-pENnxDFvnO4bushgcs2SMrQjI-8-vBP7xSPLr8p7mvJP0Ix9cKaze7WXGsxJaNTZ8wncz1B4CH1xoXpIUl8zfzho5_o9I3a5KM2af841rujLhID0FrxMcrAecSw3EcBBR/s1600/fib(4)-4.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;184&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgUMknaPbeeZR-pENnxDFvnO4bushgcs2SMrQjI-8-vBP7xSPLr8p7mvJP0Ix9cKaze7WXGsxJaNTZ8wncz1B4CH1xoXpIUl8zfzho5_o9I3a5KM2af841rujLhID0FrxMcrAecSw3EcBBR/s320/fib(4)-4.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Terminate le chiamate del ramo, comincia la risalita&lt;br /&gt;
e la restituzione dei risultati, qui a fib(2)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq9N082QAWuzSTL1anGJ8yxGKC16zgX3R3odL2ERNU6UFDwu_-WH1ak3rm59soWxt-7MZd1DPK60bOviFmGJrCvbEXFxPXlTNTr3c5TIIvPNbJnoTpH0QsgEpoB6OZP-1yE5lko0JSaMpT/s1600/fib(4)-5.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;185&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgq9N082QAWuzSTL1anGJ8yxGKC16zgX3R3odL2ERNU6UFDwu_-WH1ak3rm59soWxt-7MZd1DPK60bOviFmGJrCvbEXFxPXlTNTr3c5TIIvPNbJnoTpH0QsgEpoB6OZP-1yE5lko0JSaMpT/s320/fib(4)-5.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Restituzione a fib(3)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWJAVFxuS8Dq-BIJ-TYSrr6KO08T1z2KFaZ6j4KTxEfmsuknytCqZm7LcjZSvW6EjokuqAoDCO808BDRCsAJO1gpV66vArujZMEGKDLz8kqvypN1gk14hhBb6_4SR4nyaE1VB7Jdhq1pl6/s1600/fib(4)-6.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;176&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWJAVFxuS8Dq-BIJ-TYSrr6KO08T1z2KFaZ6j4KTxEfmsuknytCqZm7LcjZSvW6EjokuqAoDCO808BDRCsAJO1gpV66vArujZMEGKDLz8kqvypN1gk14hhBb6_4SR4nyaE1VB7Jdhq1pl6/s320/fib(4)-6.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Terminata la restituzione del primo ramo,&lt;br /&gt;
cominciano le chiamate al secondo ramo&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhS5gDza-k9SwDaEJGaF4x8QI1oOk8FRkFdxyBYBjxajdgsiZNOe5ZlHPfwqe9IdXuWR2duoG6lRgTQPazhVHiO-uzZayu3PTQm8ZurGahYsj3x4Prjb2Q-dmOMN3kuNU3eOIAJFiLk7Da-/s1600/fib(4)-7.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;181&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhS5gDza-k9SwDaEJGaF4x8QI1oOk8FRkFdxyBYBjxajdgsiZNOe5ZlHPfwqe9IdXuWR2duoG6lRgTQPazhVHiO-uzZayu3PTQm8ZurGahYsj3x4Prjb2Q-dmOMN3kuNU3eOIAJFiLk7Da-/s320/fib(4)-7.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Come nel primo caso, terminate le chiamate, si restituiscono i risultati&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHhocOS-NfrUv5Ayr-0-NCw4JCUHuBMpMc0I2kRrEbilXlhkNGKZIM-Uunf54nwoU9hc6LcqdJIjdqxFHumJIGviG6Bv6xYrJgmAuuocyHoLjBTUT43piZtk8SU9_I-ZwM6aXGSElyoByr/s1600/fib(4)-8.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;174&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHhocOS-NfrUv5Ayr-0-NCw4JCUHuBMpMc0I2kRrEbilXlhkNGKZIM-Uunf54nwoU9hc6LcqdJIjdqxFHumJIGviG6Bv6xYrJgmAuuocyHoLjBTUT43piZtk8SU9_I-ZwM6aXGSElyoByr/s320/fib(4)-8.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Infine l&#39;ultima somma restituisce il risultato finale (esatto)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Nell&#39;utilizzare la &lt;b&gt;ricorsione &lt;/b&gt;è necessario sapere che in genere è molto più &lt;b&gt;pesante &lt;/b&gt;in termini di elaborazione rispetto alla normale iterazione. Infatti, in algoritmi come questo il numero di chiamate sale esponenzialmente: guardiamo lo schema finale delle chiamate se provassimo a calcolare il 5° numero della sequenza di fibonacci (nella sequenza precedente era &lt;i&gt;fibonacci(4)&lt;/i&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUB_8X-wOnBKKdBcr0UltyMKDU-J-p8mxoma3svmLpzpDSEKPJg9qOTYael1sn3e1a2T56SjPjQPL1AlFe8oTw4bVQ6YFxls3IWrgfhOWZGZOAH9SUwLapNYSWgalarQ_4J06hOJpIxyif/s1600/fibonacci(5).PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;297&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhUB_8X-wOnBKKdBcr0UltyMKDU-J-p8mxoma3svmLpzpDSEKPJg9qOTYael1sn3e1a2T56SjPjQPL1AlFe8oTw4bVQ6YFxls3IWrgfhOWZGZOAH9SUwLapNYSWgalarQ_4J06hOJpIxyif/s400/fibonacci(5).PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;In arancione ci sono i 2 casi base che restituiscono 0 o 1&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;e tra parentesi la somma parziale fino a quel momento&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Immaginiamo cosa accadrebbe per numeri più grandi.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In sintesi l&#39;utilità della &lt;b&gt;ricorsione &lt;/b&gt;è di consentire la scrittura di algoritmi di una certa complessità in maniera più semplice (per determinate categorie di problemi) ed elegante. Di contro, la sua dispendiosità in termini di risorse ne limita fortemente il campo di applicazione.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/9221278920762408681/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/la-ricorsione-in-java.html#comment-form' title='4 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/9221278920762408681'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/9221278920762408681'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/la-ricorsione-in-java.html' title='La ricorsione in Java'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjYNtqahJCdKVRxVhGRDjhZKCanAGS_GRZpHxrJWff4tauQcGPzbwjyYmN_IG_Iwp9JnPYm3TFiMlJWjWEjUoQScekRSpnANDsskXAlrYiCTeFZ0bqSRS_81IOhb7Zn2bGyeIBhW0_FqZNd/s72-c/fib(4)-1.PNG" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-3929635373572979957</id><published>2013-05-09T09:00:00.000+02:00</published><updated>2013-05-09T09:00:17.989+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java EE 7"/><category scheme="http://www.blogger.com/atom/ns#" term="JAX-RS"/><category scheme="http://www.blogger.com/atom/ns#" term="Jersey"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><category scheme="http://www.blogger.com/atom/ns#" term="RESTful"/><title type='text'>Realizzare un servizio REST con JAX-RS 2.0 e Jersey 2.0</title><content type='html'>&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come realizzo un servizio REST 2.0 e un client per testarlo?&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per addentrarsi nel mondo &lt;b&gt;REST&lt;/b&gt; consiglio di leggere il post &lt;a href=&quot;http://lancill.blogspot.com/2013/05/rest-20-definizione-e-configurazione.html&quot;&gt;&lt;b&gt;REST 2.0: Definizione e configurazione con il framework Jersey&lt;/b&gt;&lt;/a&gt;. Il post spiega brevemente i concetti &lt;b&gt;REST &lt;/b&gt;e cosa occorre per configurare un ambiente che lo utilizzi. La lettura servirà anche per conoscere quali sono i &lt;i&gt;jar&lt;/i&gt; da includere nei progetti che stiamo per realizzare.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In questo post creeremo un Servizio &lt;b&gt;REST&lt;/b&gt;&amp;nbsp;che risponderà, con un semplice &lt;i&gt;&quot;echo&quot;,&lt;/i&gt; alle richieste di un Client.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Partiamo con la programmazione del Servizio.&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo un nuovo &lt;b&gt;Dynamic Web Project&lt;/b&gt; in &lt;i&gt;Eclipse&lt;/i&gt; denominato &lt;i&gt;RESTfulService&lt;/i&gt; e ricordiamoci di selezionare, nell&#39;ultimo step del wizard, di creazione la checkbox &quot;&lt;i&gt;Generate web.xml deployment descriptor&lt;/i&gt;&quot;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Questa opzione ci permetterà di avere a disposizione un web.xml di base creato da Eclipse da modificare secondo le nostre preferenze (ovviamente non è un passo obbligatorio, ma di sicuro ci permette di risparmiare tempo e fatica).&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dividiamo la costruzione del servizio in due parti:&lt;/span&gt;&lt;/div&gt;&lt;ol&gt;&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Configurazione&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creazione del servizio di Echo&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;1 - Configurazione.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per prima cosa dobbiamo personalizzare il &lt;i&gt;web.xml&lt;/i&gt;&amp;nbsp;appena creato da Eclipse in questo modo:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;pre class=&quot;brush:xml; highlight:8&quot;&gt;&amp;lt;web-app version=&quot;3.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xsi:schemalocation=&quot;http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&quot;&amp;gt;
  &amp;lt;servlet&amp;gt;
  &amp;lt;servlet-name&amp;gt;JerseyWeb&amp;lt;/servlet-name&amp;gt;
  &amp;lt;servlet-class&amp;gt;org.glassfish.jersey.servlet.ServletContainer&amp;lt;/servlet-class&amp;gt;
  &amp;lt;init-param&amp;gt;
   &amp;lt;param-name&amp;gt;javax.ws.rs.Application&amp;lt;/param-name&amp;gt;
   &amp;lt;param-value&amp;gt;echo.application.EchoApplication&amp;lt;/param-value&amp;gt;
  &amp;lt;/init-param&amp;gt;
  &amp;lt;init-param&amp;gt;
   &amp;lt;param-name&amp;gt;jersey.config.xml.security.disable&amp;lt;/param-name&amp;gt;
   &amp;lt;param-value&amp;gt;true&amp;lt;/param-value&amp;gt;
  &amp;lt;/init-param&amp;gt;
  &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;
 &amp;lt;/servlet&amp;gt;

 &amp;lt;servlet-mapping&amp;gt;
  &amp;lt;servlet-name&amp;gt;JerseyWeb&amp;lt;/servlet-name&amp;gt;
  &amp;lt;url-pattern&amp;gt;/*&amp;lt;/url-pattern&amp;gt;
 &amp;lt;/servlet-mapping&amp;gt;
  
 &amp;lt;/web-app&amp;gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo ora la classe&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;echo.application.EchoApplication&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;referenziata nella riga in evidenza del&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;web.xml.&lt;/i&gt;&lt;/div&gt;&lt;div style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La classe&amp;nbsp;&lt;b&gt;EchoApplication&lt;/b&gt;&amp;nbsp;(necessaria per configurare il gestore della nostra architettura REST).&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:java&quot; style=&quot;text-align: start;&quot;&gt;package echo.application;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import echo.resource.EchoResource;

public class EchoApplication extends Application {

     @Override
     public Set&amp;lt;Class&amp;lt;?&amp;gt;&amp;gt; getClasses() {
         Set&amp;lt;Class&amp;lt;?&amp;gt;&amp;gt; classes = new HashSet&amp;lt;Class&amp;lt;?&amp;gt;&amp;gt;();
         classes.add(EchoResource.class);
         return classes;
     }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;2 - Servizio REST:&amp;nbsp;&lt;b&gt;echo.resource.EchoResource&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:java&quot; style=&quot;text-align: start;&quot;&gt;package echo.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@Path(&quot;/echo&quot;)
public class EchoResource {
  @GET
  @Produces(&quot;text/plain&quot;)
  public String echo(@QueryParam(&quot;echo_msg&quot;) String echoString) {
    return echoString;
  }   
}
&lt;/pre&gt;&lt;div style=&quot;text-align: start;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come potete notare, la classe &lt;b&gt;EchoResource&lt;/b&gt; fa un forte uso delle &lt;i&gt;Annotation.&lt;/i&gt; Vediamole nel dettaglio:&lt;/span&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: start;&quot;&gt;&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@Path&lt;/b&gt;&amp;nbsp;definisce l&#39;&lt;i&gt;endpoint&lt;/i&gt; della nostra risorsa (o servizio). Questo &lt;i&gt;url&lt;/i&gt; insieme al &lt;i&gt;context path&lt;/i&gt; della nostra web application forma l&#39;endpoint completo (nel nostro esempio http://localhost:8080/RESTfulService&lt;b&gt;/echo&lt;/b&gt;). Grazie a questo endpoint, il servizio sarà invocabile a quell&#39;indirizzo dal client.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@GET&amp;nbsp;&lt;/b&gt;indica quale metodo del protocollo &lt;i&gt;HTTP&lt;/i&gt; sarà utilizzato per invocare il metodo&lt;i&gt;&amp;nbsp;echo&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@Produces&lt;/b&gt;&amp;nbsp;indica il &lt;i&gt;content-type&lt;/i&gt; della risposta (da notare la &lt;i&gt;s&lt;/i&gt;&amp;nbsp;della terza persona del verbo &lt;i&gt;to produce&lt;/i&gt;)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@QueryParam&amp;nbsp;&lt;/b&gt;indica semplicemente che l&#39;attributo echoString deve essere mappato sul parametro in query string &quot;echo_msg&quot;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Realizzazione del Client&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;&lt;br /&gt;
&lt;/b&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo un semplice &lt;b&gt;Java Project&lt;/b&gt; da Eclipse. Questo progetto conterrà il client del nostro servizio e sarà una &lt;i&gt;Java Application standard&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Di seguito il codice del client REST &lt;b&gt;EchoClient&lt;/b&gt; che invia la stringa di cui il servizio fornirà l&#39;echo in risposta:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:java&quot;&gt;import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

public class EchoClient {
 
  public static void main(String [] args){
    // Recupero instance di Client
    Client client = ClientBuilder.newClient();
  
    // Creo un WebTarget con l&#39;endpoint del servizio REST 
    WebTarget target = client.target(&quot;http://localhost:8080/RESTfulService/echo&quot;);
    //Aggiungo un parametro alla richiesta REST
    //Il parametro rappresenta il messaggio di cui fare l&#39;echo
    target=target.queryParam(&quot;echo_msg&quot;, &quot;ciao&quot;);
  
    // Invocazione del servizio
    Response res = target.request(&quot;text/plain&quot;).get();
  
    // Visualizzato l&#39;intera risposta e il suo contenuto
    System.out.println(res);
    System.out.println(res.readEntity(String.class));
  }
}
&lt;/pre&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Notiamo il &lt;b&gt;WebTarget &lt;/b&gt;che punta all&#39;url definita precedentemente con l&#39;Annotation &lt;i&gt;@Path&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Eseguiamo ora il deploy della nostra web application RESTfulService (creando il war ed installandolo nella cartella di pubblicazione del server - ad esempio webapps in Tomcat) e lanciamo il client appena creato.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; del client sarà il seguente:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codice&quot;&gt;ScopedJaxrsResponse{ClientResponse{method=GET, uri=http://localhost:8080/RESTfulService/echo?echo_msg=ciao, status=200, reason=OK}} &lt;br /&gt;
ciao&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/3929635373572979957/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/realizzare-un-servizio-rest-con-jax-rs.html#comment-form' title='5 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3929635373572979957'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3929635373572979957'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/realizzare-un-servizio-rest-con-jax-rs.html' title='Realizzare un servizio REST con JAX-RS 2.0 e Jersey 2.0'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-3194297099797042098</id><published>2013-05-07T09:00:00.000+02:00</published><updated>2013-05-07T09:00:16.923+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java EE 7"/><category scheme="http://www.blogger.com/atom/ns#" term="JAX-RS"/><category scheme="http://www.blogger.com/atom/ns#" term="Jersey"/><category scheme="http://www.blogger.com/atom/ns#" term="Paradigmi"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><category scheme="http://www.blogger.com/atom/ns#" term="RESTful"/><category scheme="http://www.blogger.com/atom/ns#" term="SOA"/><title type='text'>REST 2.0: Definizione e configurazione con il framework Jersey</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Cos&#39;è REST e come configurare un ambiente per utilizzarlo?&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;REST &lt;/b&gt;(&lt;span style=&quot;background-color: white; line-height: 19.1875px;&quot;&gt;&lt;b&gt;RE&lt;/b&gt;presentational &lt;b&gt;S&lt;/b&gt;tate &lt;b&gt;T&lt;/b&gt;ransfer&lt;/span&gt;) è un paradigma per la definizione di architetture distribuite.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;I suoi principi si basano sulla leggerezza e sull&#39;utilizzo dei metodi offerti dal protocollo &lt;i&gt;HTTP&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ogni entità/operazione viene rappresentata come una &lt;b&gt;Risorsa&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per l&#39;invocazione si utilizzano i metodi HTTP&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;(GET,PUT,POST,DELETE)&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La rappresentazione dei dati è snella (es. &lt;i&gt;JSON&lt;/i&gt;)&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;REST non è un concetto nuovo: un sito web rispetta i principi REST, cosi come un feed RSS. Di recente si sta estendendo il concetto REST alle architetture orientate ai servizi, oggi principalmente basate su HTTP/SOAP.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Una funzionalità/servizio che rispetta i principi REST è denominata&amp;nbsp;&lt;b&gt;RESTful&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In Java esiste la specifica&lt;b&gt; &lt;a href=&quot;http://jcp.org/en/jsr/detail?id=339&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;JAX-RS (JSR-339)&lt;/a&gt;&lt;/b&gt; che regola l&#39;utilizzo di REST.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;ultima release di &lt;b&gt;JAX-RS&lt;/b&gt; è la 1.1 ma è in fase di approvazione la versione 2.0 che introduce miglioramenti, semplificazioni e nuove funzionalità.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La specifica 2.0 farà parte di &lt;b&gt;Java EE 7&lt;/b&gt; prevista per il &lt;a href=&quot;http://jdevelopment.nl/open-source/java-ee-7-progress-page/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Q2 2013&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dato l&#39;imminente arrivo, in questo post vedremo come utilizzare &lt;b&gt;REST 2.0&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Sebbene sia possibile utilizzare le API già messe a disposizione da JAX-RS, è preferibile avere il supporto di un framework in grado di semplificare e ridurre le operazioni di routine o di basso livello richieste da JAX-RS.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;


&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Uno di questi è&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://jersey.java.net/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Jersey&lt;/a&gt;&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;che implementa le specifiche JAX-RS.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Oggi è già disponibile l&#39;implementazione per la versione 2.0 (per chi volesse approfondire l&#39;argomento&amp;nbsp;&lt;a href=&quot;http://www.infoq.com/news/2008/10/jaxrs-comparison&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;qui&lt;/a&gt;&amp;nbsp;si possono trovare i vari framework messi a confronto).&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Jersey &lt;/b&gt;quindi ci consentirà di semplificare la realizzazione di un servizio &lt;b&gt;REST&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;A questo punto cosa devo fare per provare a realizzare un semplice servizio?&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ecco gli ingredienti:&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Eclipse&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Tomcat 7&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://jcp.org/aboutJava/communityprocess/pfd/jsr339/index.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;JAX-RS 2.0&lt;/b&gt;&lt;/a&gt;&amp;nbsp;(rc3)&lt;/span&gt;&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;javax.ws.rs-api-2.0-rc3.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;javax.ws.rs-api-2.0-SNAPSHOT.jar&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.glassfish.jersey.bundles%22%20AND%20a%3A%22jax-rs-ri%22&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Jersey 2.0&lt;/b&gt;&lt;/a&gt;&amp;nbsp;(rc1)&lt;/span&gt;&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;jersey-client-2.0-rc1&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;jersey-common-2.0-rc1&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;jersey-container-servlet-2.0-rc1&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;jersey-container-servlet-core-2.0-rc1&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;jersey-container-servlet-core-2.0-rc1&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.jar&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dipendenze (che trovate nella distribuzione di Jersey nella cartella &lt;b&gt;ext&lt;/b&gt;)&lt;/span&gt;&lt;/li&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;asm-all-repackaged-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;cglib-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;guava-14.0.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;hk2-api-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;hk2-locator-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;hk2-utils-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;javax.annotation-api-1.2-b02.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;javax.inject-1.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;javax.inject-2.1.78.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;osgi-resource-locator-1.0.1.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;validation-api-1.1.0.CR3.jar&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Utilizzeremo queste librerie per creare un client &lt;b&gt;REST &lt;/b&gt;e un servizio &lt;b&gt;REST &lt;/b&gt;che espone un semplice metodo.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il client sarà una standard &lt;i&gt;Java Application&lt;/i&gt; mentre il servizio sarà realizzato come &lt;i&gt;Web Application&lt;/i&gt; (Dynamic Web Project di Eclipse).&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Nel prossimo post, scopriremo insieme come con poche e semplici righe di codice è possibile esporre una funzionalità service-oriented basata su REST.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/3194297099797042098/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/rest-20-definizione-e-configurazione.html#comment-form' title='1 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3194297099797042098'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3194297099797042098'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/rest-20-definizione-e-configurazione.html' title='REST 2.0: Definizione e configurazione con il framework Jersey'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-5215681158886186803</id><published>2013-05-02T09:00:00.000+02:00</published><updated>2013-05-02T09:00:15.440+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Application Scope"/><category scheme="http://www.blogger.com/atom/ns#" term="Class Loader"/><category scheme="http://www.blogger.com/atom/ns#" term="ClassCastException"/><category scheme="http://www.blogger.com/atom/ns#" term="Design Pattern"/><title type='text'>ClassCastException con oggetto in ClassLoader diversi</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Perchè ricevo un errore di ClassCastException se dalla mia webapp recupero lo stesso oggetto creato da un&#39;altra webapp?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Di solito questo errore si verifica quando stiamo utilizzando un oggetto in &lt;b&gt;due class loader diversi&lt;/b&gt;. Ad esempio ho una webapp A che utilizza un oggetto X del jar &lt;i&gt;X.jar&lt;/i&gt; presente nella sua &lt;i&gt;WEB-INF/lib&lt;/i&gt;. Decido di recuperare l&#39;oggetto X dalla webapp B che avrà anch&#39;essa nella sua &lt;i&gt;WEB-INF/lib&lt;/i&gt; il jar &lt;i&gt;X.jar&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In questo caso è vero che l&#39;oggetto X è lo stesso ma viene caricato sia nel class loader di A che in quello di B. Ciò comporta la generazione di un errore di &lt;b&gt;ClassCastException&lt;/b&gt; quando si tenta di recuperare l&#39;oggetto X da B.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per ovviare facilmente a questo errore si potrebbe decidere di inserire il jar X.jar in un &lt;b&gt;class loader condiviso&lt;/b&gt; tra le due webapp A e B (ad esempio il class loader dell&#39;Application Server).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Una soluzione più elegante, che offre una serie di vantaggi e rispetta le tecniche di Design Pattern, è quella di inserire a livello di class loader condiviso, solo una interfaccia e demandare l&#39;implementazione di tale interfaccia alle classi che saranno caricate dai diversi class loader.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Vediamo un esempio:&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;border: 1px solid; padding: 5px;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;N.B.&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per provare l&#39;esempio, abilitare il&amp;nbsp;&lt;i&gt;crossContext&amp;nbsp;&lt;/i&gt;nel file&amp;nbsp;&lt;b&gt;context.xml&lt;/b&gt;&amp;nbsp;presente nella cartella&amp;nbsp;&lt;b&gt;config&amp;nbsp;&lt;/b&gt;di tomcat&lt;/span&gt; &lt;br /&gt;
&lt;pre class=&quot;brush: xml; highlight:1&quot; style=&quot;text-align: start;&quot;&gt;&amp;lt;context &lt;span style=&quot;background-color: yellow;&quot;&gt;crosscontext=&quot;true&quot;&lt;/span&gt;&amp;gt;

    ...

&amp;lt;/context&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Definizione dell&#39;interfaccia &lt;b&gt;ICar&lt;/b&gt;&amp;nbsp;contenuta nel jar Interfaces.jar&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package interfaces;
public interface ICar {
 
 public void setNome(String nome);
 public String getNome();
 
 public void setModello(String modello);
 public String getModello();
 
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Definizione della classe&amp;nbsp;&lt;b&gt;BMWCar&lt;/b&gt;&amp;nbsp;(che implementa &lt;b&gt;ICar&lt;/b&gt;) contenuta nel jar Pojo.jar&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import interfaces.ICar;

public class BMWCar implements ICar{
 
 private String nome;
 private String modello;

 public void setNome(String nome) {
  this.nome=nome;
 }
 public String getNome(){
  return this.nome;
 }
 
 public void setModello(String modello){
  this.modello=modello;
 }
 
 public String getModello(){
  return this.modello;
 }

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Servlet &lt;b&gt;CreateCar&lt;/b&gt; della webapp A:&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;crea un oggetto bmw di tipo &lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;BMWCar&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;utilizzando l&#39;interfaccia &lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;ICar&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;memorizza l&#39;oggetto bmw nell&#39;Application Scope con chiave CAR&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import interfaces.ICar;
import pojo.BMWCar;

@WebServlet(&quot;/CreateBMWCar&quot;)
public class CreateBMWCar extends HttpServlet {
 private static final long serialVersionUID = 1L;

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  ServletContext sc = request.getServletContext();
  
  ICar bmw = new BMWCar();
  bmw.setModello(&quot;Serie 3&quot;);
  bmw.setNome(&quot;320d&quot;);
  
  sc.setAttribute(&quot;CAR&quot;, bmw);
  
  response.setContentType(&quot;text/plain&quot;);
  PrintWriter out = response.getWriter();
  out.write(&quot;Creata auto:\n&quot;);
  out.write(&quot;Nome: &quot;+bmw.getNome() + &quot;Modello: &quot;+bmw.getModello() );
 }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Servlet&amp;nbsp;&lt;b&gt;ReadCar&lt;/b&gt;&amp;nbsp;della WebApp B: recupera dall&#39;Application Scope della webapp A l&#39;auto creata:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import interfaces.ICar;

@WebServlet(&quot;/ReadCar&quot;)
public class ReadCar extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  ServletContext sc = request.getServletContext().getContext(&quot;/WebAppA&quot;);
  
  ICar car = (ICar) sc.getAttribute(&quot;CAR&quot;);
  
  response.setContentType(&quot;text/plain&quot;);
  PrintWriter out = response.getWriter();
  out.write(&quot;Auto recuperata:\n&quot;);
  out.write(&quot;Nome: &quot;+car.getNome() + &quot; Modello: &quot;+car.getModello()+&quot;&quot; );
 }

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Di seguito un&#39;immagine per capire come e dove sono organizzate/definite le classi descritte&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlteDtPhiFFR5jVGtyfeE4GqEf3JK-L5tuSXva3wdGHgBA_vCfcaqR6olfLNqqSdv0bpcaZ-8TVo4M5v5Xn-fsqrfMAl_Bva-B0ysp8rnsfbgewuBgM6evsBxC0uBR1RdFI3nvFZwOj892/s1600/AvoidClassCastException.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlteDtPhiFFR5jVGtyfeE4GqEf3JK-L5tuSXva3wdGHgBA_vCfcaqR6olfLNqqSdv0bpcaZ-8TVo4M5v5Xn-fsqrfMAl_Bva-B0ysp8rnsfbgewuBgM6evsBxC0uBR1RdFI3nvFZwOj892/s1600/AvoidClassCastException.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/5215681158886186803/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/05/classcastexception-con-oggetto-in.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5215681158886186803'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5215681158886186803'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/05/classcastexception-con-oggetto-in.html' title='ClassCastException con oggetto in ClassLoader diversi'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhlteDtPhiFFR5jVGtyfeE4GqEf3JK-L5tuSXva3wdGHgBA_vCfcaqR6olfLNqqSdv0bpcaZ-8TVo4M5v5Xn-fsqrfMAl_Bva-B0ysp8rnsfbgewuBgM6evsBxC0uBR1RdFI3nvFZwOj892/s72-c/AvoidClassCastException.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-3556637726082052517</id><published>2013-04-17T09:00:00.000+02:00</published><updated>2013-04-17T14:43:03.171+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="org.apache.commons.lang.time.DateFormatUtils"/><title type='text'>Rappresentare le Date in Java con Apache Commons DateFormatUtils</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso personalizzare le date in Java?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dopo aver visto come utilizzare le date con il &lt;b&gt;&lt;a href=&quot;http://lancill.blogspot.it/2013/04/rappresentare-le-date-in-java-con.html&quot; target=&quot;_blank&quot;&gt;SimpleDateFormat&lt;/a&gt;&lt;/b&gt;, passiamo a vedere una possibile alternativa fornita da un package di &lt;b&gt;Apache Commons&lt;/b&gt;&amp;nbsp;contenente l&#39;oggetto &lt;b&gt;DateFormatUtils&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per utilizzarlo è necessario scaricare &lt;b&gt;&lt;a href=&quot;http://lancill.altervista.org/BLOG/commons-lang3-3.1.jar&quot; target=&quot;_blank&quot;&gt;commons-lang3-3.1.jar&lt;/a&gt;&lt;/b&gt; (versione testata in questo post).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Dopo aver settato il &lt;i&gt;build path&lt;/i&gt; del progetto ed importato il jar appena scaricato, guardiamo nell&#39;esempio come rappresentare le &lt;b&gt;date &lt;/b&gt;in maniera semplice e veloce:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java; highlight: [6]&quot;&gt;package date;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

public class DateFormatUtilsTest {
   public static void main(String[] args) {

     // utilizziamo DateFormatUtils con la data odierna
     String dataOdierna = DateFormatUtils.format((new java.util.Date()), &quot;MM/dd&quot;); 
     System.out.println(&quot;dataOdierna = &quot; + dataOdierna); 

     // impostiamo la data a nostro piacimento
     Calendar cal = Calendar.getInstance();
     cal.set(2009, 11, 9); // il mese è zero based, quindi 0 = GENNAIO, 11 = DICEMBRE!!

     // utilizziamo DateFormatUtils con un oggetto Calendar
     String pattern = &quot;dd-MM-yyyy hh:mm z&quot;; 
     String dataPersonalizzata = DateFormatUtils.format(cal, pattern); 
     System.out.println(&quot;dataPersonalizzata1 = &quot; + dataPersonalizzata); 

     // utilizziamo DateFormatUtils con un oggetto java.util.Date
     Date data = cal.getTime(); 
     dataPersonalizzata = DateFormatUtils.format(data, &quot;dd/MM/yyyy HH:mm&quot;); 
     System.out.println(&quot;dataPersonalizzata2 = &quot; + dataPersonalizzata);
   }
}
&lt;/pre&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; sarà il seguente:&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dataOdierna = 04/15&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dataPersonalizzata1 = 09-12-2009 09:12 CET&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;dataPersonalizzata2 = 09/12/2009 21:38&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Possiamo personalizzare ulteriormente il &lt;i&gt;pattern &lt;/i&gt;utilizzando la tabella di riferimento qui sotto:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZzGyTEWFH-uMo-RCu8yG5xOV7MnhlS8PbTdyfrt1N67fDaJXJu_FyWSPySdlSCa-njDZXWMePg3tXlmevw-nCqxKnbNVLIq9jHodAKyb-dKZQbhHSASigfoCHWdsHBngofVQVkMyvVZgA/s1600/SimpleDateFormat.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;291&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZzGyTEWFH-uMo-RCu8yG5xOV7MnhlS8PbTdyfrt1N67fDaJXJu_FyWSPySdlSCa-njDZXWMePg3tXlmevw-nCqxKnbNVLIq9jHodAKyb-dKZQbhHSASigfoCHWdsHBngofVQVkMyvVZgA/s400/SimpleDateFormat.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: x-small;&quot;&gt;Pattern - Tabella di riferimento&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;Per inserire del testo&amp;nbsp;&lt;/span&gt;&lt;u style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;non&lt;/u&gt;&lt;span style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;&amp;nbsp;interpretato come&amp;nbsp;&lt;/span&gt;&lt;i style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;pattern&amp;nbsp;&lt;/i&gt;&lt;span style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;è sufficiente metterlo tra&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;singoli apici&lt;/b&gt;&lt;span style=&quot;background-color: white; color: #323232; line-height: 18px;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per ulteriori approfondimenti consiglio una lettura alla &lt;a href=&quot;https://www.google.it/url?sa=t&amp;amp;rct=j&amp;amp;q=&amp;amp;esrc=s&amp;amp;source=web&amp;amp;cd=1&amp;amp;cad=rja&amp;amp;ved=0CC8QFjAA&amp;amp;url=http%3A%2F%2Fcommons.apache.org%2Flang%2Fapidocs%2Forg%2Fapache%2Fcommons%2Flang3%2Ftime%2FDateFormatUtils.html&amp;amp;ei=q1dsUbPzGsKD4ATs_YCQCQ&amp;amp;usg=AFQjCNFV6Kf3NldkBHmbKvix6ge-MIOggA&amp;amp;sig2=VpgyxF2RqLzoHzevegJg9A&amp;amp;bvm=bv.45175338,d.ZWU&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;documentazione ufficiale&lt;/b&gt;&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/3556637726082052517/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/04/rappresentare-le-date-in-java-con_17.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3556637726082052517'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3556637726082052517'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/04/rappresentare-le-date-in-java-con_17.html' title='Rappresentare le Date in Java con Apache Commons DateFormatUtils'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZzGyTEWFH-uMo-RCu8yG5xOV7MnhlS8PbTdyfrt1N67fDaJXJu_FyWSPySdlSCa-njDZXWMePg3tXlmevw-nCqxKnbNVLIq9jHodAKyb-dKZQbhHSASigfoCHWdsHBngofVQVkMyvVZgA/s72-c/SimpleDateFormat.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-6970800650375238718</id><published>2013-04-15T09:00:00.000+02:00</published><updated>2013-04-17T14:44:27.472+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="java.text.SimpleDateFormat"/><title type='text'>Rappresentare le Date in Java con SimpleDateFormat</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso rappresentare le date in Java?&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Java &lt;/b&gt;ha una molteplicità di oggetti che gestiscono le &lt;b&gt;date&lt;/b&gt;, qualcuno con un funzionamento intuitivo, altri meno. Un tentativo di semplificare la loro gestione è fornito dalla classe &lt;b&gt;SimpleDateFormat &lt;/b&gt;(&lt;b&gt;&lt;a href=&quot;http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;java.text.SimpleDateFormat&lt;/a&gt;&lt;/b&gt;).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Vediamo come funziona con il seguente esempio &lt;b&gt;SimpleDateFormatTest&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDateFormatTest {
  public static void main(String[] args) throws ParseException {
  
    SimpleDateFormat sdf = new SimpleDateFormat(); // creo l&#39;oggetto
  
    // primo pattern: 2009, 12, 09
    sdf.applyPattern(&quot;yyyy, MM, dd&quot;);
    Date data = sdf.parse(&quot;2009, 12, 9&quot;);
    String dataStringa = sdf.format(data);
    System.out.println(dataStringa);  
        
    // modifico il pattern: 2009-12-09
    sdf.applyPattern(&quot;yyyy-MM-dd&quot;);
    dataStringa = sdf.format(data);
    System.out.println(dataStringa);  
        
    // settiamo il formato data (pattern) nel costruttore: 080413231216100
    sdf = new SimpleDateFormat(&quot;ddMMyyHHmmssSS&quot;);
    dataStringa = sdf.format(new Date()); // data corrente (8 aprile 2013)
    System.out.println(dataStringa);
  
    // pattern: 08-04-13-23.12.16100
    sdf.applyPattern(&quot;dd-MM-yy-HH:mm:ssSS&quot;);
    dataStringa = sdf.format(new Date());
    System.out.println(dataStringa); 
  
    // pattern: 08/04/2013
    sdf = new SimpleDateFormat(&quot;dd/MM/yyyy&quot;);
    dataStringa = sdf.format(new Date());
    System.out.println(dataStringa); 
  
    // settiamo la data in inglese: 01 June 1990
    data = sdf.parse(&quot;01/06/1990&quot;);
    Locale.setDefault(Locale.ENGLISH);
    sdf = new SimpleDateFormat(&quot;dd MMMM yyyy&quot;); //necessario dichiarare un nuovo oggetto per applicare i settaggi di localizzazione
    dataStringa = sdf.format(data);
    System.out.println(dataStringa); 
  
    // e in italiano: 01 giugno 1990
    Locale.setDefault(Locale.ITALIAN);
    sdf = new SimpleDateFormat(&quot;dd MMMM yyyy&quot;); 
    dataStringa = sdf.format(data);
    System.out.println(dataStringa); 
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il codice è ricco di commenti auto-esplicativi, ma vediamo lo stesso l&#39;&lt;b&gt;output&lt;/b&gt; fornito dal programma:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;2009, 12, 09&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;2009-12-09&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;080413233647979&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;08-04-13-23:36:47979&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;08/04/2013&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;01 June 1990&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444; font-family: Courier New, Courier, monospace;&quot;&gt;01 giugno 1990&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ecco una breve &lt;b&gt;tabella riepilogativa&lt;/b&gt;&amp;nbsp;su come comporre i &lt;i&gt;pattern&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAPKSd_BUHz_JGuglr0IEIurGc7aZ5AFIY9Gy4VWvw8m-yce026JbPqThg9vhveqk7UzZdNEYAVSvK-Cqvh7UjH3wr8-zBVUAS37-QQr4FlL0olicivSBEDh1SOUI9-xs-R_r1M7pWtMur/s1600/SimpleDateFormat.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;291&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAPKSd_BUHz_JGuglr0IEIurGc7aZ5AFIY9Gy4VWvw8m-yce026JbPqThg9vhveqk7UzZdNEYAVSvK-Cqvh7UjH3wr8-zBVUAS37-QQr4FlL0olicivSBEDh1SOUI9-xs-R_r1M7pWtMur/s400/SimpleDateFormat.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tabella pattern SimpleDateFormat&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; text-align: justify;&quot;&gt;Per inserire del testo &lt;u&gt;non&lt;/u&gt; interpretato come &lt;i&gt;pattern &lt;/i&gt;è sufficiente metterlo tra &lt;b&gt;singoli apici&lt;/b&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;NOTA&lt;/u&gt;: &lt;b&gt;&lt;i&gt;SimpleDateFormat &lt;/i&gt;&lt;/b&gt;non è &lt;i&gt;ThreadSafe&lt;/i&gt;, quindi non è sincronizzato. La raccomandazione è quindi di creare ciascuna istanza &lt;b&gt;&lt;i&gt;SimpleDateFormat &lt;/i&gt;&lt;/b&gt;in &lt;i&gt;Thread &lt;/i&gt;separati oppure di sincronizzarne esternamente l&#39;accesso.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Un metodo alternativo a &lt;b&gt;SimpleDateFormat&lt;/b&gt; può essere trovato &lt;a href=&quot;http://lancill.blogspot.com/2013/04/rappresentare-le-date-in-java-con_17.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;qui&lt;/b&gt;&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/6970800650375238718/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/04/rappresentare-le-date-in-java-con.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6970800650375238718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6970800650375238718'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/04/rappresentare-le-date-in-java-con.html' title='Rappresentare le Date in Java con SimpleDateFormat'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgAPKSd_BUHz_JGuglr0IEIurGc7aZ5AFIY9Gy4VWvw8m-yce026JbPqThg9vhveqk7UzZdNEYAVSvK-Cqvh7UjH3wr8-zBVUAS37-QQr4FlL0olicivSBEDh1SOUI9-xs-R_r1M7pWtMur/s72-c/SimpleDateFormat.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-6013485367480550483</id><published>2013-04-11T09:00:00.000+02:00</published><updated>2013-04-11T09:00:16.565+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="hash"/><category scheme="http://www.blogger.com/atom/ns#" term="ordinamento"/><category scheme="http://www.blogger.com/atom/ns#" term="sort"/><category scheme="http://www.blogger.com/atom/ns#" term="Struttura dati"/><title type='text'>Ordinamento HashMap basato su proprietà custom</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso ordinare un HashMap basandomi su una proprietà degli oggetti che contiene?&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E&#39; possibile eseguire un &lt;b&gt;ordinamento &lt;/b&gt;di un &lt;b&gt;HashMap &lt;/b&gt;basato su una proprietà dei suoi oggetti attraverso l&#39;utilizzo di due elementi:&lt;/span&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://lancill.blogspot.it/2012/07/java-comparator.html&quot; target=&quot;_blank&quot;&gt;Comparator&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;i&gt;&lt;a href=&quot;http://lancill.blogspot.it/2012/07/strutture-dati-list-set-map.html&quot; target=&quot;_blank&quot;&gt;TreeMap&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Innanzitutto creiamo un &lt;i&gt;bean &lt;/i&gt;per rappresentare gli elementi da ordinare in base ad una proprietà. Il bean sarà &lt;i&gt;Tab &lt;/i&gt;e la proprietà &lt;i&gt;position&lt;/i&gt;. Come avrete intuito, faremo l&#39;ordinamento di una serie di tab come quelli che troviamo in quasi tutte le interfacce grafiche dei moderni siti web.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La nostra lista di tab (non ordinata) sarà la seguente:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Portfolio&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Home&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Contatti&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Chi siamo&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Da ordinare nel seguente modo:&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Home&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Chi siamo&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Portfolio&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Contatti&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Innanzitutto creiamo il &lt;b&gt;bean &lt;/b&gt;&lt;i&gt;&lt;b&gt;Tab&lt;/b&gt;:&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&quot;brush: java&quot;&gt;public class Tab {
 
 private int position;

 public Tab(int position) {
  this.position=position;
 }

 public int getPosition() {
  return position;
 }

 public void setPosition(int position) {
  this.position = position;
 }
 
 @Override
 public String toString(){
  return &quot;Tab in posizione &quot;+position;
  
 }

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creiamo la classe &lt;b&gt;PositionComparator &lt;/b&gt;che implementa &lt;i&gt;Comparator &lt;/i&gt;e ci permette di definire i criteri di ordinamento (per approfondimenti vi rimando al post su &lt;a href=&quot;http://lancill.blogspot.com/2012/07/java-comparator.html&quot; target=&quot;_blank&quot;&gt;Comparator&lt;/a&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;class PositionComparator implements Comparator&amp;lt;String&amp;gt; {

 Map&amp;lt;String, Tab&amp;gt; unsortedMap;

 public PositionComparator(Map&amp;lt;String, Tab&amp;gt; unsortedMap) {
  this.unsortedMap = unsortedMap;
 }

 @Override
 public int compare(String a, String b) {
  if (unsortedMap.get(a).getPosition() &amp;lt; unsortedMap.get(b).getPosition()) {
   /* System.out.println(a +&quot; e&#39; prima di &quot;+b);*/
   return -1;
  } else if (unsortedMap.get(a).getPosition() &amp;gt; unsortedMap.get(b)
    .getPosition()) {
   // System.out.println(a +&quot; e&#39; dopo di &quot;+b);
   return 1;
  } else {
   return 0;
  }

 }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;A questo punto non resta che creare la nostra applicazione Java che crea i tab in sequenza &quot;casuale&quot; per poi ordinarli attraverso il metodo &lt;b&gt;sortTabs()&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush: java&quot;&gt;import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class SortMap {

 public static void main(String[] args) {
  Map&amp;lt;String, Tab&amp;gt; tabs = new HashMap&amp;lt;String, Tab&amp;gt;();
  tabs.put(&quot;Portfolio&quot;, new Tab(3));
  tabs.put(&quot;Home&quot;, new Tab(1));
  tabs.put(&quot;Contatti&quot;, new Tab(4));
  tabs.put(&quot;Chi siamo&quot;, new Tab(2));

  System.out.println(&quot;------------Map non ordinata-----------&quot;);
  System.out.println(&quot;unsorted map: &quot; + tabs);
  for (String key : tabs.keySet()) {
   System.out.println(&quot;tab: &quot; + tabs.get(key).getPosition());
  }
  System.out.println(&quot;-----------------------------------\n&quot;);

  //Ordinamento
  tabs = sortTabs(tabs);

  System.out.println(&quot;------------Map ordinata-----------&quot;);
  System.out.println(&quot;sorted map: &quot; + tabs);
  for (String key : tabs.keySet()) {
   System.out.println(&quot;tab: &quot; + tabs.get(key).getPosition());
  }
  System.out.println(&quot;-----------------------------------&quot;);
  
 }

 public static Map&amp;lt;String, Tab&amp;gt; sortTabs(Map&amp;lt;String, Tab&amp;gt; tabs) {

  PositionComparator pc = new PositionComparator(tabs);
  TreeMap&amp;lt;String, Tab&amp;gt; sorted_map = new TreeMap&amp;lt;String, Tab&amp;gt;(pc);

  sorted_map.putAll(tabs);

  return sorted_map;
 }
}
&lt;/pre&gt;
&lt;div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Notiamo come la &lt;i&gt;Map&lt;/i&gt;&lt;i&gt;&amp;nbsp;&lt;/i&gt;iniziale (riga 9) utilizzi una &lt;b&gt;HashMap &lt;/b&gt;mentre il metodo &lt;b&gt;sortTabs()&lt;/b&gt;&amp;nbsp;una&amp;nbsp;&lt;b&gt;TreeMap &lt;/b&gt;(riga 37). La&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;differenza fondamentale tra le due strutture dati è che &lt;b&gt;Tr&lt;/b&gt;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span class=&quot;hps&quot;&gt;eeMap&lt;/span&gt;&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;memorizza&lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;&lt;/i&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;gli&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;elementi&lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;in&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;un albero e&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;consente di&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;recuperarli&lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;in un ordine&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;di default oppure&amp;nbsp;&lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;definito&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; &lt;/span&gt;&lt;span class=&quot;hps&quot; style=&quot;font-family: Verdana, sans-serif;&quot;&gt;da chi la usa (custom, come nel nostro caso)&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ricapitoliamo brevemente il flusso logico del codice per riuscire ad ordinare una Map preesistente:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Si associa la Map non ordinata al Comparator&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creo il &lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;PositionComparator &lt;/i&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;fornendo&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;in input la Map dei tab&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Si crea una TreeMap associandole il Comparator&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Istanzio una TreeMap fornendo in input il &lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;PositionComparator&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La TreeMap creata viene popolata con la Map iniziale&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;sorted_map.putAll(tabs)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; dell&#39;applicazione sarà il seguente:&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;
------------Map non ordinata-----------&lt;br /&gt;
unsorted map: {Chi siamo=Tab in posizione 2, Home=Tab in posizione 1, Contatti=Tab in posizione 4, Portfolio=Tab in posizione 3}&lt;br /&gt;
tab: 2&lt;br /&gt;
tab: 1&lt;br /&gt;
tab: 4&lt;br /&gt;
tab: 3&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
------------Map ordinata-----------&lt;br /&gt;
sorted map: {Home=Tab in posizione 1, Chi siamo=Tab in posizione 2, Portfolio=Tab in posizione 3, Contatti=Tab in posizione 4}&lt;br /&gt;
tab: 1&lt;br /&gt;
tab: 2&lt;br /&gt;
tab: 3&lt;br /&gt;
tab: 4&lt;br /&gt;
-----------------------------------&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/6013485367480550483/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/04/ordinamento-hashmap-basato-su-proprieta.html#comment-form' title='1 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6013485367480550483'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6013485367480550483'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/04/ordinamento-hashmap-basato-su-proprieta.html' title='Ordinamento HashMap basato su proprietà custom'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-6292787202589884754</id><published>2013-04-09T09:00:00.000+02:00</published><updated>2013-04-09T09:00:09.737+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="image scaling"/><category scheme="http://www.blogger.com/atom/ns#" term="immagini"/><category scheme="http://www.blogger.com/atom/ns#" term="resize immagini"/><title type='text'>Java Application per ridimensionamento immagine in proporzione</title><content type='html'>&lt;h3&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso ridimensionare un&#39;immagine rispettando le sue proporzioni?&lt;/span&gt;&lt;/h3&gt;
&lt;br /&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Di seguito una breve guida per creare una piccola &lt;i&gt;Java Application&lt;/i&gt; che visualizza un&#39;immagine ridimensionata rispettando le sue proporzioni.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;applicazione inoltre salva l&#39;immagine su disco attraverso il metodo &lt;b&gt;saveImage()&lt;/b&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il codice utilizza il metodo &lt;b&gt;getScaledDimension()&amp;nbsp;&lt;/b&gt;&lt;a href=&quot;http://stackoverflow.com/questions/10245220/java-image-resize-maintain-aspect-ratio&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;riportato su StackOverflow&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush: java; highlight: [23,24,25,26]&quot;&gt;import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

public class ImageResizeProportionally {

 public static void main(String[] args) {

  String path_immagine = &quot;C:\\mia_immagine.jpg&quot;;

  BufferedImage srcImage;
  try {
   srcImage = ImageIO.read(new File(path_immagine));

   BufferedImage scaledImage = getScaledImage(srcImage);

   showImage(scaledImage);
 
   saveImage(scaledImage);
  } catch (Exception e) {
   e.printStackTrace();
  }

 }

 // Ridimensionamento 100x118
 private static BufferedImage getScaledImage(BufferedImage originalImage) {

  int MAX_IMG_WIDTH = 110;
  int MAX_IMG_HEIGHT = 118;

  int width = originalImage.getWidth();
  int height = originalImage.getHeight();

  Dimension originalDimension = new Dimension(originalImage.getWidth(),
    originalImage.getHeight());
  Dimension boundaryDimension = new Dimension(MAX_IMG_WIDTH,
    MAX_IMG_HEIGHT);
  Dimension scalingDimension = getScaledDimension(originalDimension,
    boundaryDimension);

  width = (int) scalingDimension.getWidth();
  height = (int) scalingDimension.getHeight();

  BufferedImage resizedImage = new BufferedImage(width, height,
    originalImage.getType());
  Graphics2D g = resizedImage.createGraphics();

  g.drawImage(originalImage, 0, 0, width, height, null);

  return resizedImage;

 }

public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {

    int original_width = imgSize.width;
    int original_height = imgSize.height;
    int bound_width = boundary.width;
    int bound_height = boundary.height;
    int new_width = original_width;
    int new_height = original_height;

    // Controllo se e&#39; necessario eseguire lo scaling
    if (original_width &amp;gt; bound_width) {
        //scaling della larghezza in base alla larghezza massima
        new_width = bound_width;
        //eseguo lo scaling dell&#39;altezza per mantenere le proporzioni
        new_height = (new_width * original_height) / original_width;
    }

    // Dopo aver calcolato la nuova altezza, controllo se è ancora fuori limite
    if (new_height &amp;gt; bound_height) {
        
        new_height = bound_height;
        //rieseguo lo scaling per mantenere le proporzioni
        new_width = (new_height * original_width) / original_height;
    }

    return new Dimension(new_width, new_height);
}

 /**
  * Visualizzazione dell&#39;immagine ridimensionata in un JFrame
  */
 private static void showImage(BufferedImage image) {

  JFrame editorFrame = new JFrame(&quot;Immagine ridimensionata&quot;);
  editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

  ImageIcon imageIcon = new ImageIcon(image);

  JLabel jLabel = new JLabel();
  jLabel.setIcon(imageIcon);

  editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

  editorFrame.pack();
  editorFrame.setLocationRelativeTo(null);
  editorFrame.setVisible(true);

 }

 private static void saveImage(BufferedImage scaledImage) throws FileNotFoundException, IOException {
  FileOutputStream file = new FileOutputStream(&quot;C:\\mia_immmagine_ridimensionata.jpg&quot;);
  ImageIO.write(scaledImage, &quot;jpg&quot;, file);
  file.close();
 }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; sarà una finestra &lt;i&gt;Java&lt;/i&gt; che mostrerà l&#39;immagine &lt;b&gt;ridimensionata&lt;/b&gt; e, grazie al metodo &lt;i&gt;saveImage()&lt;/i&gt;, in &lt;i&gt;C:&lt;/i&gt; avremo una copia in scala dell&#39;originale &lt;i&gt;mia_immagine.jpg&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/6292787202589884754/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/04/java-application-per-ridimensionamento.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6292787202589884754'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6292787202589884754'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/04/java-application-per-ridimensionamento.html' title='Java Application per ridimensionamento immagine in proporzione'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-8662953023713736187</id><published>2013-04-03T09:00:00.000+02:00</published><updated>2013-04-03T09:00:14.819+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="reindirizzare"/><category scheme="http://www.blogger.com/atom/ns#" term="Response"/><category scheme="http://www.blogger.com/atom/ns#" term="reverse proxy"/><category scheme="http://www.blogger.com/atom/ns#" term="sendredirect"/><title type='text'>Problemi sendRedirect con reverse proxy?</title><content type='html'>&lt;h3&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come posso evitare l&#39;utilizzo di sendRedirect quando questa istruzione crea problemi in un ambiente con reverse proxy?&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Si può sostituire l&#39;istruzione &lt;b&gt;response.sendRedirect(url_path)&lt;/b&gt; impostando gli &lt;i&gt;header &lt;/i&gt;della &lt;b&gt;response HTTP&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Gli header da impostare sono:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Location&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Status Code&lt;/span&gt;&lt;/li&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;il codice da utilizzare è il &lt;i&gt;302 - Moved Temporarily&lt;/i&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Vediamo come:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&quot;brush: java; highlight: [10, 12]&quot;&gt;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SomeServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
      
    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
  
    response.setHeader(&quot;Location&quot;, &quot;/mia_pagina.jsp&quot;);

  }
}

&lt;/pre&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/8662953023713736187/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2013/04/problemi-sendredirect-con-reverse-proxy.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8662953023713736187'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8662953023713736187'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2013/04/problemi-sendredirect-con-reverse-proxy.html' title='Problemi sendRedirect con reverse proxy?'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-3309969704286974888</id><published>2012-12-19T09:00:00.000+01:00</published><updated>2012-12-19T09:00:08.669+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache FOP"/><category scheme="http://www.blogger.com/atom/ns#" term="fop"/><category scheme="http://www.blogger.com/atom/ns#" term="pdf"/><category scheme="http://www.blogger.com/atom/ns#" term="xml"/><category scheme="http://www.blogger.com/atom/ns#" term="xsl"/><title type='text'>Creare PDF da un XML con Apache FOP 1.1</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si crea un PDF da un file XML?&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;br /&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E&#39; possibile creare un &lt;b&gt;PDF &lt;/b&gt;a partire da un &lt;b&gt;XML &lt;/b&gt;con &lt;a href=&quot;http://xmlgraphics.apache.org/fop/1.1/index.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Apache FOP 1.1&lt;/b&gt;&lt;/a&gt;, ultima versione attualmente disponibile. E&#39; indispensabile sapere che per la trasformazione è necessario un file &quot;intermedio&quot; &lt;b&gt;.xsl&lt;/b&gt; utilizzato come &lt;b&gt;template &lt;/b&gt;(modello) in cui vengono conservate le informazioni di impaginazione.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come al solito, guardiamo con degli esempi &lt;i&gt;ad hoc&lt;/i&gt;&amp;nbsp;ogni singolo step anche se, in questo post, si da per scontata una conoscenza basilare di &lt;b&gt;xml &lt;/b&gt;ed &lt;b&gt;xsl&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La seguente classe &lt;b&gt;Xml2Pdf&lt;/b&gt; trasforma l&#39;&lt;b&gt;XML&lt;/b&gt; in &lt;b&gt;PDF &lt;/b&gt;grazie alle informazioni contenute nel &lt;i&gt;.xsl&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package fop;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class Xml2Pdf {
  public static void main(String[] args) throws IOException {

    File xmlIn = new File(&quot;C:\\rubrica.xml&quot;);
    File xslt = new File(&quot;C:\\rubrica.xsl&quot;);
    File pdfOut = new File(&quot;C:\\rubrica.pdf&quot;);

    // Setup output
    OutputStream out = new java.io.FileOutputStream(pdfOut);
    out = new java.io.BufferedOutputStream(out);

    try {
      FopFactory fopFactory = FopFactory.newInstance();
      FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

      // Costruisco il fop con l&#39;output desiderato
      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

      // Setup XSLT
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer transformer = factory.newTransformer(new StreamSource(new InputStreamReader(new FileInputStream(xslt), &quot;UTF-8&quot;)));
      transformer.setOutputProperty(&quot;encoding&quot;, &quot;UTF-8&quot;); // Setto l&#39;encoding di output
      transformer.setParameter(&quot;versionParam&quot;, &quot;2.0&quot;); // Setto il valore di versionParam nello stylesheet

      Source src = new StreamSource(xmlIn); // Setup input per la trasformazione XSLT 

      Result res = new SAXResult(fop.getDefaultHandler());
   
      transformer.transform(src, res); // Inizio XSLT transformation XSLT e FOP processing

    } catch (Exception e) {
       e.printStackTrace(System.err);
       System.exit(-1);
    } finally {
       out.close();
    }
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per il corretto funzionamento della classe è necessario scaricare ed aggiungere al &lt;i&gt;classpath/buildpath&lt;/i&gt; alcune librerie &lt;b&gt;&lt;a href=&quot;http://lancill.altervista.org/BLOG/FOP_1-1.rar&quot; target=&quot;_blank&quot;&gt;Apache FOP versione 1.1&lt;/a&gt; (10,5 MB)&lt;/b&gt;. Il file &lt;i&gt;.rar&lt;/i&gt; contiene tutti i &lt;i&gt;jar &lt;/i&gt;necessari elencati di seguito:&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;fop.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;avalon-framework-4.2.0.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;batik-all-1.7.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;commons-io-1.3.1.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;commons-logging-1.0.4.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;serializer-2.7.0.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;xalan-2.7.0.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;xercesImpl-2.7.1.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;xml-apis-1.3.04.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;xml-apis-ext-1.3.04.jar&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;xmlgraphics-commons-1.5.jar&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il file &lt;b&gt;rubrica.xsl&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;

&amp;lt;xsl:stylesheet version=&quot;1.1&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot; exclude-result-prefixes=&quot;fo&quot;&amp;gt;
&amp;lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; omit-xml-declaration=&quot;no&quot; indent=&quot;yes&quot;/&amp;gt;
&amp;lt;xsl:param name=&quot;versionParam&quot; select=&quot;&#39;1.0&#39;&quot;/&amp;gt;

&amp;lt;!-- definizione di rubrica --&amp;gt;
&amp;lt;xsl:template match=&quot;rubrica&quot;&amp;gt;
  &amp;lt;fo:root xmlns:fo=&quot;http://www.w3.org/1999/XSL/Format&quot;&amp;gt;
    &amp;lt;fo:layout-master-set&amp;gt;
&amp;nbsp; &amp;nbsp;   &amp;lt;fo:simple-page-master master-name=&quot;simpleA4&quot; page-height=&quot;29.7cm&quot; page-width=&quot;21cm&quot; margin-top=&quot;2cm&quot; margin-bottom=&quot;2cm&quot; margin-left=&quot;2cm&quot; margin-right=&quot;2cm&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;   &amp;lt;fo:region-body/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:simple-page-master&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:layout-master-set&amp;gt;

&amp;nbsp; &amp;nbsp; &amp;lt;fo:page-sequence master-reference=&quot;simpleA4&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:flow flow-name=&quot;xsl-region-body&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:block font-size=&quot;16pt&quot; font-weight=&quot;bold&quot; space-after=&quot;5mm&quot;&amp;gt;Titolo: &amp;lt;xsl:value-of select=&quot;titolo&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:block font-size=&quot;12pt&quot; space-after=&quot;5mm&quot;&amp;gt;Version &amp;lt;xsl:value-of select=&quot;$versionParam&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:block font-size=&quot;10pt&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;   &amp;lt;fo:table table-layout=&quot;fixed&quot; width=&quot;100%&quot; border-collapse=&quot;separate&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:table-column column-width=&quot;4cm&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:table-column column-width=&quot;4cm&quot;/&amp;gt;
&amp;nbsp;           &amp;lt;fo:table-column column-width=&quot;4cm&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:table-column column-width=&quot;5cm&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:table-body&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:apply-templates select=&quot;persona&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:table-body&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:table&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:flow&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:page-sequence&amp;gt;
&amp;nbsp; &amp;lt;/fo:root&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;!-- definizione dell&#39;elemento PERSONA --&amp;gt;
&amp;lt;xsl:template match=&quot;persona&quot;&amp;gt;
&amp;nbsp; &amp;lt;fo:table-row&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;xsl:if test=&quot;professione = &#39;blogger&#39;&quot;&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:attribute name=&quot;font-weight&quot;&amp;gt;bold&amp;lt;/xsl:attribute&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/xsl:if&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:value-of select=&quot;nome&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:table-cell&amp;gt;
    &amp;lt;fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp;   &amp;lt;fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:value-of select=&quot;telefono&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp;   &amp;lt;fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:value-of select=&quot;professione&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;fo:table-cell&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;xsl:value-of select=&quot;email&quot;/&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/fo:block&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/fo:table-cell&amp;gt;
&amp;nbsp; &amp;lt;/fo:table-row&amp;gt;
&amp;lt;/xsl:template&amp;gt;

&amp;lt;/xsl:stylesheet&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ed infine il file &lt;b&gt;rubrica.xml&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:xml&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;rubrica&amp;gt;
&amp;nbsp; &amp;lt;titolo&amp;gt;La mia rubrica&amp;lt;/titolo&amp;gt;
&amp;nbsp;&amp;nbsp;
&amp;nbsp; &amp;lt;persona&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;nome&amp;gt;Pippo&amp;lt;/nome&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;telefono&amp;gt;06123456789&amp;lt;/telefono&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;professione&amp;gt;programmatore&amp;lt;/professione&amp;gt;
&amp;nbsp; &amp;nbsp; &amp;lt;email&amp;gt;pippo@unemail.it&amp;lt;/email&amp;gt;
&amp;nbsp; &amp;lt;/persona&amp;gt;

  &amp;lt;persona&amp;gt;
    &amp;lt;nome&amp;gt;Fabrizio&amp;lt;/nome&amp;gt;
    &amp;lt;telefono&amp;gt;0655555555&amp;lt;/telefono&amp;gt; 
    &amp;lt;professione&amp;gt;blogger&amp;lt;/professione&amp;gt;
    &amp;lt;email&amp;gt;fabrizio@unemail.it&amp;lt;/email&amp;gt;
  &amp;lt;/persona&amp;gt;

  &amp;lt;persona&amp;gt;
    &amp;lt;nome&amp;gt;Pluto&amp;lt;/nome&amp;gt;
    &amp;lt;telefono&amp;gt;0644444444&amp;lt;/telefono&amp;gt;
    &amp;lt;professione&amp;gt;programmatore&amp;lt;/professione&amp;gt;
    &amp;lt;email&amp;gt;pluto@unemail.it&amp;lt;/email&amp;gt;
  &amp;lt;/persona&amp;gt;

  &amp;lt;persona&amp;gt;
    &amp;lt;nome&amp;gt;Paperino&amp;lt;/nome&amp;gt;
    &amp;lt;telefono&amp;gt;0633333333&amp;lt;/telefono&amp;gt;
    &amp;lt;professione&amp;gt;giornalista&amp;lt;/professione&amp;gt;
    &amp;lt;email&amp;gt;paperino@unemail.it&amp;lt;/email&amp;gt;
  &amp;lt;/persona&amp;gt;

  &amp;lt;persona&amp;gt;
    &amp;lt;nome&amp;gt;Clarabella&amp;lt;/nome&amp;gt;
    &amp;lt;telefono&amp;gt;0622222222&amp;lt;/telefono&amp;gt;
    &amp;lt;professione&amp;gt;disoccupato&amp;lt;/professione&amp;gt;
    &amp;lt;email&amp;gt;clarabella@unemail.it&amp;lt;/email&amp;gt;
  &amp;lt;/persona&amp;gt;

&amp;lt;/rubrica&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Posizionate il file &lt;i&gt;rubrica.xml&lt;/i&gt; e&lt;i&gt; rubrica.xsl&lt;/i&gt; in &lt;i&gt;C:\&lt;/i&gt; e lanciate la classe &lt;b&gt;Xml2Pdf&lt;/b&gt;. Il risultato sarà un file &lt;b&gt;rubrica.pdf&lt;/b&gt; con i dati contenuti nel file &lt;i&gt;.xml&lt;/i&gt; impaginato secondo i criteri del file &lt;i&gt;.xsl&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/3309969704286974888/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/12/creare-pdf-da-un-xml-con-apache-fop-11.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3309969704286974888'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/3309969704286974888'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/12/creare-pdf-da-un-xml-con-apache-fop-11.html' title='Creare PDF da un XML con Apache FOP 1.1'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-7977094417556103304</id><published>2012-12-12T09:00:00.000+01:00</published><updated>2012-12-12T09:00:14.649+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java Development Kit"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Runtime Environment"/><category scheme="http://www.blogger.com/atom/ns#" term="JDK"/><category scheme="http://www.blogger.com/atom/ns#" term="JRE"/><title type='text'>Differenza tra JRE e JDK</title><content type='html'>&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Che differenza c&#39;è tra Java Runtime Environment (JRE) e Java Development Kit (JDK)?&lt;/span&gt;&lt;/h3&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;br /&gt;
&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Se la vostra esigenza è quella di eseguire delle applicazioni &lt;b&gt;Java&lt;/b&gt;, è sufficiente la &lt;b&gt;JRE&lt;/b&gt;. Infatti &lt;b&gt;Java Runtime Enviroment&lt;/b&gt; è un&#39;implementazione della &lt;b&gt;Java Virtual Machine&lt;/b&gt; &lt;b&gt;(JVM)&lt;/b&gt; necessaria per far girare i programmi &lt;b&gt;Java &lt;/b&gt;sul vostro calcolatore. E&#39; quindi necessaria la &lt;b&gt;JRE&lt;/b&gt; per eseguire ad esempio &lt;i&gt;Eclipse&lt;/i&gt;, le &lt;i&gt;applet&lt;/i&gt;, &lt;i&gt;Tomcat&lt;/i&gt;, ecc...&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La &lt;b&gt;JDK &lt;/b&gt;invece è indispensabile nel caso voi abbiate la necessità di sviluppare software Java.&amp;nbsp;Infatti la &lt;b&gt;JDK &lt;/b&gt;contiene al suo interno una o più &lt;i&gt;JRE e&lt;/i&gt;&amp;nbsp;tutti gli applicativi utili per la programmazione in Java: debuggers, compilatori come javac, librerie per lo sviluppo, ecc...&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Sono entrambe &lt;u&gt;gratuite&lt;/u&gt; e scaricabili dal sito ufficiale&amp;nbsp;&lt;b&gt;&lt;a href=&quot;http://www.oracle.com/technetwork/java/javase/downloads/index.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;Oracle Java&lt;/a&gt;&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;NOTA&lt;/u&gt;: per &lt;b&gt;Eclipse &lt;/b&gt;è sufficiente la &lt;i&gt;JRE &lt;/i&gt;perché al suo interno contiene già il compilatore, le librerie e i tools necessari al programmatore.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/7977094417556103304/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/12/differenza-tra-jre-e-jdk.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7977094417556103304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7977094417556103304'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/12/differenza-tra-jre-e-jdk.html' title='Differenza tra JRE e JDK'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-8406210923865443518</id><published>2012-11-26T09:00:00.000+01:00</published><updated>2012-11-26T09:00:01.884+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Commons"/><category scheme="http://www.blogger.com/atom/ns#" term="Base64"/><category scheme="http://www.blogger.com/atom/ns#" term="codifica"/><category scheme="http://www.blogger.com/atom/ns#" term="commons-codec.jar"/><category scheme="http://www.blogger.com/atom/ns#" term="decodifica"/><category scheme="http://www.blogger.com/atom/ns#" term="immagini"/><title type='text'>Codifica e decodifica di un&#39;immagine in Base64</title><content type='html'>&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si effettua la codifica in Base64 di un&#39;immagine in Java?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Differentemente da quanto visto per la &lt;a href=&quot;http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-una-stringa-in.html&quot; target=&quot;_blank&quot;&gt;codifica &lt;b&gt;Base64&lt;/b&gt; delle stringhe&lt;/a&gt;, per le immagini abbiamo alcune varianti dovute soprattutto alla conversione della stessa in un &lt;b&gt;array di byte&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Scarichiamo la classe &lt;b&gt;Base64&lt;/b&gt; di &lt;b&gt;Apache Commons&lt;/b&gt; contenuta nel pacchetto&amp;nbsp;&lt;b&gt;&lt;a href=&quot;http://lancill.altervista.org/BLOG/commons-codec-1.2.jar&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;commons-codec-1.2.jar&lt;/a&gt;&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il codice nell&#39;esempio mostra come &lt;b&gt;convertire in Base64&amp;nbsp;&lt;/b&gt;un&#39;immagine &lt;i&gt;JPG, &lt;/i&gt;ma è adattabile anche ad altri formati:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package base64;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;

public class Base64ImgConversion {
  public static void main(String[] args) throws IOException {

    BufferedImage immagine = ImageIO.read(new File(&quot;C:\\immagine.jpg&quot;));
    ByteArrayOutputStream byteArrOS = new ByteArrayOutputStream();
    ImageIO.write(immagine, &quot;jpg&quot;, byteArrOS);

    // ENCODING
    byte[] encoded = Base64.encodeBase64(byteArrOS.toByteArray());

    System.out.println(&quot;Base64 Encoded String : &quot; + new String(encoded));

    // DECODING
    byte[] decoded = Base64.decodeBase64(encoded); 
    System.out.println(&quot;Base 64 Decoded  String : &quot; + new String(decoded));
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; (con &lt;i&gt;omissis &lt;/i&gt;data la lunghezza eccessiva della stringa):&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
Codificata Base64: /9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJ&lt;br /&gt;
[...]&lt;br /&gt;
L24/56n8hTlvLlj/AK9x9DiiipbZahHsPS4umP8Ax+XI+khFRG8uiObq4P1lb/GiipuzTkj2JBJMTzcTH6uaiMCM2WyT7miipTKSQnlovRRUN1M1uPkC/iM0UUobmj0izLlnlmYs7kmoaKK60fP1viEoyaKKoxFFKKKKlgOBNSp0oopAiDUmIt0UHhm5qhH0FFFar4QZoQ/cH+e1ODFbBsdhx+QooqBosXCiO1kVRgYH866fwxhdUt8KP9cvb3oorGr8JpT+JHpuTSZJoorhO8bTTRRVALRRRQI//9k=&lt;br /&gt;
&lt;br /&gt;
Decodificata Base64:&lt;br /&gt;
[...]&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come è possibile vedere dal risultato dell&#39;output, la prima stringa è la &lt;b&gt;codifica Base64&lt;/b&gt; dell&#39;immagine &lt;i&gt;immagine.jpg&lt;/i&gt; presente in &lt;i&gt;C:,&amp;nbsp;&lt;/i&gt;la seconda è l&#39;&lt;b&gt;immagine decodificata&lt;/b&gt; (in pratica il risultato equivalente all&#39;apertura dell&#39;immagine con il &lt;i&gt;blocco note&lt;/i&gt;).&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/8406210923865443518/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-unimmagine-in.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8406210923865443518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8406210923865443518'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-unimmagine-in.html' title='Codifica e decodifica di un&#39;immagine in Base64'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-6372482684077649416</id><published>2012-11-22T09:00:00.000+01:00</published><updated>2012-11-22T09:00:13.139+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Apache Commons"/><category scheme="http://www.blogger.com/atom/ns#" term="Base64"/><category scheme="http://www.blogger.com/atom/ns#" term="codifica"/><category scheme="http://www.blogger.com/atom/ns#" term="commons-codec.jar"/><category scheme="http://www.blogger.com/atom/ns#" term="conversione"/><category scheme="http://www.blogger.com/atom/ns#" term="decodifica"/><title type='text'>Codifica e decodifica di una stringa in Base64</title><content type='html'>&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;&lt;h3 style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si converte una stringa in Base64 con Java?&lt;/span&gt;&lt;/h3&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;In &lt;b&gt;Java &lt;/b&gt;esiste una classe chiamata &lt;b&gt;Base64 &lt;/b&gt;della libreria &lt;b&gt;Apache Commons &lt;/b&gt;che permette in pochi passi di convertire una stringa in &lt;b&gt;Base64&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Scarichiamo quindi il pacchetto&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;b style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;a href=&quot;http://lancill.altervista.org/BLOG/commons-codec-1.2.jar&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;commons-codec-1.2.jar&lt;/a&gt;&amp;nbsp;&lt;/b&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;ed importiamolo nel nostro progetto.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Una volta effettuate correttamente queste operazioni, guardiamo l&#39;esempio che effettua &lt;b&gt;codifica&lt;/b&gt; e &lt;b&gt;decodifica&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:java&quot;&gt;package base64;

import org.apache.commons.codec.binary.Base64;

public class Base64Conversion {
  public static void main(String[] args) {

    String daConvertire = &quot;Stringa originale&quot;;

    // CODIFICA BASE64
    byte[] encoded = Base64.encodeBase64(daConvertire.getBytes()); 
    System.out.println(&quot;Stringa da convertire: &quot; + daConvertire );
    System.out.println(&quot;Base64: &quot; + new String(encoded));

    // DECODIFICA BASE64
    byte[] decoded = Base64.decodeBase64(encoded); 
    System.out.println(&quot;Stringa decodificata : &quot; + new String(decoded));
  }
}
&lt;/pre&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codice&quot;&gt;Stringa da convertire: Stringa originale&lt;br /&gt;
Base64: U3RyaW5nYSBvcmlnaW5hbGU=&lt;br /&gt;
Stringa decodificata : Stringa originale&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per la codifica &lt;b&gt;Base64 &lt;/b&gt;delle immagini consiglio la lettura di &lt;a href=&quot;http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-unimmagine-in.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;questo post&lt;/b&gt;&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/6372482684077649416/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-una-stringa-in.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6372482684077649416'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/6372482684077649416'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/codifica-e-decodifica-di-una-stringa-in.html' title='Codifica e decodifica di una stringa in Base64'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-1315140278269059277</id><published>2012-11-21T09:00:00.000+01:00</published><updated>2012-11-21T09:00:15.352+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="compressione"/><category scheme="http://www.blogger.com/atom/ns#" term="conversione"/><category scheme="http://www.blogger.com/atom/ns#" term="image"/><category scheme="http://www.blogger.com/atom/ns#" term="ImageWriter"/><category scheme="http://www.blogger.com/atom/ns#" term="immagine"/><category scheme="http://www.blogger.com/atom/ns#" term="jpeg"/><category scheme="http://www.blogger.com/atom/ns#" term="jpg"/><title type='text'>Comprimere e convertire immagini in JPG</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;h3&gt;&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come convertire un&#39;immagine in JPG in Java?&lt;/span&gt;&lt;/h3&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;h2&gt;&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Java&lt;/b&gt;&amp;nbsp;mette a disposizione diversi oggetti che permettono la &lt;b&gt;conversione&lt;/b&gt; di un&#39;immagine in &lt;b&gt;JPG/JPEG&lt;/b&gt; e anche la regolazione della &lt;b&gt;compressione &lt;/b&gt;(e quindi della qualità).&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo l&#39;esempio &lt;b&gt;TestCompressione&lt;/b&gt;, opportunamente commentato per capire tutti i passaggi da intraprendere:&lt;/span&gt;&lt;/div&gt;&lt;pre class=&quot;brush:java&quot;&gt;package immagini;

import java.awt.image.*;
import java.io.*;
import java.util.Iterator;

import javax.imageio.*;
import javax.imageio.stream.FileImageOutputStream;

import javax.swing.JFileChooser;

public class TestCompressione {
  public static void main(String args[]) throws Exception {

    BufferedImage immagine = null;

    // Seleziono il file con finestra di dialogo
    JFileChooser fileScelto = new JFileChooser(&quot;C:\\&quot;);

    int selezione = fileScelto.showDialog(null, &quot;Seleziona il file da aprire&quot;);
    if(selezione == JFileChooser.APPROVE_OPTION)
       immagine = ImageIO.read(fileScelto.getSelectedFile());

    System.out.println(&quot;Il file selezionato è: &quot;+fileScelto.getSelectedFile());

    System.out.println(&quot;Inizio procedure compressione&quot;);
    // recupero gli ImageWriters che possono convertire in jpeg
    Iterator&amp;lt;ImageWriter&amp;gt; it = ImageIO.getImageWritersByFormatName(&quot;jpg&quot;);
    ImageWriter jpgWriter = it.next(); // prendo il primo disponibile
    ImageWriteParam parametri = jpgWriter.getDefaultWriteParam();
    parametri.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    parametri.setCompressionQuality(0.7f); // Imposto la qualità di compressione a 0.7 (0 massima compressione)

    // scrivo in output l&#39;immagine risultante
    FileImageOutputStream out = new FileImageOutputStream(new File(&quot;C:\\imgCompressa.jpg&quot;));
    jpgWriter.setOutput(out);
    jpgWriter.write(null, new IIOImage(immagine, null, null), parametri);
    jpgWriter.dispose();
    out.close();

    System.out.println(&quot;Fine procedure compressione, il file è stato creato&quot;);
  }
}&lt;/pre&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; sarà molto semplice:&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codice&quot;&gt;&lt;span style=&quot;color: #444444;&quot;&gt;Il file selezionato è: C:\immagine.bmp&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Inizio procedure compressione&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Fine procedure compressione, il file è stato creato&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E ovviamente in&amp;nbsp;&lt;i&gt;C:\&lt;/i&gt; sarà generato il file &lt;i&gt;imgCompressa.jpg&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;Java &lt;/b&gt;mette a disposizione diversi &lt;b&gt;ImageWriters &lt;/b&gt;(non solo &lt;i&gt;JPG&lt;/i&gt;) come ad esempio &lt;i&gt;BMP&lt;/i&gt;, &lt;i&gt;PNG &lt;/i&gt;e &lt;i&gt;GIF&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per ulteriori informazioni a riguardo rimando alla &lt;a href=&quot;http://docs.oracle.com/javase/1.4.2/docs/api/javax/imageio/ImageWriter.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;documentazione ufficiale&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per approfondimenti sul funzionamento e sull&#39;utilizzo del &lt;i&gt;JFileChooser&lt;/i&gt; vi consiglio la lettura di&lt;b&gt; &lt;a href=&quot;http://lancill.blogspot.com/2012/10/creare-una-finestra-di-scelta-file-con.html&quot; target=&quot;_blank&quot;&gt;questo post&lt;/a&gt;&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/1315140278269059277/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/comprimere-e-convertire-immagini-in-jpg.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/1315140278269059277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/1315140278269059277'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/comprimere-e-convertire-immagini-in-jpg.html' title='Comprimere e convertire immagini in JPG'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-5462172161692739682</id><published>2012-11-19T09:00:00.000+01:00</published><updated>2012-11-19T09:00:06.763+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="=="/><category scheme="http://www.blogger.com/atom/ns#" term="equals()"/><category scheme="http://www.blogger.com/atom/ns#" term="operatore di confronto"/><title type='text'>Differenza tra equals() ed ==</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Che differenza c&#39;è tra il metodo equals() e ==?&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Utilizzare il metodo &lt;b&gt;equals(Object o)&lt;/b&gt; ereditato da &lt;i&gt;Object()&lt;/i&gt; o utilizzare l&#39;&lt;b&gt;operatore di confronto ==&lt;/b&gt; è estremamente differente.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;METODO equals()&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Utilizzando questo metodo stiamo confrontando due oggetti secondo dei criteri di uguaglianza di &lt;b&gt;contenuto&lt;/b&gt;, cioè potremmo confrontare una persona con un&#39;altra persona, un animale con un altro animale e così via. Il metodo equals() permette infatti, con opportune personalizzazioni, di selezionare quali sono i criteri di confronto. Così potremo decidere se una persona è uguale ad un&#39;altra se hanno nome e cognome uguali, oppure nome, cognome e data di nascita, ecc... ecc...&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;OPERATORE ==&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Questo operatore di confronto valuta l&#39;uguaglianza sostanziale, ovvero se l&#39;oggetto, il riferimento o il tipo di dato primitivo a sinistra è effettivamente lo stesso di quello a destra. Quindi risulterà vero soltanto quando &lt;b&gt;i riferimenti di memoria coincideranno&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Vediamo la differenza&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;con un esempio pratico in &lt;b&gt;Java &lt;/b&gt;usando una classe &lt;b&gt;Coordinate &lt;/b&gt;che ha come attributi soltanto i due interi x e y:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Classe &lt;b&gt;Coordinate&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package equals;

public class Coordinate {

  private int x;
  private int y;

  public Coordinate(int x, int y){
    this.x = x;
    this.y = y;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  @Override
  public boolean equals(Object coordinate){
    Coordinate c = (Coordinate) coordinate;
    return (this.x == c.getX()) &amp;amp;&amp;amp; (this.y == c.getY()); // restituisco vero solo se sia la x che la y coincidono
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Classe di &lt;b&gt;Test&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package equals;

public class TestUguaglianza {
  public static void main(String[] args) {

    Coordinate c1 = new Coordinate(13, 15);
    Coordinate c2 = new Coordinate(16, 17);
    Coordinate c3 = new Coordinate(13, 15);
    Coordinate c4 = c1;

    System.out.println(&quot;CONFRONTO 1: c1(13,15) - c2(16,17)&quot;);
    System.out.println(&quot;c1 == c2? &quot; + (c1 == c2));
    System.out.println(&quot;c1.equals(c2)? &quot; + c1.equals(c2));
    System.out.println();

    System.out.println(&quot;CONFRONTO 2: c1(13,15) - c3(13,15)&quot;);
    System.out.println(&quot;c1 == c3? &quot; + (c1 == c3));
    System.out.println(&quot;c1.equals(c3)? &quot; + c1.equals(c3));
    System.out.println();

    System.out.println(&quot;CONFRONTO 3: c4 -&amp;gt; c1(13,15)&quot;);
    System.out.println(&quot;c1 == c4? &quot; + (c1 == c4));
    System.out.println(&quot;c1.equals(c4)? &quot; + c1.equals(c4));
  }
}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Infine l&#39;&lt;b&gt;output&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;CONFRONTO 1: c1(13,15) - c2(16,17)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1 == c2? false&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1.equals(c2)? false&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;color: #444444;&quot;&gt;CONFRONTO 2: c1(13,15) - c3(13,15)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1 == c3? false&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1.equals(c3)? true&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;color: #444444;&quot;&gt;CONFRONTO 3: c4 -&amp;gt; c1(13,15)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1 == c4? true&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;c1.equals(c4)? true&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Commentiamo brevemente i risultati associando un&#39;immagine esplicativa:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;CONFRONTO 1&lt;/b&gt; - c1 == c2 è &lt;i&gt;false &lt;/i&gt;perché ho due oggetti che risiedono in locazioni di memoria differenti. Il confronto con &lt;i&gt;equals()&lt;/i&gt; è altrettanto falso perché il test dei contenuti tra la x e la y è effettivamente diverso.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhORTn7YmdzA_mZ-Jb0_x_GcYfTGQRswWq6ZdABi9mgXSdHBr86jvjQHtaf2mEqkd__l2RtRo1104h4diG9Jyham7QvNnloq85WNsToavu8w-2r59whgAUhteJuC762DKtHmbPW3idZYriK/s1600/confronto3.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;255&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhORTn7YmdzA_mZ-Jb0_x_GcYfTGQRswWq6ZdABi9mgXSdHBr86jvjQHtaf2mEqkd__l2RtRo1104h4diG9Jyham7QvNnloq85WNsToavu8w-2r59whgAUhteJuC762DKtHmbPW3idZYriK/s400/confronto3.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;CONFRONTO 1:&amp;nbsp;c1 == c2 FALSE - c1.equals(c2) FALSE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;CONFRONTO 2&lt;/b&gt; - c1 == c3 è &lt;i&gt;false &lt;/i&gt;nonostante i valori delle coordinate siano uguali. Questo perché, come già detto, == confronta i &lt;b&gt;riferimenti &lt;/b&gt;di memoria che risultano, quindi, differenti. Il secondo invece, dato che confronta i valori delle coordinate x e y, restituisce &lt;i&gt;true&lt;/i&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik83vSTiWLgPvyRrEa1Mjx4vUAZL6jYtuMMHC3Jtl0uG4hPOsQo8WR2LiK3FqJtF9M3kPFlJGIn1FpcKgBD7wCVQ_rXtN_PkHYU4gDf-_oQv8qKv8mJbj5CNfmED09VA5k78euv9sHl4RH/s1600/confronto1.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik83vSTiWLgPvyRrEa1Mjx4vUAZL6jYtuMMHC3Jtl0uG4hPOsQo8WR2LiK3FqJtF9M3kPFlJGIn1FpcKgBD7wCVQ_rXtN_PkHYU4gDf-_oQv8qKv8mJbj5CNfmED09VA5k78euv9sHl4RH/s400/confronto1.PNG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;CONFRONTO 2: c1 == c3 FALSE - c1.equals(c3) TRUE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt; &lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;CONFRONTO 3&lt;/b&gt; - In questo caso abbiamo entrambi i confronti &lt;i&gt;true &lt;/i&gt;perché il riferimento in memoria è lo stesso (c4 punta alla locazione di memoria c1) e di conseguenza anche i valori confrontati sono uguali.&lt;/span&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxKOxb79bmb7H7dMPev62WYTL8zbnsWeE3T6hZ8EBhfcXga4u06QJ_SMtWDMu1cL4dcMKKOqfQusDmaflzlCWwfVqFYad2gQ4QXtVnj6CSiF5zRdlMpywJuL_GrJSPFFgwjNARWagkll1p/s1600/confronto2.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxKOxb79bmb7H7dMPev62WYTL8zbnsWeE3T6hZ8EBhfcXga4u06QJ_SMtWDMu1cL4dcMKKOqfQusDmaflzlCWwfVqFYad2gQ4QXtVnj6CSiF5zRdlMpywJuL_GrJSPFFgwjNARWagkll1p/s320/confronto2.PNG&quot; width=&quot;269&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;CONFRONTO 3:&amp;nbsp;c1 == c4 TRUE - c1.equals(c4) TRUE&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/5462172161692739682/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/differenza-tra-equals-ed.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5462172161692739682'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/5462172161692739682'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/differenza-tra-equals-ed.html' title='Differenza tra equals() ed =='/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhORTn7YmdzA_mZ-Jb0_x_GcYfTGQRswWq6ZdABi9mgXSdHBr86jvjQHtaf2mEqkd__l2RtRo1104h4diG9Jyham7QvNnloq85WNsToavu8w-2r59whgAUhteJuC762DKtHmbPW3idZYriK/s72-c/confronto3.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-1500603287860453395</id><published>2012-11-15T09:00:00.000+01:00</published><updated>2012-11-15T09:00:15.200+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java.math.BigInteger"/><category scheme="http://www.blogger.com/atom/ns#" term="Range di valori rappresentabili"/><title type='text'>Rappresentare numeri grandissimi</title><content type='html'>&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;E&#39; possibile rappresentare in Java dei numeri grandissimi che hanno un valore oltre quello consentito dai tipi di dati primitivi long e int?&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come abbiamo visto nel post sul &lt;b&gt;&lt;a href=&quot;http://lancill.blogspot.com/2012/06/tipi-primitivi-range-di.html&quot; target=&quot;_blank&quot;&gt;range di valori rappresentabili&lt;/a&gt;&lt;/b&gt; con i diversi tipi di dati primitivi, effettuare un&#39;operazione il cui risultato vada oltre tale limite comporta un errore di calcolo con conseguenze potenzialmente disastrose.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La soluzione a tali limitazioni esiste: la prima, più complessa, rientra nell&#39;analisi numerica e in algoritmi di troncamento, arrotondamento e bit shifting, ma non è scopo di questo post.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La seconda invece è messa a disposizione da &lt;b&gt;Java&lt;/b&gt; con le classi &lt;b&gt;java.math.BigInteger&lt;/b&gt; e &lt;b&gt;java.math.BigDecimal&lt;/b&gt; (rispettivamente per i numeri interi e decimali).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo con un esempio come funzionano:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package bignumbers;

import java.math.BigInteger;

public class BigNumbers {
  public static void main(String[] args) {

    // rappresentazione errata oltre i limiti
    long primo = 9999999999999L;
    long secondo = 9999999999999L;
    long risultato = primo * secondo;
    System.out.println(&quot;Risultato primo * secondo = &quot; + risultato);

    // rappresentazione corretta
    BigInteger bigPrimo = new BigInteger(&quot;9999999999999&quot;);
    BigInteger bigSecondo = new BigInteger(&quot;9999999999999&quot;);
    BigInteger bigRisultato = bigPrimo.multiply(bigSecondo); 
    System.out.println(&quot;Risultato bigPrimo * bigSecondo = &quot; + bigRisultato);
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; sarà:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Risultato primo * secondo = -2537784290115403775&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;Risultato bigPrimo * bigSecondo = 99999999999980000000000001&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come si può vedere, nel primo caso abbiamo avuto un risultato &lt;b&gt;errato &lt;/b&gt;dovuto al superamento dei limiti rappresentabili dal tipo di dato &lt;i&gt;long&lt;/i&gt;. Nel secondo, gli algoritmi della classe &lt;b&gt;BigInteger &lt;/b&gt;ci hanno permesso di &lt;b&gt;superare&lt;/b&gt; il range rappresentabile e stampare il risultato corretto.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La classe &lt;b&gt;BigInteger&lt;/b&gt;,&lt;b&gt;&amp;nbsp;&lt;/b&gt;così come la classe &lt;b&gt;BigDecimal&lt;/b&gt;, non ha limiti ben definiti come i tipi di dati primitivi, ma dipende dalla dotazione hardware del calcolatore.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Queste classi forniscono anche altri metodi per effettuare le diverse operazioni aritmetiche e anche quelle logiche (come ad esempio il confronto). Per ulteriori approfondimenti vi rimando alla &lt;a href=&quot;http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;documentazione ufficiale&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/1500603287860453395/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/rappresentare-numeri-grandissimi.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/1500603287860453395'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/1500603287860453395'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/rappresentare-numeri-grandissimi.html' title='Rappresentare numeri grandissimi'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-8867371632461602587</id><published>2012-11-13T09:37:00.000+01:00</published><updated>2012-11-13T16:07:45.902+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java.awt.Desktop"/><category scheme="http://www.blogger.com/atom/ns#" term="java.io.File"/><category scheme="http://www.blogger.com/atom/ns#" term="print"/><category scheme="http://www.blogger.com/atom/ns#" term="stampa"/><title type='text'>Stampare un file su carta</title><content type='html'>&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si manda in stampa un file in Java?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ci sono diversi modi per &lt;b&gt;stampare un file su carta&lt;/b&gt; direttamente da &lt;b&gt;Java&lt;/b&gt;. Vediamo quello più veloce ed immediato grazie alla classe &lt;b&gt;java.awt.Desktop&lt;/b&gt; introdotta a partire da Java 6:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package stampa;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javax.print.PrintException;

public class StampaFile {
&amp;nbsp; public static void main(String[] args) throws PrintException, IOException {

&amp;nbsp; &amp;nbsp; if(!java.awt.Desktop.isDesktopSupported()) {
&amp;nbsp; &amp;nbsp; &amp;nbsp; System.out.println(&quot;Funzione non supportata!&quot;);
&amp;nbsp; &amp;nbsp; &amp;nbsp; return;
&amp;nbsp; &amp;nbsp; }

&amp;nbsp; &amp;nbsp; try {
&amp;nbsp; &amp;nbsp; &amp;nbsp; Desktop desk = java.awt.Desktop.getDesktop();

&amp;nbsp; &amp;nbsp; &amp;nbsp; // mando in stampa il file
&amp;nbsp; &amp;nbsp; &amp;nbsp; desk.print(new File(&quot;C:\\prova.pdf&quot;));

&amp;nbsp; &amp;nbsp; } catch (NullPointerException | IllegalArgumentException | UnsupportedOperationException |
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;      IOException | SecurityException ex) {
&amp;nbsp; &amp;nbsp; &amp;nbsp; ex.printStackTrace();
&amp;nbsp; &amp;nbsp; }
&amp;nbsp; }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Eseguendo questa classe invierete direttamente il file alla stampante senza dover confermare nulla, nemmeno nella finestra di dialogo.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E&#39; importante precisare che tale metodo di stampa è estremamente semplice perché demanda tutto al gestore&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;predefinito&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;del file del Sistema Operativo. Nel nostro caso, se non avessimo un gestore per i file &lt;i&gt;PDF &lt;/i&gt;(come ad esempio &lt;i&gt;Adobe Reader&lt;/i&gt;) riceveremmo un&#39;eccezione. Stesso dicasi per la stampante: è necessario averne impostata una predefinita.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Un&#39;ulteriore limitazione di questo metodo è l&#39;impossibilità di gestire e regolare le diverse impostazioni di stampa.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/8867371632461602587/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/stampare-un-file-su-carta.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8867371632461602587'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/8867371632461602587'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/stampare-un-file-su-carta.html' title='Stampare un file su carta'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-464090230866802573</id><published>2012-11-09T09:05:00.000+01:00</published><updated>2012-11-09T09:05:08.008+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Annotations"/><category scheme="http://www.blogger.com/atom/ns#" term="Override"/><title type='text'>Annotations: @Override</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Perché si utilizza l&#39;annotazione @Override? A cosa serve e quali sono i vantaggi?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;@Override&lt;/b&gt; è una annotazione aggiunta a partire da &lt;b&gt;Java 5&lt;/b&gt; ed utilizzata per indicare al compilatore che il metodo su cui è posta sta effettuando un &lt;b&gt;override &lt;/b&gt;(sovrascrittura)&amp;nbsp;di un metodo della sua superclasse o, da &lt;b&gt;Java 6&lt;/b&gt;, anche di un&#39;interfaccia.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La domanda nasce spontanea: &lt;i&gt;&quot;cosa cambia se non lo utilizzo?&quot;&lt;/i&gt;.&amp;nbsp;Proviamo a fare un &lt;b&gt;override&lt;/b&gt; sul metodo &lt;i&gt;equals()&lt;/i&gt; della superclasse &lt;i&gt;Object&lt;/i&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public class Animale{
  private String nome;

  public boolean equals(Animale&amp;nbsp;animale){
    return nome.equals(animale.nome);
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come si nota non abbiamo utilizzato &lt;b&gt;@Override&lt;/b&gt;... e infatti non stiamo effettuando un &lt;b&gt;override&lt;/b&gt;, bensì un &lt;i&gt;overloading&lt;/i&gt;! Sicuramente la maggior parte di voi non avrà fatto caso all&#39;errore. Il metodo &lt;i&gt;equals()&lt;/i&gt; ereditato della classe &lt;i&gt;Object &lt;/i&gt;accetta come parametro un &lt;i&gt;Object &lt;/i&gt;(non un &lt;i&gt;Animale&lt;/i&gt;), di conseguenza il compilatore riconoscerà il metodo &lt;i&gt;equals()&amp;nbsp;&lt;/i&gt;della classe &lt;i&gt;Animale()&amp;nbsp;&lt;/i&gt;come un metodo aggiuntivo e non come uno che sovrascrive l&#39;originale.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;La risposta è quindi facile: &lt;b&gt;@Override&lt;/b&gt; nasce per soddisfare esigenze di pratiche di buona programmazione e facilitare il programmatore nel prevenire errori di difficile individuazione (come nel caso dell&#39;esempio).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Inserendo &lt;b&gt;@Override&lt;/b&gt; nella classe Animale otterremmo infatti un errore:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public class Animale{
  private String nome;
  
  @Override
  public boolean equals(Animale&amp;nbsp;animale){
    return nome.equals(animale.nome);
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il compilatore non ci permetterà di creare il .class fornendo a video il seguente messaggio:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;The method equals(Animale) of type Animale must override or implement a supertype method&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Invece, se utilizzassimo correttamente &lt;b&gt;@Override&lt;/b&gt;&amp;nbsp;potremmo compilare la classe &lt;i&gt;Animale &lt;/i&gt;correttamente:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package override;

public class Animale{
  private String nome;

  @Override
  public boolean equals(Object animale){
    return nome.equals(((Animale) animale).nome);
  }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/464090230866802573/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/11/annotations-override.html#comment-form' title='1 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/464090230866802573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/464090230866802573'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/11/annotations-override.html' title='Annotations: @Override'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-2862067163920730710</id><published>2012-10-29T09:00:00.000+01:00</published><updated>2012-10-29T09:00:06.195+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="campi privati"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Reflection"/><category scheme="http://www.blogger.com/atom/ns#" term="java.lang.IllegalAccessException"/><category scheme="http://www.blogger.com/atom/ns#" term="java.lang.Reflect"/><category scheme="http://www.blogger.com/atom/ns#" term="metodi privati"/><title type='text'>Java Reflection: accesso a metodi e campi privati</title><content type='html'>&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si può accedere da un classe generica ai metodi e alle variabili di istanza private di un&#39;altra classe?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Genericamente &lt;u&gt;non&lt;/u&gt;&lt;b&gt; &lt;/b&gt;è possibile accedere ai &lt;b&gt;campi&amp;nbsp;o metodi privati&lt;/b&gt;&amp;nbsp;di un&#39;altra classe. Ma g&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;enericamente non vuol dire in modo assoluto! Infatti, grazie alla &lt;b&gt;Reflection Java&lt;/b&gt; possiamo accedere ai &lt;b&gt;campi privati&lt;/b&gt; di un&#39;altra classe.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Analizziamo alcuni metodi che ci mette a disposizione la classe &lt;b&gt;java.lang.Class&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;getFields()&lt;/b&gt;: permette di recuperare la lista di tutti i campi pubblici della classe e della sua superclasse;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;getDeclaredFields()&lt;/b&gt;: fornisce la lista di tutti i campi presenti nella classe (senza distinzione di visibilità);&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;getDeclaredMethods()&lt;/b&gt;: come sopra ma applicato ai metodi;&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;getDeclaredField(String nome)&lt;/b&gt;: restituisce il Field indicato come parametro (senza distinzione di visibilità);&lt;/span&gt;&lt;/li&gt;
&lt;li style=&quot;text-align: justify;&quot;&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;getDeclaredMethos(String nome)&lt;/b&gt;: restituisce il Method indicato come parametro (senza distinzione di visibilità).&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo l&#39;esempio, prima con la &lt;b&gt;ClassePrivata&lt;/b&gt; da analizzare:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package reflection;

public class ClassePrivata {
  public int publicValue = 99999;
  protected int protectedValue = 200;
  int packageProtectedValue = 10000;
  private int secretValue = 12345;

  public String getPublicValue(){
     return &quot;Il valore pubblico è: &quot; +publicValue;
  }

  protected String getProtectedValue(){
     return &quot;Il valore protected è: &quot; +protectedValue;
  }

  String getPackageProtectedValue(){
     return &quot;Il valore packageProtected è: &quot;+packageProtectedValue;
  }

  private String getSecretValue(){
     return &quot;Il valore segreto è: &quot; +secretValue;
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;E poi con la classe analizzatrice &lt;b&gt;ElencoCampiMetodi&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package reflection;

import java.util.Arrays;

public class ElencoCampiMetodi {
  public static void main(String args[]) throws ClassNotFoundException {

    Class&amp;lt;ClassePrivata&amp;gt; pvtClass = 
         (Class&amp;lt;ClassePrivata&amp;gt;) Class.forName(&quot;reflection.ClassePrivata&quot;);

    //getFields() non restituisce i campi privati
    System.out.println(&quot;Fields: &quot; + 
                   Arrays.toString(pvtClass.getFields()));

    //getDeclaredFields() restituisce sia i campi privati che non
    System.out.println(&quot;Declared Fields: &quot; +
                   Arrays.toString(pvtClass.getDeclaredFields()));    

    //getDeclaredMethods() restituisce sia i metodi privati che non
    System.out.println(&quot;Declared methods: &quot; +
                   Arrays.toString(pvtClass.getDeclaredMethods()));
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt;, come previsto, sarà:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Fields:&lt;/b&gt; [public int reflection.ClassePrivata.publicValue]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Declared Fields:&lt;/b&gt; [public int reflection.ClassePrivata.publicValue, int reflection.ClassePrivata.packageProtectedValue, private int reflection.ClassePrivata.secretValue]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Declared methods:&lt;/b&gt; [private java.lang.String reflection.ClassePrivata.getSecretValue(), public java.lang.String reflection.ClassePrivata.getPublicValue()]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Passiamo ora al &lt;b&gt;recupero&lt;/b&gt; vero e proprio del valore di un &lt;b&gt;campo privato&lt;/b&gt; e all&#39;&lt;b&gt;esecuzione&lt;/b&gt; di un &lt;b&gt;metodo privato&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public class PrivateAccessTest {
  public static void main(String args[]) throws ClassNotFoundException {

    Class&amp;lt;ClassePrivata&amp;gt; pvtClass = 
         (Class&amp;lt;ClassePrivata&amp;gt;) Class.forName(&quot;reflection.ClassePrivata&quot;);

    try {
      ClassePrivata myClassPvt = new ClassePrivata();

      // recupero il campo privato e lo rendo accessibile
      Field campoPrivato = pvtClass.getDeclaredField(&quot;secretValue&quot;);
      campoPrivato.setAccessible(true);

      // recupero il valore del campo privato
      int valore =  campoPrivato.getInt(myClassPvt);   

      // stampo il campo privato e il suo valore
      System.out.println(&quot;Campo Privato: &quot; + campoPrivato);
      System.out.println(&quot;Valore campo privato: &quot; + valore);

      // recupero il metodo privato e lo rendo accessibile
      Method metodoPrivato = pvtClass.getDeclaredMethod(&quot;getSecretValue&quot;);
      metodoPrivato.setAccessible(true);

      // recupero il valore di ritorno del metodo privato
      String result = (String) metodoPrivato.invoke(myClassPvt);
      System.out.println(result);

    } catch (IllegalAccessException | IllegalArgumentException
           | InvocationTargetException | NoSuchFieldException
           | SecurityException | NoSuchMethodException e) {
      e.printStackTrace();
    }
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;L&#39;&lt;b&gt;output&lt;/b&gt; anche in questo caso ci darà conferma del risultato voluto:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Campo Privato:&lt;/b&gt; private int reflection.ClassePrivata.secretValue&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Valore campo privato:&lt;/b&gt; 12345&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;Il valore segreto è:&lt;/b&gt; 12345&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;&lt;u&gt;NOTA&lt;/u&gt;&lt;/b&gt;: per accedere ad un campo o metodo privato dobbiamo renderlo accessibile grazie al metodo &lt;b&gt;setAccessible(true)&lt;/b&gt;. Se non lo facessimo, appena tentiamo l&#39;accesso al campo o al metodo ci ritroveremo un&#39;&lt;b&gt;eccezione&lt;/b&gt; di questo tipo:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;codice&quot;&gt;
&lt;span style=&quot;color: #444444;&quot;&gt;&lt;b&gt;java.lang.IllegalAccessException:&lt;/b&gt; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; Class reflection.PrivateAccessTest can not access a member of class reflection.ClassePrivata with modifiers &quot;private&quot;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;&lt;u&gt;NOTA FINALE&lt;/u&gt;&lt;/b&gt;: il codice utilizza il &lt;b&gt;multicatch &lt;/b&gt;introdotto da &lt;b&gt;Java 7&lt;/b&gt;. Se state utilizzando una versione precedente di Java dovete sostituirlo con il &lt;i&gt;catch&lt;/i&gt; alla vecchia maniera! Per ulteriori informazioni sul multicatch vi invito a leggere questo &lt;a href=&quot;http://lancill.blogspot.com/2012/10/java-7-multicatch.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;post&lt;/b&gt;&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/2862067163920730710/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/10/java-reflection-accesso-metodi-e-campi.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/2862067163920730710'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/2862067163920730710'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/10/java-reflection-accesso-metodi-e-campi.html' title='Java Reflection: accesso a metodi e campi privati'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-50821757947256975</id><published>2012-10-22T09:00:00.000+02:00</published><updated>2012-10-23T11:35:14.264+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="eccezioni"/><category scheme="http://www.blogger.com/atom/ns#" term="java 7"/><category scheme="http://www.blogger.com/atom/ns#" term="multicatch"/><title type='text'>Java 7: Multicatch</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;In un try/catch c&#39;è modo di scrivere più eccezioni nello stesso catch evitando così codice ridondante nel caso di stesso comportamento?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Fino a Java 6 succede una cosa alquanto scomoda nell&#39;utilizzo del &lt;b&gt;try/catch&lt;/b&gt;: quando ci troviamo di fronte ad un codice che ha lo &lt;b&gt;stesso comportamento&lt;/b&gt; con &lt;b&gt;diverse eccezioni&lt;/b&gt;, siamo costretti a dover ripetere lo &lt;b&gt;stesso codice&lt;/b&gt; più volte dentro diversi &lt;b&gt;catch&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo nel dettaglio un esempio in cui posso avere due eccezioni a &lt;i&gt;runtime&lt;/i&gt;: una &lt;i&gt;NumberFormatException &lt;/i&gt;qualora inserisca in input un formato non compatibile con la conversione in &lt;i&gt;int &lt;/i&gt;(ad esempio delle lettere), oppure una &lt;i&gt;ArithmeticException&lt;/i&gt; qualora inserisca un numero non compreso nell&#39;intervallo 0 e 5 (estremi esclusi):&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package multicatch;

import java.util.Scanner;

public class OldCatch {
  public static void main(String args[]) {
    try {
      Scanner scanner = new Scanner(System.in);
      String number = scanner.next();
      scanner.close();
      
      int num = Integer.parseInt(number);
    
      if (num &amp;lt; 0 || num &amp;gt; 5)
        throw new ArithmeticException();
 
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } catch (ArithmeticException e) {
      e.printStackTrace();
    }
  }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per risolvere la ridondanza possiamo utilizzare uno stratagemma di questo tipo:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package multicatch;

import java.util.Scanner;

public class OldCatchAdvanced {
  public static void main(String args[]) {
    try {
      Scanner scanner = new Scanner(System.in);
      String number = scanner.next();
      scanner.close();

      int num = Integer.parseInt(number);

      if (num &amp;lt; 0 || num &amp;gt; 5)
        throw new ArithmeticException();

    } catch (Exception e) {
      if((e instanceof NumberFormatException) || (e instanceof ArithmeticException))
        e.printStackTrace();
      else
        throw new RuntimeException(e);
    }
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;&lt;b&gt;NOTA&lt;/b&gt;&lt;/u&gt;: Personalmente trovo questo stratagemma abbastanza &quot;sporco&quot; e ne sconsiglio l&#39;utilizzo.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;b&gt;JAVA 7&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per fortuna, le problematiche dei programmatori Java non restano inascoltate e da &lt;b&gt;Java 7&lt;/b&gt; in poi è stato introdotto il &lt;b&gt;multicatch&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo come possiamo riscrivere il codice di sopra:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package multicatch;

import java.util.Scanner;

public class MultiCatch {
  public static void main(String args[]) {
    try {
      Scanner scanner = new Scanner(System.in);
      String number = scanner.next();
      scanner.close();

      int num = Integer.parseInt(number);
      
      if (num &amp;lt; 0 || num &amp;gt; 5)
        throw new ArithmeticException();

    } catch (NumberFormatException | ArithmeticException e) {
      e.printStackTrace();
    }
  }
}&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Come è facile capire dall&#39;esempio, qualora avessimo più eccezioni che scatenano comportamenti simili, basterà dividerle con un &lt;b&gt;|&lt;/b&gt; (&lt;a href=&quot;http://lancill.blogspot.com/2012/05/and-e-or-bit-bit-e-logici.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;OR bit a bit&lt;/b&gt;&lt;/a&gt;), risolvendo così i problemi di ridondanza di codice. Negli esempi abbiamo utilizzato solo due eccezioni ma è possibile concatenarne quante vogliamo.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/50821757947256975/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/10/java-7-multicatch.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/50821757947256975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/50821757947256975'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/10/java-7-multicatch.html' title='Java 7: Multicatch'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4223459690803294564.post-7569351439515342026</id><published>2012-10-17T09:00:00.000+02:00</published><updated>2012-10-21T11:59:27.504+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="FileFilter"/><category scheme="http://www.blogger.com/atom/ns#" term="javax.swing.JFileChooser"/><category scheme="http://www.blogger.com/atom/ns#" term="selezione File"/><title type='text'>Creare una finestra di scelta file con JFileChooser</title><content type='html'>&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;DOMANDA:&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif; font-size: large;&quot;&gt;Come si fa a visualizzare una finestra per la scelta di un file in Java?&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h2&gt;
&lt;span style=&quot;color: #666666; font-family: Verdana, sans-serif; font-size: large;&quot;&gt;RISPOSTA:&lt;/span&gt;&lt;/h2&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Java mette a disposizione la classe &lt;b&gt;javax.swing.JFileChooser&lt;/b&gt; che permette di aprire con poche righe di codice una &lt;b&gt;finestra di selezione (Dialog) per i file&lt;/b&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Guardiamo l&#39;esempio con la classe &lt;b&gt;FileDialog&lt;/b&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package file;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class FileDialog {
  public static void main(String[] args) {
    
    // impostazione della directory di partenza
    JFileChooser fileScelto = new JFileChooser(&quot;C:\\&quot;);
    
    // creazione di due filtri
    FileFilter filtroJpg = new FileNameExtensionFilter(&quot;JPEG file&quot;, &quot;jpg&quot;, &quot;jpeg&quot;);
    FileFilter filtroTxt = new FileNameExtensionFilter(&quot;TXT file&quot;, &quot;txt&quot;);
    
    // inserisco nella scelta del tipo di file un filtro TXT
    fileScelto.addChoosableFileFilter(filtroTxt);
    // di default filtro solo i file Jpg (lo aggiunge in automatico alla combo sottostante)
    fileScelto.setFileFilter(filtroJpg);
    // imposto di default il nome del file da cercare
    fileScelto.setSelectedFile(new File(&quot;immagine.jpg&quot;));

    int selezione = fileScelto.showDialog(null, &quot;Seleziona il file da aprire&quot;);
    if(selezione == JFileChooser.APPROVE_OPTION)
      System.out.println(fileScelto.getSelectedFile());
  }
}
&lt;/pre&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Quindi eseguendo l&#39;esempio ci aspettiamo l&#39;apertura di una finestra di questo tipo:&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6RyiizJ4KdMp9kFbVhhyphenhyphenrqvRS9cKAwV9kC6Tnm_lCVmtnvoMDP4_o-2-1so1kGuGFlZ-f12hZQp2EaGvpYqbC0t80r-_iVwWhPSvnREmGS6MghdILthqPjUZXcmZqfaAHTHVn9JeqDFCi/s1600/jfilechooser1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;228&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6RyiizJ4KdMp9kFbVhhyphenhyphenrqvRS9cKAwV9kC6Tnm_lCVmtnvoMDP4_o-2-1so1kGuGFlZ-f12hZQp2EaGvpYqbC0t80r-_iVwWhPSvnREmGS6MghdILthqPjUZXcmZqfaAHTHVn9JeqDFCi/s320/jfilechooser1.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;JFileChooser - All&#39;apertura&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;...e nella select &quot;Tipo File&quot; i filtri personalizzati.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP5KSQl6WMd8MRgu0QXKhbzX6VjbsRbHlkebH5Se2r85zng7ZrM15TqHbZIkPiGTVdajhEozLiM1ly-vs-N3uBBZQSoah0OfSbe-MvQf1WoZyIPGNAmxoUxN2HxG1cnOxunkwpVKh2M6ZO/s1600/jfilechooser2.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;226&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP5KSQl6WMd8MRgu0QXKhbzX6VjbsRbHlkebH5Se2r85zng7ZrM15TqHbZIkPiGTVdajhEozLiM1ly-vs-N3uBBZQSoah0OfSbe-MvQf1WoZyIPGNAmxoUxN2HxG1cnOxunkwpVKh2M6ZO/s320/jfilechooser2.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;JFileChooser - Combo di selezione Tipo File (Filtri)&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Ora che abbiamo visto l&#39;effetto grafico, anche se i commenti all&#39;interno del codice chiarificano già lo scopo delle singole righe, analizziamo brevemente qualche istruzione.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Creando i &lt;b&gt;filtri (javax.swing.filechooser.FileFilter)&lt;/b&gt; possiamo scremare la scelta del tipo di file a seconda delle nostre esigenze. Nell&#39;esempio abbiamo creato un filtro per i file &lt;i&gt;Jpeg&lt;/i&gt; e uno per i &lt;i&gt;Txt&lt;/i&gt;. Il primo parametro (&lt;i&gt;&quot;JPEG file&quot;&lt;/i&gt;) è una semplice stringa che fornisce un&#39;etichetta (visibile nella select&amp;nbsp;&lt;i&gt;Tipo file&lt;/i&gt;) associata agli effettivi filtri. Gli altri parametri (variabili da 1 a n) identificano le estensioni dei file da filtrare (nel nostro caso &lt;i&gt;.jpg&lt;/i&gt; e &lt;i&gt;.jpeg&lt;/i&gt;).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Il metodo &lt;b&gt;addChoosableFileFilter()&lt;/b&gt;, applicato sul &lt;b&gt;FileChooser&lt;/b&gt;, aggiunge nella select dei tipi di file il filtro passato come parametro (nell&#39;esempio &lt;i&gt;filtroTxt &lt;/i&gt;con etichetta&lt;i&gt; TXT file &lt;/i&gt;e associazione con le estensioni&lt;i&gt;&amp;nbsp;.txt&lt;/i&gt;). &lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Invece con il metodo &lt;b&gt;setFileFilter()&lt;/b&gt;, applicato sempre al &lt;b&gt;FileChooser&lt;/b&gt;, stiamo impostando,&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;all&#39;apertura della finestra,&lt;/span&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&amp;nbsp;un filtro di default sui file. Da notare che &lt;/span&gt;&lt;i style=&quot;font-family: Verdana, sans-serif;&quot;&gt;setFileFilter()&lt;/i&gt;&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt; in automatico aggiunge una voce alla select dei tipi di file.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Infine &lt;b&gt;setSelectedFile()&lt;/b&gt;&amp;nbsp;imposta, sempre all&#39;apertura, il nome del file che ci aspettiamo di aprire (nel nostro caso&lt;i&gt; immagine.jpg&lt;/i&gt;).&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;Per ulteriori personalizzazioni ed approfondimenti vi rimando alla relativa pagina della documentazione di &lt;a href=&quot;http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html&quot; target=&quot;_blank&quot;&gt;&lt;i&gt;&lt;b&gt;JFileChooser&lt;/b&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://lancill.blogspot.com/feeds/7569351439515342026/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://lancill.blogspot.com/2012/10/creare-una-finestra-di-scelta-file-con.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7569351439515342026'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4223459690803294564/posts/default/7569351439515342026'/><link rel='alternate' type='text/html' href='http://lancill.blogspot.com/2012/10/creare-una-finestra-di-scelta-file-con.html' title='Creare una finestra di scelta file con JFileChooser'/><author><name>Fabrizio</name><uri>http://www.blogger.com/profile/07862677803082957995</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm9_aZWXJ8LOZEFMPAtAnJwU_-0M6Mub1ZCE18h16u2IMV9ldsFm8QlAGcrMfH4J9G5ivoXn1hJuwEBtGY1I-rYEAQy50e0Gng7hMg6StNQBOxEKt_JIectYmNH5FnOp4/s220/AIbEiAIAAABECL-tgK-dwLOW1wEiC3ZjYXJkX3Bob3RvKihiOGYwNmQ2Y2FmMGYyMTFmNDlhMWU5ZjdkOTliZTgzM2QwZGJmZjAxMAH_yGfC4m_yN7rLCgdxVqFzAp_oFQ.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6RyiizJ4KdMp9kFbVhhyphenhyphenrqvRS9cKAwV9kC6Tnm_lCVmtnvoMDP4_o-2-1so1kGuGFlZ-f12hZQp2EaGvpYqbC0t80r-_iVwWhPSvnREmGS6MghdILthqPjUZXcmZqfaAHTHVn9JeqDFCi/s72-c/jfilechooser1.png" height="72" width="72"/><thr:total>0</thr:total></entry></feed>