[英]How to include page encoding to all the jsp page
I have included <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
in one jsp page and it displayed french characters properly. 我在一个jsp页面中包含了
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
,它正确地显示了法语字符。 But i want to include the page encoding to all the jsp pages. 但我想将页面编码包含在所有jsp页面中。 In web.xml i included
在web.xml我包括
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
but it will work for tomcat7 i am using tomcat6. 但它适用于tomcat7我正在使用tomcat6。
Is there any other solution for this? 还有其他解决方案吗?
You could define a new Filter in your web.xml and modify your answers there. 您可以在web.xml中定义新的Filter并在那里修改答案。
For example, you could put this in your web.xml : 例如,您可以将它放在web.xml中:
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.example.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*.jsp</url-pattern>
<!-- or any pattern you like to excluse non-jsp resources -->
</filter-mapping>
And create a com.example.EncodingFilter
class like this : 并创建一个
com.example.EncodingFilter
类,如下所示:
public class EncodingFilter implements javax.servlet.Filter {
// init, destroy, etc. implementation
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response); // pass request to next filter
}
}
You could furthermore add tests around your setCharacterEncoding()
call to filter only specific URI or exclude for example a non-HTML jsp or specific parameter, like a JSP outputing PDF if URL paramter format=PDF
is provided : 您还可以围绕
setCharacterEncoding()
调用添加测试,以仅过滤特定的URI或排除非HTML jsp或特定参数,例如,如果提供了URL参数format=PDF
则输出PDF的JSP:
if (!request.getQueryString().contains("format=PDF") {
response.setCharacterEncoding("utf-8");
}
chain.doFilter(request, response); // pass request to next filter
如果在使用Tomcat时您的参数不是UTF-8编码,请尝试在Tomcat的server.xml中调整Connector配置,如下所示:
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />
You can use a meta tag instead of adding page encoding in all the jsp file or in web.xml . 您可以使用元标记,而不是在所有jsp文件或web.xml中添加页面编码。 If you have a common .jsp file include
如果你有一个共同的.jsp文件包含
Another way is to apply encoding to all pages is using filter. 另一种方法是对所有页面应用编码使用过滤器。
public void doFilter(ServletRequest request,ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
// response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding(encoding);
filterChain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
String encodingParam = filterConfig.getInitParameter("encoding");
if (encodingParam != null) {
encoding = encodingParam;
}
}
Register this filter in web.xml 在web.xml中注册此过滤器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.