小编典典

无法从JSP向Servlet发送特殊字符(UTF-8):显示问号

jsp

我在从jsp向servlet发送cyrillic or umlauts等特殊字符时遇到问题。非常感谢您的帮助。

这是我所做的:

  1. 在jsp中定义了utf-8字符集:
        <%@ 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>
     ...
  1. 在server.xml中为连接器设置Tomcat URIEncoding
        <Connector URIEncoding="UTF-8" ...
  1. 在Servlet内部-将CharacterEncoding设置为UTF并读取请求
        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);
  1. 这是使用例如时显示的内容: Однако
        encoding: UTF-8
    received: ??????

我想念什么吗?我想servlet无法正确解码String,但是我不知道为什么会这样。我已经关注了所有有关此主题的建议,但是没有运气。

提前致谢。


阅读 261

收藏
2020-06-08

共1个答案

小编典典

一切看起来都很好。仅System.out.println()控制台也需要配置为将字节流解释为UTF-8。

如果您坐在像Eclipse这样的IDE中,则可以通过将“ _窗口”

“首选项”>“常规”>“工作区”>“文本文件编码”设置_为UTF-8来实现。对于其他环境,您应该对此有更具体的说明,以便我们告诉您如何对其进行配置。

2020-06-08