CSDN博客

img boboroychen

jsp与servlet中的中文问题

发表于2004/11/1 18:03:00  661人阅读

分类: java相关

jsp页面中一句话解决问题
<%@page contentType="text/html;charset=gb2312" language="java" %>
但是要使客户提交的中文也行那就再来一句
<%request.setCharacterEncoding(”gb2312”);%>

但是不能解决直接手写含中文的url连接到页面,否则传的参数会乱码,也可以用String的构造方法试试

servlet是我折腾了好一阵子的
也是一句话
    response.setContentType(”text/html;charset=gb2312”);效果等同jsp中的那句话
不过。。。。小心哦
它一定要加在 PrinterWriter out=response.getWriter();之前
否则你去抓狂吧,当然你还可以用

     String 的构造函数new String(srcString,"destination.encode");如:
     new String( demoString.getBytes(”这里是源串编码方式”,”iso-8859-1)
这好像一直是救命稻草 换码 哈哈这也是世界化的痛苦哦,但对于要求世界化来说真是福音。
     有人说把中文放在  .properties 文件中
      或许我没做过大项目的缘故,从文件中读所需的中文似乎不是很方便
      对于软件的localization来说也许很有用
      就像c++中的资源文件
      还有一个很好的办法不用每个文件都来句  request.setCharacterEncoding(”gb2312”),那就是filter(我用的tomcat)。写一个服务器端过滤器就可以将所有的request编码为你想要的方式。这种方式同样可以用于身份验证,比如验证访问这个目录的用户是否已登陆,是否有权限,就不用要验证的页面都来个<%@ include file=”checker.jsp”%>了,真是好东西。还是放出我搜集的样例代码吧:
     

//title: filter for transform encoding method to gb2312
//Note:部署filter包含两部分:指定类名、filter名及初始化参数;
//                           指定filter的映射及作用范围(目录)

package jgwl.base;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;
public class myFilter implements Filter{
    protected FilterConfig filterConfig;
    private String targetEncoding="gb2312";
   
    //和一般的servlet一样可以获得初始参数
    public void init(FilterConfig config)throws ServletException {
        this.filterConfig=config;
        this.targetEncoding=config.getInitParameter("encoding");
        System.out.println("!!!!request is encoded in  "+targetEncoding);
    }
   
    //过滤功能在此处实现
    public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
            throws IOException,ServletException{
               //follow line is for debug helping       
              //System.out.println("!!!!request is encoded in ** "+targetEncoding);
        HttpServletRequest request=(HttpServletRequest)req;
        request.setCharacterEncoding(this.targetEncoding);
             //turn the operation right to the next filter
        chain.doFilter(req, res);
    }
   
    public void serFilterConfig(final FilterConfig filterConfig){
        this.filterConfig=filterConfig;
    }
    public void destroy(){
        this.filterConfig=null;
    }
}



//title: filter for transform encoding method to gb2312
//Note:部署filter包含两部分:指定类名、filter名及初始化参数;
//                           指定filter的映射及作用范围(目录)

package jgwl.base;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;
public class myFilter implements Filter{
    protected FilterConfig filterConfig;
    private String targetEncoding="gb2312";
   
    //和一般的servlet一样可以获得初始参数
    public void init(FilterConfig config)throws ServletException {
        this.filterConfig=config;
        this.targetEncoding=config.getInitParameter("encoding");
        System.out.println("!!!!request is encoded in  "+targetEncoding);
    }
   
    //过滤功能在此处实现
    public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain)
            throws IOException,ServletException{
               //follow line is for debug helping       
              //System.out.println("!!!!request is encoded in ** "+targetEncoding);
        HttpServletRequest request=(HttpServletRequest)req;
        request.setCharacterEncoding(this.targetEncoding);
             //turn the operation right to the next filter
        chain.doFilter(req, res);
    }
   
    public void serFilterConfig(final FilterConfig filterConfig){
        this.filterConfig=filterConfig;
    }
    public void destroy(){
        this.filterConfig=null;
    }
}


阅读全文
0 0

相关文章推荐

img
取 消
img