Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something wrong with encoding of .properties or JSP

I have jsp file:

   <%@ page language="java" contentType="text/html;
charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<c:set var="language"
    value="${not empty param.language ? param.language : not empty language ? language : pageContext.request.locale}"
    scope="session" />
<fmt:setLocale value="${language}" />
<fmt:setBundle basename="localization.text" />
<!DOCTYPE html>
<html lang="${language}">
<head>
<title>JSP/JSTL i18n demo</title>
</head>
<body>
<form accept-charset="UTF-8"><select id="language"
    name="language" onchange="submit()">
    <option value="en" ${language=='en' ? 'selected' : ''}>English</option>
    <option value="ru" ${language=='ru' ? 'selected' : ''}>Russian</option>
</select></form>
<form name="loginForm" method="POST" action="controller"><input
    type="hidden" name="command" value="login" /> <label for="login"><fmt:message
    key="login.label.login" />:</label> <input type="text" name="login" value="">
<br>
<input type="hidden" name="command" value="password" /> <label
    for="password"><fmt:message key="login.label.password" />:</label> <input
    type="password" name="password" value=""> <br>
<fmt:message key="login.button.submit" var="buttonValue" /> <input
    type="submit" name="submit" value="${buttonValue}"></form>
</body>
</html>

As you could get from written above the problem is in encoding and the problem is in Russian language. So here is my .properties file (text_ru.properties in localization folder):

login.label.login = Логин
login.label.password = Пароль
login.button.submit = Отправить

Btw,it's English file:

login.label.login = Login
login.label.password = Password
login.button.submit = Sign in

But browser gives me this thing:

jsp pic

I saved my .properties files in utf-8 and tried it with the help of two programms (the first is Eclipse and the second is Notepad++) and I don't really know what to do with this encoding problem.

Will be very grateful for your help.

like image 693
And Avatar asked Dec 07 '11 21:12

And


People also ask

How do I change the encoding of a properties file in eclipse?

properties files are Latin1 (ISO-8859-1) encoded by definition. ISO-8859-1 as its default encoding. You can change this under: Preferences > General > Content Types.

What does <% mean in JSP?

<%@ is the directive attribute. You may include a page or may declare a tag-library using <%@

What is Pageencoding in JSP?

For JSP pages, the page encoding is the character encoding in which the file is encoded. For JSP pages in standard syntax, the page encoding is determined from the following sources: The page encoding value of a JSP property group (see Setting Properties for Groups of JSP Pages) whose URL pattern matches the page.


1 Answers

Unfortunately, when .properties files are read via a ResourceBundle, it is expecting ISO-8859-1 always.

The usual approach is to unicode-escape the non-ascii characters in the properties file. Then it will look something like that:

hours.label=\u0427\u0430\u0441\u043e\u0432\u0435

AnyEdit tools is an eclipse plugin that makes the escaping and unescaping during development easy.

Another, more tedious approach is to provide your own tag that uses your own ResourceBundle implementation that in turn uses .load(reader), where the reader is using UTF-8

like image 80
Bozho Avatar answered Oct 06 '22 00:10

Bozho