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>
这个乱码如何解决 请指教 谢谢
解决方案 »
- 怎样判断类围棋中的联通区域
- 想放弃5K的工资,自己创业,拥有不平凡的生活,恳请得到指点
- 严重: 读取文件日志错误java.io.IOException: Couldn't get lock for ...\2012-02-03.log
- 要如何才能实现将Class类型转化为自定义类类型?
- 怎么设置表格中的某几行数据的颜色
- 30分钟写一道程序(C、C++、Java、伪码),昨天的面试题目!!!
- 请问,java中有什么共同的函数可以用来控制线程同步?(不用synchronized)在线等待。
- java字符串查找与替换
- 关于 new一个java子类需先实例化父类的思考
- 猜拳游戏
- JAVA中得ArrayList<T> 是泛型类么?
- 简单的反射获取方法问题
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
改为:
resp.setContentType("text/html;charset=GBK");
resp.setCharacterEncoding("GBK");
加上过滤器应该可以啊。 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>
你看看浏览器使用的编码,选择GB2313或是UTF-8试试。
页面上的编码也应该一致.
这种方式只对post方式有效....
先写个过滤器
再写个包装类把post方式的排除;get的用这种方式解决
例如:String name;//为表单的数据
String realName=new String(name.getByte("原有的编码"),"要转的编码");
post get 通吃.......