#2
yexin2182007-08-17 16:41
|
用户操作界面:jsp文件
代码
- <%@ page contentType="text/html;charset=GB2312" language="java" %>
- <%@page pageEncoding="GB2312"%>
- <head>
- <META http-equiv=Content-Type content="text/html; charset=GB2312">
- </head>
- <script type="text/javascript" src="js/baidusongs.js" ></script>
- <script type="text/javascript" src="js/http_request.js"></script>
- <title>Bai Du Songs</title>
- <style type="text/css">
- 。。
- <span class="STYLE2">Name of Song(歌名):</span>
- <input name=
- "song_name" type="text" id="song" value="You" size="20" maxlength="20" />
- </p>
- <p>
- <span class="STYLE2">Name of Singer(歌手):</span>
- <input type="text" id="singer" name=
- "singer_value" size="20" maxlength="20" />
- </p>
- <p>
- <!--<span id="addr_song"></span><br/>
- <span id="addr_word"></span>-->
- <div id = "addr_song"></div>
- <div id = "addr_word"></div>
- </p>
- <p>
- <button type="button" class="STYLE2" id="songsearch">Search</button>
js代码:
代码
- window.onload = function(){
- document.getElementById("songsearch").onclick=function(){
- var _song= document.getElementById("song");
- var _singer=document.getElementById("singer");
- if(_song.value){
- getSong(_song.value,_singer.value);
- }
- };
- //defualt value
- //getSong("等一分钟","");
- };
- function getSong(song,singer){
- if(song == null){
- document.getElementById("song").value="";
- //return ;
- }else{
- var url = "http://localhost:8080/baidusongs/songsearch?song="+encodeURI(song)+"&singer="+encodeURI(singer)+"&timeStamp="+ new Date().getTime();
- // var url = "http://localhost:8080/baidusongs/songsearch?song="+escape(song)+"&singer="+escape(singer)+"&timeStamp="+ new Date().getTime();
- httpRequest("GET",url,true,handleResponse);
- }
- }
- ....
之后由servlet接受参数然后进行处理
servlet代码如下:
代码
- protected void doGet(HttpServletRequest httpServletRequest,
- HttpServletResponse httpServletResponse) throws ServletException,
- IOException {
- httpServletResponse.setContentType("text/xml;charset=GB2312");
- httpServletRequest.setCharacterEncoding("GB2312");
- //httpServletResponse.setCharacterEncoding("UTF-8");
- //String song = httpServletRequest.getParameter("song");
- //String song= new String(httpServletRequest.getParameter("song").getBytes("ISO-8859-1"), "GB2312");
- //String singer =new String(httpServletRequest.getParameter("singer").getBytes("ISO-8859-1"), "GB2312");
- String song = httpServletRequest.getParameter("song");
- String singer = httpServletRequest.getParameter("singer");
- System.out.println("Song name:"+song +"\n singer "+singer+"\n");
- String resp = null;
- if (!song.equals("")) {
- URL songUrl = new URL(baiduUrl+song+"$$"+singer+"$$$$");
- HttpURLConnection songCon = (HttpURLConnection) songUrl
- .openConnection();
- songCon.setRequestMethod("GET");
- songCon.setDoInput(true);
- songCon.connect();
- BufferedReader in = new BufferedReader(new InputStreamReader(
- songCon.getInputStream()));
- StringBuffer buf = new StringBuffer("");
- String inputLine;
- while ((ininputLine = in.readLine( )) != null) {
- buf.append(inputLine); }
- in.close( );
- resp = buf.toString( );
- System.out.println("#######################");
- System.out.println(resp);
- System.out.println("#######################\n");
- try{
- getAddress(resp);
- resp="<songaddr>"+this.getSongAddr()+"</songaddr><wordaddr>"+this.getWordAddr()+"</wordaddr>";
- //System.out.println("_________________"+resp+"\n");
- }catch(ParserConfigurationException e){
- e.printStackTrace();
- }
- }else{
- resp="<error />";
- }
- //httpServletResponse.setContentType("text/xml; charset=UTF-8");
- //Convey to the user agent or browser that it should
- //not cache the responses
- // httpServletResponse.setHeader("Cache-Control", "no-cache");
- // httpServletResponse.getWriter( ).write(resp);
- httpServletResponse.setContentType("text/xml; charset=GB2312");
- 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>";
- System.out.println("*******************\n");
- System.out.println(content);
- System.out.println("*******************\n");
- httpServletResponse.getWriter( ).write(content);
- }
可是总是出现乱码现象,比如:
Song name:???
singer
我所有文件的存储encoding都是采用gb2312,编译servlet也采用了javac -encoding gb2312 *.java.
在tomcatweb.xml配置如下:
代码
- <?xml version="1.0" encoding="GB2312"?>
- <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">
- <!-- General description of your web application -->
- <display-name>Echo Servlet</display-name>
- <description>
- Bai du songs
- </description>
- <jsp-config>
- <jsp-property-group>
- <description>Special property group for JSP Configuration JSP example.</description>
- <display-name>JSPConfiguration</display-name>
- <url-pattern>*.jsp</url-pattern>
- <el-ignored>true</el-ignored>
- <page-encoding>GB2312</page-encoding>
- <scripting-invalid>false</scripting-invalid>
- <include-prelude></include-prelude>
- <include-coda></include-coda>
- </jsp-property-group>
- </jsp-config>
- <!-- define servlets and mapping -->
- <servlet>
- <servlet-name>SongSearch</servlet-name>
- <servlet-class>baidu.SongSearch</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>SongSearch</servlet-name>
- <url-pattern>/songsearch</url-pattern>
- </servlet-mapping>
- </web-app>
谢谢!
[此贴子已经被作者于2007-8-17 12:05:02编辑过]