日期:2014-05-16 浏览次数:20714 次
·-- 首先开发中要注意的一个问题,这个问题困扰我好几个小时,编码问题。
如果在Servlet进行如下设置时:
???????????????????? resp.setContentType("text/xml;charset=UTF-8");
??? ???? resp.setCharacterEncoding("UTF-8");
IE8和火狐时没有问题的,但是换成如下形式,
???????????????????? resp.setContentType("text/xml;charset=UTF-8");
??? ???????????????? resp.setCharacterEncoding("GBK");
或者
???????????????????? resp.setContentType("text/xml;charset=GBK");
??? ???????????????? resp.setCharacterEncoding("GBK");
时,会出现的现象是火狐操作正常,IE操作不正常。原理暂时不清楚,等待高手指点。
1、新建一个Servlet
package com.sunrex.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class AjaxServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/xml;charset=UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Cache-Control", "no-cache"); StringBuffer sb = new StringBuffer(); sb.append("<nation>"); sb.append("<detail>"); sb.append("<address>").append("中国").append("</address>"); sb.append("<city>").append("上海").append("</city>"); sb.append("</detail>"); sb.append("<detail>"); sb.append("<address>").append("美国").append("</address>"); sb.append("<city>").append("纽约").append("</city>"); sb.append("</detail>"); sb.append("<detail>"); sb.append("<address>").append("日本").append("</address>"); sb.append("<city>").append("东京").append("</city>"); sb.append("</detail>"); sb.append("</nation>"); System.out.println(sb.toString()); PrintWriter out = resp.getWriter(); out.write(sb.toString()); out.close(); } }
?
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ajax</servlet-name> <servlet-class>com.sunrex.servlet.AjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ajax</servlet-name> <url-pattern>/servlet/ajax</url-pattern> </servlet-mapping> </web-app>
?
3、jsp文件
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> <script type="text/javascript"> var xmlHttp; function createXMLHTTP() { if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } function show() { createXMLHTTP(); var url = "servlet/ajax"; xmlHttp.open("GET", url); xmlHttp.onreadystatechange = callback; xmlHttp.send(null); } function callback() { if(xmlHttp.readyState == 4) { if(xmlHttp.status == 200) { var xmlText = xmlHttp.responseXML.getElementsByTagName("nation")[0]; var childrenNodes = xmlText.childNodes; var nodeLength = childrenNodes.length; var tables = "<table border='1'>"; for(var