问题:搞不懂HttpServletRequest的getCharacterEncoding()方法了

运行Struts中那个upload的例子,当使用那个upload-utf8.jsp上传文件时,上传后转到display.jsp时,显的总是乱码。就算在display.jsp中设置了<%@ page contentType="text/html; charset=utf-8" %>也没有用。

如下:

upload-utf8.jsp的设置:


<%@ page language="java" contentType="text/html; charset=gbk" %>

action:


            String encoding = request.getCharacterEncoding();

            System.out.println("the encoding is: "+ encoding);

            if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))

            {

                response.setContentType("text/html; charset=utf-8");

            }

虽然在upload-utf8.jsp中也指定了utf8编码,但是在request.getCharacterEncoding();得到的encoding总是null,也就是没有在request中没有指定编码,明明是指定了的呀?怪事。

还有,就是那个upload.jsp,在昨天的运行中还是在display.jsp中出现了乱码,可是今天却没有了,搞不懂,怪事。

3 thoughts on “问题:搞不懂HttpServletRequest的getCharacterEncoding()方法了”

  1. 编码问题确实挺郁闷的,曾经为编码问题研究了很长时间。 通过虚拟机编译是uncode码编码和解码, 通过ie是通过iso8859-1解码,谁让IE是微软做的呢。 在工作的时候,遇到编码问题,最简单的方法就是,把所有的码设置为utf-8 不知道这回复您能不能有幸看到,如果看过了,然后工作中还有什么问题,加我MSN或者发邮件都可以。 qww_java@hotmail.com 希望能够共同研究

Comments are closed.