springboot19-设置字符编码方式二---更简单

1、编写的servlet

@WebServlet(urlPatterns = "/myservlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("你好世界");
        resp.setContentType("test/html;character=utf-8");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

2、编写配置文件

设置请求响应字符编码

使用springboot时支持编码

servlet.encoding.enabled=true

强制使用springboot所支持的参数

server.servlet.encoding.force=true

编码格式设置为utf-8

server.servlet.encoding.charset=utf-8



**3、启动类加载**

@SpringBootApplication
@ServletComponentScan(basePackages = "com.study.springboot.servlet")
public class Springboot16Character2Application {

public static void main(String[] args) {
    SpringApplication.run(Springboot16Character2Application.class, args);
}

}

posted @ 2021-09-15 16:04  不是孩子了  阅读(160)  评论(0)    收藏  举报