Can not send special characters (UTF-8) from JSP to Servlet: question marks displayed

痴心易碎 提交于 2020-01-09 10:51:42

问题


I have a problem sending special characters like cyrillic or umlauts from a jsp to a servlet. I would greatly appreciate your help here.

Here is what I have done:

  1. Defined the utf-8 charset in the jsp:

    <%@ page language="java" contentType="text/html; charset=utf-8" 
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     ...
    
    <div class="authentication">
      <form name="registerForm" action="register" method="get" accept-charset="UTF-8">
        <input type="input" name="newUser" size="17"/>
        <input type="submit" value="senden" />
      </form>
    </div>
     ...
    
  2. Set Tomcat URIEncoding for Connector, in the server.xml

    <Connector URIEncoding="UTF-8" ...
    
  3. Inside the servlet - set the CharacterEncoding to UTF and read request

    public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
    
    request.setCharacterEncoding("UTF-8");
    String username = request.getParameter("newUser");
    System.out.println("encoding: "+request.getCharacterEncoding());
    
    System.out.println("received: "+username);
    
  4. Here is what gets displayed when using for example: Однако

    encoding: UTF-8
    received: ??????
    

Am I missing something ? I suppose the servlet can not decode the String properly, but I have no idea why is this happening. I've followed all the advises on this topic but had no luck.

thanks in advance.


回答1:


Everything looks fine. Only the System.out.println() console also needs to be configured to interpret the byte stream as UTF-8.

If you're sitting in an IDE like Eclipse, then you can do that by setting Window > Preferences > General > Workspace > Text File Encoding to UTF-8. For other environments, you should be more specific about that so that we can tell how to configure it.



来源:https://stackoverflow.com/questions/7539590/can-not-send-special-characters-utf-8-from-jsp-to-servlet-question-marks-disp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!