如何在 doPost() 调用上设置 UTF-8 响应?
How to Set UTF-8 response on doPost() call?
我正在尝试使用 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","İş hayatında ne çok engelle karşılaşıldığını,");
中间的代码片段正在正常返回页面。但是当表单提交相同的代码时,当它接收到 servlet 时中断了:
request.getParameter(userinmsg)
我怎样才能获得可以正确发送到 Servlet 并且可以用相同的方式执行 JSON object 的启用 UTF 的数据。
在我的浏览器开发者工具中调试
请求 Headers
回应
您可以看到您的字符集被破坏了(因为使用 utf-8 的服务器端 json 数据结果是正确的,您对 request.getParameter()) 在表单提交后传输时。
尝试添加 <input type="hidden" name="_charset_" value="UTF-8"/>
,浏览器将 fill-in 提交的字符编码作为字段的值。
单独修改了您的表单以及字符集 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
Default Parameter Encoding
中将您的字符集设置为 UTF-8,如下图所示。
~希望有用
我正在尝试使用 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","İş hayatında ne çok engelle karşılaşıldığını,");
中间的代码片段正在正常返回页面。但是当表单提交相同的代码时,当它接收到 servlet 时中断了:
request.getParameter(userinmsg)
我怎样才能获得可以正确发送到 Servlet 并且可以用相同的方式执行 JSON object 的启用 UTF 的数据。
在我的浏览器开发者工具中调试
请求 Headers
回应
您可以看到您的字符集被破坏了(因为使用 utf-8 的服务器端 json 数据结果是正确的,您对 request.getParameter()) 在表单提交后传输时。
尝试添加 <input type="hidden" name="_charset_" value="UTF-8"/>
,浏览器将 fill-in 提交的字符编码作为字段的值。
单独修改了您的表单以及字符集 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
Default Parameter Encoding
中将您的字符集设置为 UTF-8,如下图所示。
~希望有用