问题描述

昨天在参考别人的项目时,发现页面引用jscss等文件总是乱码,后来才发现是MIME类型统一设置为text/html,并且仅仅编码设置了浏览器端的解析编码。另外,可以先通过文本编辑器(如notepad++等)先统一下编码。

简单总结到的区别

response.setContentType:设置资源MIME类型,还可设置资源在浏览器端的解码方式。
response.setCharacterEncoding:设置Servlet响应结果的编码。

JSP中设置编码参数简单分析:https://blog.csdn.net/weixin_43670802/article/details/104502395

简单示例

更多示例:https://www.programcreek.com/java-api-examples/?class=javax.servlet.http.HttpServletResponse&method=setCharacterEncoding

@Override
public void execute(FlowContext context) throws ServletException, IOException, InventoryException, ExternalServiceException {

    HttpServletResponse res = context.getHttpServletResponse();
    res.setCharacterEncoding("UTF-8");
    res.setContentType("text/xml; charset=UTF-8");

    XStream stream = new XStream(new DomDriver("UTF-8"));
    stream.toXML(xmlObject, res.getWriter());
}

参考
https://www.jb51.net/article/73907.htm
https://blog.csdn.net/jeanwest01/article/details/80476024
https://ask.csdn.net/questions/229399?ref=myrecommend
JSP response.setCharacterEncoding与response.setContentType的区别

相关文章:

  • 2022-02-07
  • 2022-02-25
  • 2021-07-11
  • 2021-09-14
  • 2021-08-07
猜你喜欢
  • 2021-10-19
  • 2021-10-19
  • 2021-10-19
  • 2022-12-23
  • 2021-11-29
  • 2021-11-29
  • 2021-12-09
相关资源
相似解决方案