我正在尝试使用 UTF-8 数据将 JSON 响应返回到页面,因为这是我从表单发布到我的 osgi servlet 的内容。 Servlet代码如下所示:
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 数据:
jsonobj.put("myjspstati","This is hayatında ne çok engelle karşılaşıldığını,");
在代码片段之间,可以很好地返回页面。但是,当表单提交时,相同的代码在接收到 servlet 时会被破坏:
request.getParameter(userinmsg)
如何获取启用 UTF 的数据,这些数据可以正确发送到 Servlet,并可以使用相同的 JSON 对象。
最佳答案
您可以看到您的字符集被破坏(因为使用 utf-8 的服务器端 json 数据结果正确,并且您对 request.getParameter() 有疑问)表单提交。
尝试添加<input type="hidden" name="_charset_" value="UTF-8"/>
其中浏览器将填写提交的字符编码作为字段的值。
单独修改了您的表单以及字符集 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>
Another way of handling it at AEM level Felix Config
将 Apache Sling Request Parameter Handling
中的字符集设置为 UTF-8 Default Parameter Encoding
如下截图所示。
~希望它有效
关于java - 如何在 doPost() 调用上设置 UTF-8 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42015668/