I have a small webapp which handles a lot of Spanish text.
At one point in the code, a JSP page responds with a Json String containing some of this text. If I print the String to the Console, it looks like jibberish. But if I examine the header/content of the response in Chrome Developer Tools, it looks correct. It is transferred in the correct encoding. This part of the webapp functions as expected.
At another point in the code, a different JSP page responds with HTML. Some of this HTML contains more of the Spanish text. This time, the text is transferred (and displayed) as jibberish.
What are potential reasons that this could be happening? Both times, I'm just printing the text using out.print. Why does it work at one point, but not in other?
Examples:
// In a file who's only output is the json string
String jsonString = ...
System.err.println(jsonString); // prints jibberish
out.println(jsonString); // looks correct when the response is viewed in Chrome Developer tools, and looks correct in a browser
...
// In a file who's output is a complete html page
String spanishText = ...
out.println("<label>" + spanishText + "</label>"); // looks like jibberish when the response is viewed in Chrome developer tools, and shows up as jibberish in a browser
You need to set the encoding which the JSP/Servlet response should use to print the characters and instruct the webbrowser to use the same encoding.
This can be done by putting this in top of your JSP:
<%# page pageEncoding="UTF-8" %>
Or if you're actually doing this in a Servlet:
response.setCharacterEncoding("UTF-8");
The "jibberish" when using System.err is a different problem. You need to set the encoding of the console/logfile which is been used to print this information to. If it's for example Eclipse, then you can set it by Window > Preferences > General > Workspace > Text File Encoding.
See also:
Unicode - How to get the characters right? - Fixing JSP/Servlet response
Unicode - How to get the characters right? - Fixing development environment
Related
In my grails application I need to set html tags to the session. For example
session.setAttribute("message","<font color=\"green\">Successfully Processed</font>")
but when I am fetching the value from the GSP (like ${session.message}) the opening and closing tags get changed to <
So the whole text gets printed as such. Why?
This is due to encoding. The documentation explains the details and why. In your case you want to use raw like this:
${raw(session.message)}
I have a web page where I do a search based on the text given in a text box. This text can be in any language like japanese, chinese etc (or any mbcs character).
Now when i enter a text in japanese (or any other mbcs character), the result populates the screen (form) with some wierd characters.
For Example: testテスト will turn into testãã¹ã.
When i see the post parameters in Firebug (debugging tool) i can see that the search string goes as testテスト however when i put debug statements in my code, i can see that request.getParameter("searchString") is not able to identify the japanese characters and turn them into some wierd chars.
My JSP header already has <%# page contentType="text/html; charset=UTF-8"
I have also tried putting pageEncoding="UTF-8" in this but it didn't help.
I tried setting character encoding like request.setCharacterEncoding("UTF-8") also just before doing request.getParameter but that too didn't work for me.
After going through a few forums and blogs i also tried setting useBodyEncodingForURI=true in the <Connector> of tomcat config but that also did not help me.
Can anybody suggest me something to resolve this issue?
set the following encoding in every servlet/ action
response.setContentType("UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
also set following in first servlet/action
for japanese
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
session.setAttribute(Globals.LOCALE_KEY, new Locale("jp", "ja_JP"));
I'm having trouble dealing with Charsets while parsing and rendering a page using the JSoup library. here is an example of the page it renders:
http://dl.dropbox.com/u/13093/charset-problem.html
As you can see, where there should be ' characters, ? is being rendered instead (even when you view the source).
This page is being generated by downloading a web page, parsing with JSoup, and then re-rendering it again having made some structural changes.
I'm downloading the page as follows:
final Document inputDoc = Jsoup.connect(sourceURL.toString()).get();
When I create the output document I do so as follows:
outputDoc.outputSettings().charset(Charset.forName("UTF-8"));
outputDoc.head().appendElement("meta").attr("charset", "UTF-8");
outputDoc.head().appendElement("meta").attr("http-equiv", "Content-Type")
.attr("content", "text/html; charset=UTF-8");
Can anyone offer suggestions as to what I'm doing wrong?
edit: Note that the source page is http://blog.locut.us/ and as you'll see, it appears to render correctly
The question marks are typical whenever you write characters to the outputstream of the response which are not covered by the response's character encoding. You seem to be relying on the platform default character encoding when serving the response. The response Content-Type header of your site also confirms this by a missing charset attribute.
Assuming that you're using a servlet to serve the modified HTML, then you should be using HttpServletResponse#setCharacterEncoding() to set the character encoding before writing the modified HTML out.
response.setCharacterEncoding("UTF-8");
response.getWriter().write(html);
The problem is most likely in reading the input page, you need to have the correct encoding for the source too.
I'm developing an Java Web Application, I used some jQuery and a REST web services that output an JSON object with a list of Javascript objects using AJAX. All is ok but when I try to fill a table created with Javascript using jQuery.html() to a valid div, all hell broke loose in Chrome, including this
Error: INVALID_STATE_ERR: DOM Exception 11
in the Javascript console.
The problem is like this, try this in chrome Javascript console:
$('#ValidadorWrapper').html("<div>AMOXICILIN 100 C&psulapsula</div>");
But if we delete the ampersand, it works
$('#ValidadorWrapper').html("<div>AMOXICILIN 100 Camp;psulapsula</div>");
This problem happens only in Chrome, I suspect it has something to do with the encoding characters but I cant' find any way of doing it. Obviously I need to input an & ( i mean the & entity) in this document.
Some steps I have tried and didn't work:
I'm using the gson library to output a String to an JSP page. My JSP page have this header <%#page pageEncoding= "UTF-8" contentType="text/html; charset=UTF-8" %> . This was my first attempt and didn't work when an ampersand appeared in my JSON object (well any special char).
My second attempt was using the HTMLEntities Java library to encode all special chars. This is the actual version and it still doesn't work
Using unicode chars like \u0026 doesnt work either
There is something more strange. Apparently if I use $('#ValidadorWrapper').html("AMOXICILIN 100 \u0026"); it works!, but this is just an example. I'm trying to fill an HTML table with my object so I really need to put that data inside html (table) tags
Try this:
$('<div></div>').appendTo('#ValidadorWrapper').text('AMOXICILIN 100 Cápsulapsula');
I have homework which I have to use scriptlets in ,
I need to make new line in my jsp page usint out object
I tried to use
<%
out.println();
out.newLine();
%>
but both doesn't work !!! I treid to use
out.flush()
but it doesn't work!!
Perhaps out.println("<br>"); is what you're after. (Remember that the browser in which you're viewing the jsp-page in, interprets the output of your script as HTML, which basically ignores newline characters.)
You can look at the source of the page to see what the jsp-page actually generates.
If you really want to see the verbatim output of the jsp-script, you could do
out.println("<html><body><pre>");
// ...
out.println("</pre></body></html>");
#Alaa - out.newLine() does work. It just doesn't do what you are expecting it to do ... assuming that your JSP is generating an HTML page.
When you use out.newLine(), it adds a newline character to the content stream that you are generating. If you use view source on the page in your web browser you can see the newline character.
But a newline character in an HTML document typically does not result in a line break in the displayed page as rendered by a browser. To get the browser to render line break in the displayed page, you typically* need to output a <br /> element.
* - Actually, there are other ways to get the visual equivalent of a line break involving CSS, etcetera. And within a <pre>...</pre> a raw newline character does get rendered as a line break.
Remember the JSP code is outputting HTML. The HTML will then be rendered by the browser. A single blank line in HTML may not be shown as a blank line on the screen when the HTML is rendered.
You need to either examine the HTML source in the browser and look for the blank line. Or else try output more significant HTML to verify the JSP scriptlets are working like:
<%
out.println("<p>hello</p>");
%>