I try to do a java web app. Everything is good in local tomcat 7 server. I have a jsp file;
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
and in this file i send form to my servlet(post) and in my servlet;
request.setCharacterEncoding("UTF-8");
and it works. But in jelastic tomcat server it doesn't work and these turkish characters 'ş','ğ','ı' are inserting to mySql database '?'.
If i update cells, it shown on file true.
What can i do? i try everything on internet but it doesn't change.
Double check the following settings, making sure everyone knows it's UTF-8 party.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="format-detection" content="telephone=no" />
</head>
<body>
your html content goes here....
</body>
</html>
Database tables are using utf-8 charset, I don't trust db defaults that's why create table definitions have it.
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_swedish_ci;
CREATE TABLE tMyTable (
id int(11) NOT NULL auto_increment,
code VARCHAR(20) NOT NULL,
name VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_swedish_ci;
Let JDBC connection know utf-8 charset.
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
maxActive="10" maxIdle="2" maxWait="10000"
username="myuid" password="mypwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8"
validationQuery="SELECT 1"
/>
Some Tomcat versions don't use the same charset origin for GET or POST form requests, so add useBodyEncodingForURI attribute to force GET form parser oboye setCharacterEncoding value.
<Connector port="8080"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" useBodyEncodingForURI="true"
/>
This call must happen before any filter or other code tries to read parameters from the request. So try to call it early as possible.
if (req.getCharacterEncoding() == null)
req.setCharacterEncoding("UTF-8");
Be careful with the whitespace characters in a .jsp page. I use this technique to set multiple tag headers, see how ending and starting tags are next to each other.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%#
page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1"
import="java.util.*,
java.io.*"
%><%
request.setCharacterEncoding("UTF-8");
String myvalue = "hello all and ÅÄÖ";
String param = request.getParameter("fieldName");
myvalue += " " + param;
%><!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page Title</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="format-detection" content="telephone=no" />
</head>
<body>
your html content goes here.... <%= myvalue %>
</body>
JSP page contentType attribute is the one set in http response object and pageEncoding is the one being used in a disk file. They don't need to match and I usually use ISO-8859-1 if page is only using safe us-ascii characters. Don't use UTF8WithBOM format because hidden leading bom marker bytes may create problems in some J2EE servers.
Last thing is how you write strings to the response stream, if you convert strings to bytes make sure it's using utf-8 and let client know it.
response.setContentType("text/html; charset=UTF-8");
response.getOutputStream().write( myData.getBytes("UTF-8") );
This was a long post but it pretty much covers most corner issues.
The phrase "call it early as possible" in Whome's answer above hit the spot.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
String command = request.getParameter("command");
...
works. However,
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String command = request.getParameter("command");
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
...
doesn't work.
Related
I am getting "The character encoding of the plain text document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the file needs to be declared in the transfer protocol or file needs to use a byte order mark as an encoding signature" on hitting my REST endpoint
my jsp header
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"
</head>
my endpoint
#Path("/create")
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
As mentioned by #lucumt you have a syntax error in your code. the meta tag is not ending correctly in /> - also I corrected the lower case of utf-8 to uppercase:
<%# page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
My form
<form accept-charset="UTF-8">
My Html header
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<meta charset="utf-8">
Tried both and single as well.
My Resteasy filter
request.setCharacterEncoding("UTF-8");
My Method annotation
#Consumes(MediaType.APPLICATION_FORM_URLENCODED + "; charset=UTF-8")
Alter all this settings , my Resteasy service receiving the german characters wrongly for only form post. json request working fine.
Is there any other setting I have to try?
works fine with filter level settings. I didn't restart the server properly while trying these settings.
request.setCharacterEncoding("UTF-8");
I have J2EE web application that is deployed on tomcat on windows and ubuntu.
There is a text "Raphaël", that I am reading from local file(csv) system, that is coming correctly on ubuntu browsers, but it is coming as "Raphaël" on windows browsers.
I am using in Jsp
<meta charset="utf-8">
Also I have tried following meta tags also, but they didn't work.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
What could be the problem here?
CSV reading code is
reader = new CSVReader(new FileReader(file));
final List<String[]> licenses = reader.readAll();
Check that you have UTF-8 encoding in both CSV file and JVM.
For JVM setting use :
-Dfile.encoding=UTF8
To check file encoding on linux use:
file --mime-encoding file.name
For setting encoding on JSP page use:
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
If that does not help add a filter in order to have proper encoding for all responses:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException
{
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
Set your JSP contentType as UTF-8
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
First you need to read the file using correct encoding. The way you read the file depends on the default encoding of J2EE server's JVM, which can be anything. My suggestion is to do something like this:
final FileInputStream instream = new FileInputStream(file);
CSVReader reader = new CSVReader(new InputStreamReader(input, "UTF-8"));
and also set UTF-8 encoding in the JSP as Joseph has written above:
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
I'm having troubles with uploading and parsing a file as UTF-8 strings. I use the following code:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part filePart = request.getPart("file");
InputStream filecontent = filePart.getInputStream();
// ...
}
And my webpage looks like this:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
I found a great post about UTF-8 encoding in java webapps, but unfortunately it didn't work for me. I still have random symbols in strings in NetBeans debugger, and when I display them on a webpage, although most of them get displayed correctly, some cyrillic letters (я, с, Н, А) get replaced by '�?'
The file upload with a HTML form doesn't use any character encoding. The file is transferred byte by byte as is. See here under "multipart/form-data".
So if the original file at client side is a text file with UTF-8 character encoding, then on the server side it is also UTF-8.
Then you can use an InputStreamReader to decode the bytes as UTF-8 text:
InputStreamReader reader = new InputStreamReader(filecontent, "UTF-8");
That's it.
javax.servlet.http.Part, what you use in the very first line of your code, has a method on it getContentType() which will tell you what the content type of the uploaded form data is. Nothing you have written to date would constrain the uploaded form data to any particular character set; ergo you need to determine the character set and deal with it accordingly.
I have two jsp pages. I am trying to add "Russian" language. Russian characters are shown perfectly on jsp page, but when I try to send this value to another jsp page from parameter then in second jsp page this value is changed to different characters. This problem is only in Russian Language and not in others such as Italy and French.
For example
On demo.jsp page the russian character "приветствие" is shown correctly.
but when I try to send it to another page "test.jsp" then some unknown
characters are shown like "!C<Cä5 Cô>CôCC´OD=Cä5!"
Code:
demo.jsp
String welcometext=langP.get("welcome");
<jsp:include page="<%=test.jsp%>">
<jsp:param name="wlc" value="<%=Encode.hex8Code(welcometext)%>" />
</jsp:include>
In test.jsp
String title = Encode.utfToUnicode(Decode.hex8Decode(request.getParameter("wlc")));
System.out.println(" Russial welcome test "+welcome);
Is there any special code we need to add for Russia while sending them in query parameters??
Please note* the following code are already written else it would have given problem for French and Italy language too..
<%# page contentType="text/html; charset=UTF-8" %>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Also tried with following but didn't help out!
request.setCharacterEncoding("UTF-8")
Try to add <% request.setCharacterEncoding("UTF-8"); %> to your main jsp page:
Here is my example:
demo.jsp
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Привет</h1>
<jsp:include page="/test.jsp" flush="true">
<jsp:param name="wlc" value="Привет"/>
</jsp:include>
</body>
</html>
test.jsp
<h1>Param values is</h1>
<%
String hello = request.getParameter("wlc");
out.print(hello);
%>
I don't know the better solution but the following code solved this issue. I kept the variable in session attribute.
demo.jsp
session.setAttribute("welcometext", welcometext);
test.jsp
String welcometest=(String) session.getAttribute("welcometext");