0

I am writing a web app where for creating news. I want to use letters which do not belong to English alphabet, such as šđčćž. These letters can be UTF8 encoded but it does not work in my app.

I have two text fields in which I enter data. One of them is for news headline and the other one is for news content. Both of these fields are required. After saving the news and checking database I get some weird characters instead of the entered ones.

I have already tried adding in the JSF page and it did not help

<?xml version="1.0" encoding="UTF-8" ?> 

I have also tried the following in my news managed Bean when setting the news headline and content:

public void setHeadline(String headline){
    this.headline = new String(headline.getBytes("iso-8859-1"),"utf-8")
}

public void setContent(String content{
    this.content =  new String(content.getBytes("iso-8859-1"),"utf-8")
}

This helped when I enter both the headline and content and save them. But, if a user would forget to enter data into any one of these fields, the data does not get stored to the database (which is okay since the fields are required), but the field in which the data did get entered transforms the letters šđčćž into weird characters.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
johnny94
  • 311
  • 4
  • 20
  • 1
    If your jsf page is `utf-8` why do you interpret the strings as `iso-8859-1`? Assuming that `setHeadline`& `setContent` is called from within the `jsf` page. – second Aug 17 '19 at 18:58
  • it is only way to print correct string. with standard setters it will print symbols like ÄÄÄ... – johnny94 Aug 17 '19 at 19:04
  • 1
    You don't mention any search results and why the resultsid not help (see [ask]), So I assume you did not do any searching. Please do since there is a lot about this on stackoverflow. http://idownvotedbecau.se/noresearch – Kukeltje Aug 17 '19 at 19:16
  • Your JSF file is just an XML file and XML abstracts away all questions of encoding so that the users of the file don't need to care. That means that the xml encoding prolog and the actual encoding of your JSF file are hidden from the rest of your program, without effect. HTML and HTTP are the ones that contain information about encoding. I know nothing about JSF, but if it doesn't do everything in UTF-8 by default, then it should be possible to configure it to. – kumesana Aug 17 '19 at 19:18
  • @kumesana: yes, configure it via a servletfilter to make the http req/resp utf-8 or configure a default on the container. SO has multiple Q/A on this when searching for 'jsf and utf-8'. Finding these is for now an exercise for OP. – Kukeltje Aug 18 '19 at 10:58
  • @Kukeltje that is a horrible way to handle encoding questions, and if JSF actually doesn't provide any better then it's severely lacking in professionalism. I did try to look up solutions and found the answers you referenced, but decided to give JSF the benefit of the doubt and consider that these authors of these answers didn't know how you're actually supposed to do it. – kumesana Aug 19 '19 at 07:57
  • @kumesana: Why are servlet containers by default still doing ISO8859-1? When all java has been UTF-8 by default for a long time (beginning even?). You should not need to do anything (not even IN JSF) and have the container do it (and not even have to (re)configure the container. https://www.baeldung.com/tomcat-utf-8 And JSF does set it to UTF-8 when something else has not explicitly set it. And be very carefull with your last statement (if you do not know the ins and outs) – Kukeltje Aug 19 '19 at 08:35

0 Answers0