6

I am implementing JSP page, where from database I get some list and showing it in JSP using jquery-ui autocomplete combobox:

 <div class="ui-widget">
        <label>Select MKB from list: </label>
        <select id="combobox">
            <option value=""></option>
            <c:forEach var="mkb" items="${mkbList}">
                <option value="${mkb.id}"><c:out value="${mkb.mkbText}"/></option>
            </c:forEach>
        </select>
    </div>

And the problem is that the values in the combo box are all in question marks when loading page in browser (russian characters expected).

In the same jsp file I am including header.jsp, where there is the following line:

<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8"%>

So the encoding should be fine. The encoding of source file is also in UTF-8 In jsp file there is also line:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

I am using tomcat8 and Java 7. One more thing I have tried is adding: -Dfile.encoding=UTF-8 to the startup parameters of tomcat.

The Servlet itself is doing the following:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    MkbDAO mkbDAO = new MkbDAO();
    List<MKB> mkbList = mkbDAO.getMKBList();
    request.setAttribute("mkbList", mkbList);
    RequestDispatcher rd = getServletConfig().getServletContext().getRequestDispatcher("/WEB-INF/jsp/diagList.jsp");
    rd.include(request, response);
}

I was debugging that servlet code, and looked at values in debugger watch window, and values of the list were shown in correct russian characters.

Could you help with resolving this issue?

maximus
  • 4,201
  • 15
  • 64
  • 117
  • Are you using MySQL? Are you implying that this problem doesn't occur when you don't use JSP? I.e. it displays the proper characters when you do `System.out.println(mkb.mkbText)` inside the servlet, or even better, inside a DB unit test class. – BalusC Mar 23 '14 at 13:11
  • @BalusC: I am using Postgresql, It wasn't a problem until I started using data from databse and displaying it in jsp. System.out.println(...) is writing incorrectly as well, in the idea output screen. (printing static russian text or text from database, also russian, doesn't matter) – maximus Mar 23 '14 at 13:20
  • 4
    If `System.out.println()` also shows Mojibake (provided that the target console is properly using UTF-8 to present the output), then it's clearly not a JSP/Servlet problem, but a JDBC problem. – BalusC Mar 23 '14 at 14:53
  • Are you connecting directly to Tomcat or are you using Tomcat behind HTTPD? – Cedric Simon Mar 24 '14 at 02:11
  • @CedricSimon Directly – maximus Mar 24 '14 at 05:30
  • Refer the answers in the given link .This may help if you are using eclipse http://stackoverflow.com/questions/14772275/utf-8-text-hindi-not-getting-displayed-on-browser-window-or-eclipse-console – Sachin Kariyattin Mar 25 '14 at 05:15
  • What is the type `MKB`? Could the problem be inside the MKB class? – developerwjk Mar 25 '14 at 20:02
  • http://stackoverflow.com/questions/17168868/unable-to-print-russian-characters – Divya Mar 28 '14 at 07:48
  • 1
    can you try using a tool like fiddler to see what the content is just before it comes to your browser? This will help pinpoint the issue. i.e. server sending or client/browser parsing. If it is a client/browser issue, it should have a direct explanation. Let me know – aravind Mar 30 '14 at 08:55
  • Shouldn't it be `<%@page contentType="text/html; charset=UTF-8" ...` instead of `<%@page contentType="text/html; UTF-8"`? (missing `charset` parameter name) – Alex Shesterov Mar 30 '14 at 11:28
  • I faced the same problem when I used `./shutdown.sh` and `./startup.sh` to restart Tomcat instead `./catalina.sh stop` and `./catalina.sh start` I dont know the reason of this Tomcat behavior. – Роман А. Jul 12 '18 at 15:11

5 Answers5

1

Maybe it's a problem in your Tomcat configuration: see the Tomcat FAQ for possible solutions.

mmjmanders
  • 1,533
  • 2
  • 13
  • 32
0

I have tried it with "charset=UTF-8" it's giveng me correct string instead of ???????? I have tested with "образец" meaning "sample" russian character string.

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

so you could try with "charset=UTF-8" this will solved your problem.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44
-1

Text like this ??? means you're trying to display cp1251 text as UTF-8, so check your data in db first.
Also -Dfile.encoding=UTF-8 is a bad solution due to unpredictable effects of such usage with something bigger than HelloWorld.java. Try to avoid using it.

Alex Chernyshev
  • 1,719
  • 9
  • 11
  • Sorry, the both statements are pure brabbling. The next time you try to post answers like this, try to come up with real arguments confirming the assumptions. – BalusC Mar 24 '14 at 08:01
  • @BalusC please: 1. Try it yourself here if don't belive: http://2cyr.com/decode/?lang=en Set 'source encoding' to utf-8 and 'displayed as' to cp1251 and see your ???? symbols. 2. It's a VERY bad practice because it overrides system encoding for Jvm, used for console output and as default charset for reading files. This can affect XML parsers for example. That's why things like Maven or WebSphere can be broken and produce miracle exceptions by setting this property. – Alex Chernyshev Mar 29 '14 at 08:03
-1

Try (it is a bit different than yours)

<%@ page contentType="text/html;charset=UTF-8"%>

And in the <form> that holds your <select>

<form accept-charset="UTF-8" ....>
drkunibar
  • 1,327
  • 1
  • 7
  • 7
-1

Put the line

response.setCharacterEncoding("UTF-8");

in the beginning of your doGet method