3

I am using JSF 2.0 and I have text field as

<h:form>
    <h:inputText value="#{myBean.myValue}" />
    <h:commandButton value="Submit" action="#{myBean.printMe()}" />
</h:form>

public void printMe() {
    System.out.println("first line==" + myValue + "==");
    System.out.println("second line==يشسيبشسيبشسيبشيس==");
}

When I run this project and enter يشسيبشسيبشسيبشيس in textbox, in IDE console I see as below.

INFO: first line==????????????????==
INFO: second line==????????????????==

Any idea why this is happening?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
  • Its almost certainly character encoding, but can you give more detail please? Where the does the data come from? Where do you see the ??? (in the browser?) – Zutty Nov 19 '12 at 10:57
  • Could you please check this link [http://stackoverflow.com/questions/7060752/how-to-display-text-in-serbian-russian-and-arabicjsf-2-0][1] [1]: http://stackoverflow.com/questions/7060752/how-to-display-text-in-serbian-russian-and-arabicjsf-2-0 – Ragesh P Raju Nov 19 '12 at 11:10
  • JSF2/Facelets uses UTF-8 by default already, so this problem is pretty strange. Where exactly is `fullName` definied? A hardcoded bean property? – BalusC Nov 19 '12 at 11:11
  • System.out.println not always deal properly with utf-8, try: PrintStream stream = new PrintStream(System.out, true, "UTF-8"); stream.print("my name while entering is " + new String(fullName, "UTF-8")); – user902383 Nov 19 '12 at 11:17
  • @user902383: Terrible. That's not a solution. That's a hack/workaround. See my answer for the real solution. – BalusC Nov 19 '12 at 11:20
  • I had a similar problem, it turned out to be how the browser interpreted meta info in the HTML doc. See here http://stackoverflow.com/questions/3096618/special-and-accented-characters – klonq Nov 19 '12 at 11:20
  • @klonq: This is unrelated. `System.out.println()` doesn't print to HTML. Further, see also OP's other question http://stackoverflow.com/questions/13451061/why-arabic-letters-are-not-inserting-in-database – BalusC Nov 19 '12 at 11:21
  • @BalusC : I have updated question. Could you please take a look? I don't understand why `System.out.println("second line==يشسيبشسيبشسيبشيس==");` is giving me output as `second line==????????????????==` – Fahim Parkar Nov 19 '12 at 13:05

2 Answers2

7

This is caused by using the wrong console encoding.

The line

System.out.println("My Data is " + fullName);

prints to the standard output (stdout). You need to configure it to use UTF-8 as well. Assuming that you're using Eclipse, then you need to change the stdout encoding to UTF-8 by Window > Preferences > General > Workspace > Text File Encoding.

enter image description here

If you're using Netbeans, which I can't answer from top of head, head to this answer: hebrew appears as question marks in netbeans which contains a link to this Netbeans Wiki which mentions the following:

To change the language encoding for a project:

  1. Right-click a project node in the Projects windows and choose Properties.
  2. Under Sources, select an encoding value from the Encoding drop-down field.

See also:


Unrelated to the concrete problem, those lines in the filter are unnecessary

    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/html;charset=utf-8");

They defaults in case of JSF2/Facelets to proper values already. Remove those lines.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I don't see how that's relevant. Java is platform independent and you're using Eclipse, right? – BalusC Nov 19 '12 at 11:26
  • Then configure it in Netbeans settings. I've never really used Netbeans, so I can't tell that from top of head. The OS platform (Mac/Windows/etc) is irrelevant for Java. – BalusC Nov 19 '12 at 11:29
  • that means output that is storing in DB is proper. I will check another question answer for real problem. – Fahim Parkar Nov 19 '12 at 11:30
  • I searched a bit and found a related question for Netbeans: http://stackoverflow.com/questions/662146/hebrew-appears-as-question-marks-in-netbeans – BalusC Nov 19 '12 at 11:31
  • its UTF-8, but not sure why facing this problem? I will make site up with new changes and see what I am seeing. – Fahim Parkar Nov 19 '12 at 11:40
  • Either Netbeans is doing mad or your concrete problem is caused by something else. Perhaps you're still printing data straight from DB? Anyway, have you tried Eclipse? Your question history contains hints that you've ever used it. – BalusC Nov 19 '12 at 11:42
  • I am getting "??????" still when I make changes on live site. When I fire query `select summaryTitle from mySac002.patentInfo;` I see it as `ظا?? ت??????ة ??ت?????? صد???? ????ب??ئة `. That means data is not entered correctly and it has gone as `?????????` – Fahim Parkar Nov 19 '12 at 12:21
  • I also checked Netbeans for UTF-8 and its shows UTF-8 is used. – Fahim Parkar Nov 19 '12 at 12:22
  • So you're still taking the DB's role into the question? This is then more related to your other question about the DB encoding. The current question (at least, the way as you formulated it), assumes just a pure `System.out.println()` of a submitted POST request parameter and NO single involvement of the DB here. In such case, other factors such as the stdout encoding play a role. – BalusC Nov 19 '12 at 12:24
  • If I leave DB here, I still get `?????` while printing fullName value. I printed the data on server on live website and in catalina.out (live server and not localhost), I see output of `System.out.println("my name while entering is " + fullName);` as `my name while entering is ??????????? ????? ????? ????? ???` Some times I am getting ????? and when client tried to enter data, it get output as `Ù?ظاÙ? تÙ?Ù?Ù?Ø© Ù?تÙ?Ù?Ù? صدÙ?Ù? Ù?Ù?بÙ?ئة` – Fahim Parkar Nov 19 '12 at 12:29
  • So the server is not controlled by the IDE and thus the server output doesn't appear in IDE console? In that case, the IDE encoding plays NO role. You should be changing the encoding in Tomcat's/JRE's stdout (logging) configuration itself. In the main `logging.properties` file which is actually been used by Tomcat, you should be adding `java.util.logging.ConsoleHandler.encoding=UTF-8` line. – BalusC Nov 19 '12 at 12:33
  • In IDE, I was testing locally. So I was talking about Netbeans. When I make site live with changes, I upload WAR file. As I am putting my WAR file on hosting website, I can see System.out.println() output in catalina.out file. When I open catalina.out file, I see there as ????????. I just tried to insert the text as `شسيبشسيبشسيب` and it gives me output as `?????????????????` – Fahim Parkar Nov 19 '12 at 12:41
  • Yes. Did you understand my previous comment? As far as I understand you, the IDE console encoding is perfectly fine (i.e. Netbeans/Eclipse doesn't print question marks at all) and your current problem is that the production server still writes question marks to log file? Yes? Well, the answer in my previous comment still applies. – BalusC Nov 19 '12 at 12:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/19762/discussion-between-fahim-parkar-and-balusc) – Fahim Parkar Nov 19 '12 at 12:51
  • Well, it's still definitely an encoding misconfiguration in Netbeans. Have you tried Eclipse? – BalusC Nov 19 '12 at 13:06
  • nope, I have to download Eclipse for that. if its Netbeans problem, then WHY I get `????` when I take it to live server? As JSF is printing `?????` in database also, `????` is getting stored – Fahim Parkar Nov 19 '12 at 13:13
  • Sigh. JSF doesn't do that. It's the server or Netbeans who's doing that. The DB problem is a completely different matter which is already answered in your other question about MySQL encoding. Just fix the JDBC connection parameters and ignore the sytem.out.println result and you'll see that it ends up fine in the DB. – BalusC Nov 19 '12 at 13:14
  • I understand it can be Netbeans problem, however when I make site live, there is no Netbeans. There I upload WAR files. Why I also get `??????` at live site also? – Fahim Parkar Nov 19 '12 at 13:15
  • Because, as already explained in a previous comment, the live server isn't been configured to use UTF-8 for stdout/logging. You're seeing a bunch of different but closely related environment-specific problems as single whole problem caused by JSF. You shouldn't do that. – BalusC Nov 19 '12 at 13:16
  • Okay, I will do that... Will get back to you. But one question. On Terminal when I login through SSH using my id n oassword and fire query `select fullName from personalDetails` there also I see `????`. Yes Yes Yes, first I will try with what you said and get back to you. – Fahim Parkar Nov 19 '12 at 13:17
  • If those question marks are stored as such in the DB (which is confirmed by looking **directly** in DB using some DB admin tool), then you should empty the DB table and re-insert the content using proper encoding (which is to be specified by JDBC connection parameters), as answered in your other question about DB encoding. There is no other way to fix the question marks in the DB. It's during that step too late already. And again, this initial question should have nothing to do with the DB. Redisplaying stored question marks from DB has nothing to do with this. – BalusC Nov 19 '12 at 13:18
-2

If the data comes from a DB, please check the field datatype is nvarchar.

Ragesh P Raju
  • 3,879
  • 14
  • 101
  • 136
  • I didn't said data is coming from database. I am printing data of textfield before storing and I see that data itself is wrong. So I am worried why this is happening. – Fahim Parkar Nov 19 '12 at 11:03
  • A wrong encoding in DB would only cause Mojibake, not question marks. – BalusC Nov 19 '12 at 11:08
  • @BalusC : Any reason why this is happening? Also can you take a look at [this question?](http://stackoverflow.com/questions/13451061/why-arabic-letters-are-not-inserting-in-database) – Fahim Parkar Nov 19 '12 at 11:09