java - jsp utf encoding -


I have a hard time finding out how to solve this problem:

I I am doing a web tool for an Italian university, and I have to display words with Accent (like è, ù, ...); Sometimes I get this word from a postgrescol table (UTF-8 encoded), but most of all I have to read a lot from a file. These files are encoded in the form of UTF-8 XML, and perform well in Smootron or any utf-8 editor (they are parsing python files in old files such as and egrave; instead of " È ").

I wrote a Java class that removes sections related to XML files, which works like this:

string s = parseText (file name, status)

If I type the returned string in a file, then everything looks fine; The problem is that if I get

outside .write (s)

in the jsp page, I get strange letters, I

< P> string s = getWordFromPostgresql (...)

.write

very jsp And it shows fine.

Any hint?

Thanks Nicola


@

Thank you for your response, although this instruction is already in the page, but it does not work In "works" but for strings received from the database). I think there is something about reading from the files, but I can not understand ... they work in "Java", but not in "JSP" (can not think of better explanation ... )

Here is a basic example derived from the actual code: the way to return the map from the mark (for the object representing the position in an object text) for the string (text containing), to read from the file Method:

There is .jsp pages (in the above positions with the specified UTF-Directive)

  // ... Map & lt; Mark, string & gt; Map = testo markparser PRSA Marks (...); Out.write (map.get (m));  

And the result of this is:

"Fu Pera Cosserno in Genie Anharmonico, Che Claely Quali VS Sertitivano,"

If I have a Java Put the same code in the code, and give the option. Write with System.out.println, the result of this is:

"Foo Pero Cos IEEEE Enormonico, Che Qually Quali VCS Spiritano,"


I have a hex editor Here's where I am doing some analysis with:

Original String: "Foo Pero Kosi"

in xml file: c3b2

Ò as is provided by out.write () in the jsp file: E2 88 9a E2 89 A4

ò As the file is written through:

  Fiver w = new freeware (new file ("OU Tax ")); W.write (s); // s parse string is w.close ();  
  0: 70 = F1: 117 = U2: 32 = 3: 112 = P4: 101 = E5: 114 = R6: 8730 = 7: 8804 = 8: 32 = 9: 99 = C10: 111 = o 11: 115 = 12: 8730 = 13: 168 = 14: 10 = ` 

In JSP page directive, you should try setting your content-type in UTF-8, which will also set page encoding in UTF-8.

  & lt;% @ Page content type = "text / html; charset = UTF-8"%>  

The default content type in UTF-8 JSP is not , and there are all kinds of interesting problems arising from this problem. The problem is that the underlying stream is called ISO- 885 9-1 is defined as the stream by default. If you write some unicode bytes in this stream, they will be interpreted as ISO-8859-1. I think setting U encoding is the best solution.

Edit : In addition, the string variable in Java should always be always unicode, so you always have

  should be able to say system.out.println (myString)  

and see the appropriate character set coming in your console window web-server ( Or just stop in the debugger and check it out). I suspect that when you do this, you may be looking at the wrong characters, so I think you have an encoding problem in the string creation.


Comments