| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1885 人关注过本帖
标题:[求助]ajax+servlet中文乱码,请您帮忙
只看楼主 加入收藏
yexin218
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2006-12-5
结帖率:100%
收藏
 问题点数:0 回复次数:2 
[求助]ajax+servlet中文乱码,请您帮忙

用户操作界面:jsp文件

代码
  1. <%@ page contentType="text/html;charset=GB2312" language="java" %>
  2. <%@page pageEncoding="GB2312"%>
  3. <head>
  4. <META http-equiv=Content-Type content="text/html; charset=GB2312">
  5. </head>
  6. <script type="text/javascript" src="js/baidusongs.js" ></script>
  7. <script type="text/javascript" src="js/http_request.js"></script>
  8. <title>Bai Du Songs</title>
  9. <style type="text/css">
  10. 。。
  11. <span class="STYLE2">Name of Song(歌名):</span>
  12. <input name=
  13. "song_name" type="text" id="song" value="You" size="20" maxlength="20" />
  14. </p>
  15. <p>
  16. <span class="STYLE2">Name of Singer(歌手):</span>
  17. <input type="text" id="singer" name=
  18. "singer_value" size="20" maxlength="20" />
  19. </p>
  20. <p>
  21. <!--<span id="addr_song"></span><br/>
  22. <span id="addr_word"></span>-->
  23. <div id = "addr_song"></div>
  24. <div id = "addr_word"></div>
  25. </p>
  26. <p>
  27. <button type="button" class="STYLE2" id="songsearch">Search</button>
jsp页面的参数由js函数传送到servlet:
js代码:
代码
  1. window.onload = function(){
  2. document.getElementById("songsearch").onclick=function(){
  3. var _song= document.getElementById("song");
  4. var _singer=document.getElementById("singer");
  5. if(_song.value){
  6. getSong(_song.value,_singer.value);
  7. }
  8. };
  9. //defualt value
  10. //getSong("等一分钟","");
  11. };
  12. function getSong(song,singer){
  13. if(song == null){
  14. document.getElementById("song").value="";
  15. //return ;
  16. }else{
  17. var url = "http://localhost:8080/baidusongs/songsearch?song="+encodeURI(song)+"&singer="+encodeURI(singer)+"&timeStamp="+ new Date().getTime();
  18. // var url = "http://localhost:8080/baidusongs/songsearch?song="+escape(song)+"&singer="+escape(singer)+"&timeStamp="+ new Date().getTime();
  19. httpRequest("GET",url,true,handleResponse);
  20. }
  21. }
  22. ....

