Java JSP中的页面编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3596213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Page Encoding in JSP
提问by Tom Tucker
What's the best way to specify page encoding - using a page directive like this?
指定页面编码的最佳方法是什么 - 使用这样的页面指令?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Or should I directly put it in a meta tag?
或者我应该直接把它放在一个元标记中?
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
Eclipse by default uses both, so does that mean I need both?
Eclipse 默认情况下使用两者,所以这是否意味着我需要两者?
采纳答案by Bozho
You don't need both. Actually, <%@ page pageEncoding="utf-8" %>
suffices.
你不需要两者。其实,<%@ page pageEncoding="utf-8" %>
就够了。
回答by Michael
I would think that you need both, don't you? My guess is that the page directivetells the Containerto compile the JSP using the specified encoding and the meta tagtells the browserhow the page is encoded.
我认为你需要两者,不是吗?我的猜测是page 指令告诉Container使用指定的编码编译 JSP,而元标记告诉浏览器页面是如何编码的。
回答by Truong Ha
In case you need to use UTF-8, you also need to set the charset for your connection url as well as the request and response.
如果您需要使用 UTF-8,您还需要为您的连接 url 以及请求和响应设置字符集。