public class GetNameServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");

PrintWriter out=resp.getWriter();
BufferedReader reader = new BufferedReader(new FileReader(new File("E:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\ServletPro\\XML\\ProName.xml")));
    String content="";
    while(true){
      String s=reader.readLine();//
       if(s == null){
        break;
       }
       content=content+s;
    } out.println(content);
} protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}}servlet中的内容如上所示,读取得文件格式为<?xml version="1.0" encoding="utf-8"?>
<programs>
<program name="中石油" id="10" />
<program name="中石化" id="20" />
<program name="中移动" id="30" />
<program name="中联通" id="40" />
</programs>
我用浏览器访问的时候显示一下内容:
  <?xml version="1.0" encoding="utf-8" ?> 
- <programs>
  <program name="ヨミハッモヘ" id="10" /> 
  <program name="ヨミハッサッ" id="20" /> 
  <program name="ヨミメニカッ" id="30" /> 
  <program name="ヨミチェヘィ" id="40" /> 
  </programs>
这个乱码如何解决 请指教 谢谢

解决方案 »

  1.   

    编码问题,
    resp.setContentType("text/html;charset=utf-8");
    resp.setCharacterEncoding("utf-8");
    改为:
    resp.setContentType("text/html;charset=GBK");
    resp.setCharacterEncoding("GBK");
      

  2.   

    用IE试下,然后改变页面的编码成UTF-8
      

  3.   

    好好找一下,看看整个系统还有哪里用到了除UTF-8以外的编码方式应该由于编码不统一造成的
      

  4.   

    LZ有没有用过滤器?
    加上过滤器应该可以啊。  public class FourmChainValidate implements Filter {
    private String encoding; public void destroy() {
    } public void doFilter(ServletRequest arg0, ServletResponse arg1,
    FilterChain arg2) throws IOException, ServletException {
    arg0.setCharacterEncoding(encoding);
    HttpServletRequest req = (HttpServletRequest) arg0;
    String type = req.getHeader("Content-Type");
    if (type == null || !type.startsWith("multipart/form-data")) {
    arg2.doFilter(arg0, arg1);
    } else {
    HttpServletRequestWrapper multi = new HttpServletRequestWrapper(req);
    req.setCharacterEncoding(encoding);
    arg2.doFilter(multi, arg1);
    }
    } public void init(FilterConfig filterConfig) throws ServletException {
    encoding = filterConfig.getInitParameter("encoding");
    }}
    web.xml <filter>
        <filter-name>FilterChain</filter-name>
        <filter-class>com.call.servlet.FourmChainValidate</filter-class>//类位置
        <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>FilterChain</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      

  5.   

    过滤器也只能过滤post提交的,get提交的不能生效的。
    你看看浏览器使用的编码,选择GB2313或是UTF-8试试。
      

  6.   

    把用到编码的地方,全部都改成统一的,要不用GBK,要不用UTF-8.
    页面上的编码也应该一致.
      

  7.   

    request.setCharacterEncoding("GB2312");
    这种方式只对post方式有效....
    先写个过滤器
    再写个包装类把post方式的排除;get的用这种方式解决
    例如:String name;//为表单的数据
    String realName=new String(name.getByte("原有的编码"),"要转的编码");
    post get 通吃.......