【问题标题】:Java handling special character in request.getParameterJava 处理 request.getParameter 中的特殊字符
【发布时间】:2012-06-05 10:35:31
【问题描述】:

我试图在我的 java 类中获取包含特殊字符的参数。我已经添加如下

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

我有这两个参数: (1)3 天系统管理员 (6EE SP1) 在 VMware 班加罗尔的培训 (2)Daiichi:eDossier - Phase1 new_6500004691

提交页面后我得到了完美的结果,但是两个参数名称发生了如下变化:

(1)Daiichi:eDossier â¢â¬â Phase1 new_6500004691 (1)在 VMware Bangalore 进行 3 天系统管理员 (6EE SP1) 培训

请推荐

【问题讨论】:

标签: java html jsp jakarta-ee utf-8


【解决方案1】:

尝试在第一次调用getParameter()之前设置request.setCharacterEncoding("UTF-8")

如果这不起作用,请查看at your Tomcat config

【讨论】:

  • request.setCharacterEncoding() 不起作用,因为它 Overrides the name of the character encoding used in the body of this request. 不在参数中。
  • 文档说明:This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect. 参数是正文的一部分 ;-) docs.oracle.com/javaee/5/api/javax/servlet/…
  • 目前尚不清楚 OP 是否在谈论 GET 或 POST 参数。
【解决方案2】:

我知道您的问题是什么:您的应用程序服务器/servlet 容器未配置为支持 unicode 字符。

您必须配置您的应用程序服务器/servlet 容器以支持 UTF 编码。 Tomcat 示例已在此 SO question 中得到解答。

另一种选择是编写一个字符编码过滤器并在您的 Web 应用程序中配置它。

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    相关资源
    最近更新 更多