JZTK项目 驾照题库项目servlet层得到的json字符串在浏览器中 汉字部分出现问号?无法正常显示的解决方法

原文链接: https://www.mk2048.com/blog/blog.php?id=h0jcc1iaabaa&title=JZTK%E9%A1%B9%E7%9B%AE+%E9%A9%BE%E7%85%A7%E9%A2%98%E5%BA%93%E9%A1%B9%E7%9B%AEservlet%E5%B1%82%E5%BE%97%E5%88%B0%E7%9A%84json%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%9C%A8%E6%B5%8F%E8%A7%88%E5%99%A8%E4%B8%AD

servlet层中的代码如下:

package com.swift.jztk.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swift.jztk.biz.IQuestionBiz;
import com.swift.jztk.biz.QuestionBizImpl;

@WebServlet("/getQuestions")
public class GetQuestions extends HttpServlet {
    private static final long serialVersionUID = 1L;
    IQuestionBiz biz = new QuestionBizImpl();

    public GetQuestions() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());
        String testType = request.getParameter("testType");
        String json = biz.getQuestions(testType);
        response.getWriter().println(json);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

在get方法中增加设置服务器端和浏览器端的代码解析方式为utf-8

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
成功解决



更多专业前端知识,请上 【猿2048】www.mk2048.com

猜你喜欢

转载自blog.csdn.net/weixin_39037804/article/details/102752882
今日推荐