欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp请求servlet地址时返回的中文为乱码问题如何解决?

程序员文章站 2022-06-15 15:53:24
当jsp请求servlet地址时放回的中文为乱码,首先我数据库中的编码格式为utf-8,包括jsp,myeclipse,servlet,都设置了这个编码,菜鸟小编最终百度到解决办法...

当jsp请求servlet地址时放回的中文为乱码,首先我数据库中的编码格式为utf-8,包括jsp,myeclipse,servlet,都设置了这个编码,菜鸟小编最终百度到解决办法,直接上代码。

jsp
package application.admin.servlet.cate;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import org.json.JSONArray;

import application.admin.cate.Cate;
import common.api.CommonApi;


@WebServlet("/CateListServlet")
public class CateListServlet extends HttpServlet {
   private String json_data;

   public void init() throws ServletException
   {

   }

   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
             throws ServletException, IOException
   {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        JSONArray book_cate_list;
        String user_id = request.getParameter("user_id");
        String state = request.getParameter("state");
        Cate cate = new Cate();
        try {
            book_cate_list = cate.book_cate_list();//获取所有图书分类
            int count_cate = cate.count_cate();//统计图书分类个数
            json_data = CommonApi.return_json(book_cate_list,count_cate);//拼装返回数据
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getOutputStream().write(json_data.getBytes("utf-8")); 
   }

   public void destroy()
   {
       // 什么也不做
   }
}
servlet
package application.admin.servlet.cate;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import org.json.JSONArray;

import application.admin.cate.Cate;
import common.api.CommonApi;


@WebServlet("/CateListServlet")
public class CateListServlet extends HttpServlet {
   private String json_data;

   public void init() throws ServletException
   {

   }

   public void doGet(HttpServletRequest request,
                     HttpServletResponse response)
             throws ServletException, IOException
   {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setHeader("Content-Type", "text/html; charset=UTF-8");
        JSONArray book_cate_list;
        String user_id = request.getParameter("user_id");
        String state = request.getParameter("state");
        Cate cate = new Cate();
        try {
            book_cate_list = cate.book_cate_list();//获取所有图书分类
            int count_cate = cate.count_cate();//统计图书分类个数
            json_data = CommonApi.return_json(book_cate_list,count_cate);//拼装返回数据
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getOutputStream().write(json_data.getBytes("utf-8")); 
   }

   public void destroy()
   {
       // 什么也不做
   }
}