之后由servlet接受参数然后进行处理
servlet代码如下:
代码
  1. protected void doGet(HttpServletRequest httpServletRequest,
  2. HttpServletResponse httpServletResponse) throws ServletException,
  3. IOException {
  4. httpServletResponse.setContentType("text/xml;charset=GB2312");
  5. httpServletRequest.setCharacterEncoding("GB2312");
  6. //httpServletResponse.setCharacterEncoding("UTF-8");
  7. //String song = httpServletRequest.getParameter("song");
  8. //String song= new String(httpServletRequest.getParameter("song").getBytes("ISO-8859-1"), "GB2312");
  9. //String singer =new String(httpServletRequest.getParameter("singer").getBytes("ISO-8859-1"), "GB2312");
  10. String song = httpServletRequest.getParameter("song");
  11. String singer = httpServletRequest.getParameter("singer");
  12. System.out.println("Song name:"+song +"\n singer "+singer+"\n");
  13. String resp = null;
  14. if (!song.equals("")) {
  15. URL songUrl = new URL(baiduUrl+song+"$$"+singer+"$$$$");
  16. HttpURLConnection songCon = (HttpURLConnection) songUrl
  17. .openConnection();
  18. songCon.setRequestMethod("GET");
  19. songCon.setDoInput(true);
  20. songCon.connect();
  21. BufferedReader in = new BufferedReader(new InputStreamReader(
  22. songCon.getInputStream()));
  23. StringBuffer buf = new StringBuffer("");
  24. String inputLine;
  25. while ((ininputLine = in.readLine( )) != null) {
  26. buf.append(inputLine); }
  27. in.close( );
  28. resp = buf.toString( );
  29. System.out.println("#######################");
  30. System.out.println(resp);
  31. System.out.println("#######################\n");
  32. try{
  33. getAddress(resp);
  34. resp="<songaddr>"+this.getSongAddr()+"</songaddr><wordaddr>"+this.getWordAddr()+"</wordaddr>";
  35. //System.out.println("_________________"+resp+"\n");
  36. }catch(ParserConfigurationException e){
  37. e.printStackTrace();
  38. }
  39. }else{
  40. resp="<error />";
  41. }
  42. //httpServletResponse.setContentType("text/xml; charset=UTF-8");
  43. //Convey to the user agent or browser that it should
  44. //not cache the responses
  45. // httpServletResponse.setHeader("Cache-Control", "no-cache");
  46. // httpServletResponse.getWriter( ).write(resp);
  47. httpServletResponse.setContentType("text/xml; charset=GB2312");
  48. String content = "<?xml version=\"1.0\" encoding=\"GB2312\"?>"+"<baidu><address><song_addr>"+getRealSongAddr(this.getSongAddr(),this.getSongName())+"</song_addr>"+"<word_addr>"+getRealWordAddr(this.getWordAddr())+"</word_addr></address></baidu>";
  49. System.out.println("*******************\n");
  50. System.out.println(content);
  51. System.out.println("*******************\n");
  52. httpServletResponse.getWriter( ).write(content);
  53. }


可是总是出现乱码现象,比如:
Song name:???
singer
我所有文件的存储encoding都是采用gb2312,编译servlet也采用了javac -encoding gb2312 *.java.
在tomcatweb.xml配置如下:

代码
  1. <?xml version="1.0" encoding="GB2312"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. <!-- General description of your web application -->
  8. <display-name>Echo Servlet</display-name>
  9. <description>
  10. Bai du songs
  11. </description>
  12. <jsp-config>
  13. <jsp-property-group>
  14. <description>Special property group for JSP Configuration JSP example.</description>
  15. <display-name>JSPConfiguration</display-name>
  16. <url-pattern>*.jsp</url-pattern>
  17. <el-ignored>true</el-ignored>
  18. <page-encoding>GB2312</page-encoding>
  19. <scripting-invalid>false</scripting-invalid>
  20. <include-prelude></include-prelude>
  21. <include-coda></include-coda>
  22. </jsp-property-group>
  23. </jsp-config>
  24. <!-- define servlets and mapping -->
  25. <servlet>
  26. <servlet-name>SongSearch</servlet-name>
  27. <servlet-class>baidu.SongSearch</servlet-class>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>SongSearch</servlet-name>
  31. <url-pattern>/songsearch</url-pattern>
  32. </servlet-mapping>
  33. </web-app>
可是问题还是没有得到解决!所以恳请看看。
谢谢!


[此贴子已经被作者于2007-8-17 12:05:02编辑过]

搜索更多相关主题的帖子: ajax servlet 乱码 中文 
2007-08-17 11:58
yexin218
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2006-12-5
收藏
得分:0 
问题已经解决!
String song= new String(httpServletRequest.getParameter("song").getBytes("ISO-8859-1"), "UTF8"); //String singer =new String(httpServletRequest.getParameter("singer").getBytes("ISO-8859-1"), "UTF8");
2007-08-17 16:41
followmephoe
Rank: 1
等 级:新手上路
帖 子:56
专家分:0
注 册:2006-9-5
收藏
得分:0 

你强转或者写一个过滤器都可以解决乱码问题

2007-08-21 14:19
快速回复:[求助]ajax+servlet中文乱码,请您帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065516 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved