
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");
成功解决
本文介绍了在JZTK驾照题库项目中,遇到servlet层返回的JSON字符串在浏览器显示时,汉字部分出现问号无法正常显示的问题。解决方法是在Servlet的GET方法中设置服务器端和浏览器端的字符编码为UTF-8,确保数据正确传输。
1958

被折叠的 条评论
为什么被折叠?



