如何在doPost()调用上设置UTF-8响应? [英] How to Set UTF-8 response on doPost() call?

查看:118
本文介绍了如何在doPost()调用上设置UTF-8响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用UTF-8数据返回到页面的JSON响应,因为这是我从Form发布到我的osgi servlet的内容. Servlet代码如下所示:

I Am trying to have a JSON response back to the page with UTF-8 data, as it is what i have posted from the Form to my osgi servlet. The Servlet code is as shown below:

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表单代码如下所示:

And my JSP Form code shown as below:

<%@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数据:

But when I submit the Form and see the output that has resulted back to the page is like with ANSI format. Where the the JSON data that I have added in the:

jsonobj.put("myjspstati","İş hayatında ne çok engelle karşılaşıldığını,");

在这两个代码段之间可以很好地返回页面.但是,当表单通过以下方式接收到servlet时,提交的表单的相同代码在哪里中断了:

In between code snippet is coming fine back to page. But where as the form submitted same code is breaking when it received into servlet with:

request.getParameter(userinmsg)

如何获取启用了UTF的数据,这些数据可以正确发送到Servlet,并可以使用它完成JSON对象.

How can I get the UTF enabled data that can be properly sent to Servlet and can do the JSON object with same.

在我的浏览器开发人员工具中进行调试 请求标题

响应

推荐答案

您可以看到您的字符集已损坏(,因为使用utf-8的ServerSide json数据正确无误,并且您对请求有疑问.提交表单后传输getParameter()).

you can see your charset is being broken(as the ServerSide json data with utf-8 is resulted correct and you have a doubt in your request.getParameter()) when it is getting transferred after form submission.

尝试添加<input type="hidden" name="_charset_" value="UTF-8"/>,浏览器将在其中填写提交的字符编码作为该字段的值.

Try adding <input type="hidden" name="_charset_" value="UTF-8"/> where browser will fill-in the submitted character encoding as the field's value.

单独修改了表单以及字符集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>

在AEM级别Felix Config上处理它的另一种方法

Apache Sling Request Parameter Handling Default Parameter Encoding中将您的字符集设置为UTF-8,如下图所示.

Set your charset to UTF-8 in Apache Sling Request Parameter Handling Default Parameter Encoding as shown below screenshot.

〜希望它能起作用

这篇关于如何在doPost()调用上设置UTF-8响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