﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Alex刺客-随笔分类-Server Config</title><link>http://www.blogjava.net/xiangjava/category/45595.html</link><description>代码也是一种艺术，调试也是一种快乐！处理Bug更是一种幸福！</description><language>zh-cn</language><lastBuildDate>Sat, 31 Jul 2010 01:17:25 GMT</lastBuildDate><pubDate>Sat, 31 Jul 2010 01:17:25 GMT</pubDate><ttl>60</ttl><item><title>让Tomcat URL 支持中文</title><link>http://www.blogjava.net/xiangjava/archive/2010/07/15/326199.html</link><dc:creator>Alex刺客</dc:creator><author>Alex刺客</author><pubDate>Thu, 15 Jul 2010 07:33:00 GMT</pubDate><guid>http://www.blogjava.net/xiangjava/archive/2010/07/15/326199.html</guid><wfw:comment>http://www.blogjava.net/xiangjava/comments/326199.html</wfw:comment><comments>http://www.blogjava.net/xiangjava/archive/2010/07/15/326199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/xiangjava/comments/commentRss/326199.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/xiangjava/services/trackbacks/326199.html</trackback:ping><description><![CDATA[Tomcat服务器.在使用 Ajax get方法的时候中文参数提交到了服务器变成了乱码.<br />这是因为 Tomcat 默认的 URL 编码为 <tt>ISO-8859-1 导致的.以下有两种解决方案.<br /><br />1.java转换编码方式<br /></tt><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);">     * 转换字符串编码<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 128, 0);">     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> str　要进行转换的字符串<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 128, 0);">     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> oldCode 指定str的编码<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 128, 0);">     * </span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);"> newCode 目标字符串编码<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 128, 0);">     * </span><span style="color: rgb(128, 128, 128);">@return</span><span style="color: rgb(0, 128, 0);">　目标字符串<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 128, 0);">     * </span><span style="color: rgb(128, 128, 128);">@throws</span><span style="color: rgb(0, 128, 0);"> UnsupportedEncodingException<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 128, 0);">     </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">public static</span><span style="color: rgb(0, 0, 0);"> String transcoding(String str,String oldCode, String newCode) </span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);"> UnsupportedEncodingException{<br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[] bytes </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> str.getBytes(oldCode);<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">        String newStr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> String(bytes, newCode);<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> newStr;<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    }</span></div><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">String name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> request.getParameter(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">String resultStr </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> transcoding(name, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">ISO-8859-1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">UTF-8</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);">System.out.println(resultStr);</span></div><br />2.指定Tomcat URL编码<span style="color: rgb(0, 0, 255);"><br /></span><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Connector </span><span style="color: rgb(255, 0, 0);">port</span><span style="color: rgb(0, 0, 255);">="80"</span><span style="color: rgb(255, 0, 0);"> protocol</span><span style="color: rgb(0, 0, 255);">="HTTP/1.1"</span><span style="color: rgb(255, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(255, 0, 0);">               connectionTimeout</span><span style="color: rgb(0, 0, 255);">="20000"</span><span style="color: rgb(255, 0, 0);"> <br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(255, 0, 0);">               redirectPort</span><span style="color: rgb(0, 0, 255);">="8444"</span><span style="color: rgb(255, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(255, 0, 0);">               URIEncoding</span><span style="color: rgb(0, 0, 255);">="UTF-8"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></div>在配置http端口的地址添上<span style="color: rgb(255, 0, 0);">URIEncoding</span><span style="color: rgb(0, 0, 255);">="UTF-8".<br /></span><br />这样就可这直接获取 URL 的属性<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">String name </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> request.getParameter(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">name</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">2</span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 0, 0);">System.out.println(name);</span></div><br /><img src ="http://www.blogjava.net/xiangjava/aggbug/326199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/xiangjava/" target="_blank">Alex刺客</a> 2010-07-15 15:33 <a href="http://www.blogjava.net/xiangjava/archive/2010/07/15/326199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>