Java 如何在doPost()调用上设置UTF-8响应?
我正试图用UTF-8数据将JSON响应返回到页面,因为这是我从表单发布到OSGiServlet的内容。Servlet代码如下所示:Java 如何在doPost()调用上设置UTF-8响应?,java,jsp,osgi,aem,sling,Java,Jsp,Osgi,Aem,Sling,我正试图用UTF-8数据将JSON响应返回到页面,因为这是我从表单发布到OSGiServlet的内容。Servlet代码如下所示: import org.json.simple.JSONArray; import org.json.simple.JSONObject; @SlingServlet(paths="/bin/JsonOsgiCall", methods = "POST", metatype=true) public class JsonOsgiCall extend
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@SlingServlet(paths="/bin/JsonOsgiCall", methods = "POST", metatype=true)
public class JsonOsgiCall extends org.apache.sling.api.servlets.SlingAllMethodsServlet {
private static final long serialVersionUID = 2598426539166788515L;
protected final Logger log = LoggerFactory.getLogger(this.getClass());
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
JSONObject jsonobj = new JSONObject();
try {
jsonobj.put("testint", 30);
jsonobj.put("myjspstring", request.getParameter("userinmsg"));
jsonobj.put("myjspstati","`İş hayatında ne çok engelle karşılaşıldığını,`");
JSONArray list = new JSONArray();
list.add("message 1");
list.add("message 2");
jsonobj.put("messages", list);
log.info("*** JSON Object with ***" + jsonobj.toJSONString());
//out.println(jsonobj.toJSONString());
out.println(jsonobj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
我的JSP表单代码如下所示:
<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="org.json.simple.JSONObject,java.io.PrintWriter,java.util.*"%>
<%
%>CALL OSGI SERVICE FOR JSON RESPONSE
<cq:includeClientLib js="granite.csrf.standalone"/>
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Editer les sous-titres</title>
</head>
<body>
<form id="submitForm" method="POST" action="/bin/JsonOsgiCall">
<textarea name="userinmsg" autocomplete="off" type="text" id="userinmsg" rows="4" cols="30" style="width: 450px; margin-left: 25px;">
</textarea>
<br/>
<p style="margin-left: 420px;"><input name="submitmsg" type="submit" id="submitmsg" value="Call JSON Service" /></p>
</form>
</body>
但是当我提交表单并看到返回到页面的输出时,与ANSI格式类似。其中,输入了我在中添加的JSON数据:
杰索诺比·普特米杰斯帕斯塔蒂,哈亚特·恩格勒·卡雷拉·阿尔德·恩格勒
中间的代码片段很好地返回页面。但是,当表单提交时,当它接收到servlet时,相同的代码被破坏:
request.getParameteruserinmsg
我如何获得能够正确发送到Servlet并使用相同的方法处理JSON对象的启用UTF的数据
在我的浏览器开发工具中调试
请求头
回应
您可以看到您的字符集正在被断开,因为带有utf-8的服务器端json数据的结果是正确的,并且您在表单提交后传输request.getParameter时对其有疑问 尝试添加浏览器将填写提交的字符编码作为字段值的位置 单独修改表单以及字符集utf-8的隐藏输入 AEM级别Felix配置的另一种处理方式 在ApacheSling请求参数处理默认参数编码中,将字符集设置为UTF-8,如下屏幕截图所示。
~Hope it work您的代码没有问题。您是否尝试过任何服务器配置?AEM是从jetty/jre运行还是通过apache托管?你是否也检查过你的过滤链,看看是否有什么东西改变了编码?您也可以尝试,但怀疑这会有所帮助。我已将Felix console Apache Sling请求参数处理默认参数编码添加到_charset_uz=UTF-8中,其他内容没有任何更改。AEM运行在独立的本地单独服务器上,而不是通过apache托管。如果您检查浏览器开发工具中的标题,您看到相应的req/response的正确编码了吗?@i.net用开发人员工具的屏幕截图更新了我的问题。您能试一下您的代码吗?我想提出这个建议,但OP显然设置了相关的sling属性,该属性取代了此要求,因此我认为这是一个不同的问题。我现在想到的是sling过滤器。@I.net您的回答是正确的,我遗漏了@Krish对Apache sling请求参数处理默认参数编码为_charset_uz=UTF-8的评论,这是强制AEM级别的所有表单使用UTF-8的另一种方式。使用此方法更新了我的答案。@VAr它按照您的建议工作,修改了默认参数,将其编码为UTF-8,然后开始工作。但观察到以下行为。1.第一次将该值更改为_charset_u=UTF-8是不起作用的。2.将值更改为UTF-8开始工作,并且能够看到正确的输出。3.并且将其更改回_charset_Uf=UTF-8也仍然有效,并产生了正确的输出…**奇怪但不确定它的行为**。。
<form id="submitForm" method="POST" action="/bin/JsonOsgiCall">
<textarea name="userinmsg" autocomplete="off" type="text" id="userinmsg" rows="4" cols="30" style="width: 450px; margin-left: 25px;">
</textarea>
<br/>
<input type="hidden" name="_charset_" value="UTF-8"/>
<p style="margin-left: 420px;"><input name="submitmsg" type="submit" id="submitmsg" value="Call JSON Service" /></p>
</form>