jsp

位置:IT落伍者 >> jsp >> 浏览文章

解决JSP开发Web程序中的中文问题[1]


发布日期:2024年05月05日
 
解决JSP开发Web程序中的中文问题[1]

这段时间经常看到有人问到web开发中怎么中文总是?号原因其实很简单因为大家大多用的是tomcat服务器而tomcat服务器的默认编码为 iso(西欧字符)就是因为iso(西欧字符)编码造成了我们经常看到?号

方法一最简单也是用的最多的方法

<%@ page language=java pageEncoding=GBK %>

或者<%@ page contenttype=text/html;charset=gbk;>这里可以用gb或者gbk只是gbk比gb支持跟多的字符

这个方法用于jsp页面中的中文显示

方法二使用过滤器

过滤器使用主要针对表单提交插入数据库的数据都是?号这也是应为tomcat不按request所指定的编码进行编码还是自作主张的采用默认编码方式iso编码

编写一个SetCharacterEncodingFilter类

import javaioIOException;

import javaxservletFilter;

import javaxservletFilterChain;

import javaxservletFilterConfig;

import javaxservletServletException;

import javaxservletServletRequest;

import javaxservletServletResponse;

public class SetCharacterEncodingFilter implements Filter {

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;

public void init(FilterConfig filterConfig) throws ServletException {

thisfilterConfig=filterConfig;

thisencoding=filterConfiggetInitParameter(encoding);

String value=filterConfiggetInitParameter(ignore);

if(value==null)

thisignore=true;

else if(valueequalsIgnoreCase(true))

thisignore=true;

else

thisignore=false;

}

public void doFilter(ServletRequest request ServletResponse response FilterChain chain) throws IOException ServletException {

// TODO 自动生成方法存根

if (ignore || (requestgetCharacterEncoding() == null)) {

String encoding = selectEncoding(request);

if (encoding != null)

requestsetCharacterEncoding(encoding);

}

chaindoFilter(request response);

}

public void destroy() {

// TODO 自动生成方法存根

thisencoding = null;

thisfilterConfig = null;

}

protected String selectEncoding(ServletRequest request) {

return (thisencoding);

}

}

[] []

               

上一篇:解决JSP开发Web程序中的中文问题[2]

下一篇:JSP的九个隐含对象[3]