HoaPhan
HoaPhan

Reputation: 1916

character encoding in JSF 1.1

I'm developing a tool using JSF 1.1 and I'm having this problem : I have a String in my backing bean which is printed as:

./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope

on a txt file.

But when I put it on a h:inputTextArea, it goes like this:

./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope

-

<%@ page contentType="text/html;charset=UTF-8" %>
and this
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

But it didn't work either. can somebody tell me how to fix this. Thanks

/* String[0] as stdout, String[1] as stderr */
String[] results = sshBO.execCommand(cmd, timeout);

/* Done with SSH things */
sshBO.closeSession();

/* Bring the output and err to the presentation */
msg = results[1]+results[0];
FileServices.saveStringToFile("F:/myoutput.txt", msg);
msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */

on the JSP pages :

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://sakaiproject.org/jsf/sakai" prefix="sakai" %>

<f:view >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
...

<h:inputTextarea disabled="true" value="#{SSH.msg}" styleClass="myTextArea" />

Upvotes: 1

Views: 1327

Answers (2)

McDowell
McDowell

Reputation: 108899

In order to determine the source of your transcoding bug, inspect the data after each transcoding operation. Use a tool like this one to determine what the values should be.

For example, Java strings are being transcoded from UTF-16 (the encoding of Java strings) to UTF-8 by your JSP writer. The other transcoding operation looks like reading program output from the native system.

/* String[0] as stdout, String[1] as stderr */
String[] results = sshBO.execCommand(cmd, timeout);

For example, you can print the hex values of your strings using code like this:

for (char ch : msg.toCharArray())
  System.out.format("%04x ", (int) ch);

The code point should print as 2018. Your file writing code may share a similar bug to the code that reads input, misleading you about the source of the problem.


msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */

This makes no difference because it takes UTF-16 data, transcodes it to UTF-8, then transcodes it back to UTF-16. But aside from that, if your string is corrupt, it's already too late. You're fixing the bug in the wrong place.

Upvotes: 0

BalusC
BalusC

Reputation: 1108782

The character exist in Unicode of the bytes 0xE2 0x80 0x98. When you use the CP1252 (Windows default) encoding to encode those bytes, you get ‘.

You need to explicitly set the pageEncoding to UTF-8.

<%@ page pageEncoding="UTF-8" %>

This way it will print the characters using UTF-8 and implicitly set the Content-Type header right.

Upvotes: 1

Related Questions