title: jsp utf encoding
body: <p>I'm having a hard time figuring out how to handle this problem:</p>
  
  <p>I'm developing a web tool for an Italian university, and I have to display words with accents (such as è, ù, ...); sometimes I get these words from a PostgreSql table (UTF8-encoded), but mostly I have to read long passages from a file. These files are encoded as utf-8 xml, and display fine in Smultron or any utf-8 editor (they were created parsing in python old files with entities such as <code>&amp;egrave;</code> instead of "è").</p>
  
  <p>I wrote a java class which extracts the relevant segments from the xml file, which works like this:</p>
  
  <p><code>String s = parseText(filename, position)</code></p>
  
  <p>if I write the returned String to a file, everything looks fine; the problem is that if I do</p>
  
  <p><code>out.write(s)</code> </p>
  
  <p>in the jsp page, I get strange characters. By the way, I use </p>
  
  <p><code>String s = getWordFromPostgresql(...)</code></p>
  
  <p><code>out.write(s)</code></p>
  
  <p>in the very same jsp and it displays OK.</p>
  
  <p>Any hint?</p>
  
  <p>Thanks
  Nicola</p>
  
  <hr>
  
  <p>@<a href="https://stackoverflow.com/questions/488448/jsp-utf-encoding/488467#488467">krosenvold</a></p>
  
  <p>Thanks for your response, however that directive is already in the page, but it doesn't work (actually it "works" but only for the strings I get from the database). I think there's something about reading from the files, but I can't understand ... they work in "java" but not in "jsp" (can't think about a better explanation ...)</p>
  
  <p>here's a basic example extracted from the actual code: the method to read from the files return a Map, from a Mark (an object representing a position in the text) to a String (containing the text):</p>
  
  <p>this is in the .jsp page (with the utf-directive cited in the posts above)</p>
  
  <pre><code>    // ...
      Map&lt;Mark, String&gt; map = TestoMarkParser.parseMarks(...);
      out.write(map.get(m));
  </code></pre>
  
  <p>and this is the result:</p>
  
  <p>"Fu per√≤ cos√¨ in uso il Genere Enharmonico, che quelli quali vi si esercitavano,"</p>
  
  <p>if I put the same code in a java class, and substitute out.write with System.out.println, the result is this:</p>
  
  <p>"Fu però così in uso il Genere Enharmonico, che quelli quali vi si esercitavano,"</p>
  
  <hr>
  
  <p>I've been doing some analysis with an hex editor, here it is:</p>
  
  <p>original string: "fu però così "</p>
  
  <p>ò in xml file:
  C3 B2 </p>
  
  <p>ò as rendered by out.write() in the jsp file:
  E2 88 9A E2 89 A4 </p>
  
  <p>ò as written to file via:</p>
  
  <pre><code>FileWriter w = new FileWriter(new File("out.txt"));
  w.write(s);     // s is the parsed string
  w.close();
  </code></pre>
  
  <p>C3 B2</p>
  
  <p>printing the values of each character as an int</p>
  
  <pre><code>0: 70 = F
  1: 117 = u
  2: 32 =  
  3: 112 = p
  4: 101 = e
  5: 114 = r
  6: 8730 = � 
  7: 8804 = � 
  8: 32 =  
  9: 99 = c
  10: 111 = o
  11: 115 = s
  12: 8730 = �
  13: 168 = �
  14: 10 = `
  </code></pre>