ajax取歌詞(*歌曲地址是中文的會出錯)

來源:互聯網
上載者:User

 取歌詞(歌曲地址是中文的會出錯)

play.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312"%>
<%@ page import="com.pp.db.*" %>
<%@ page import="my.bean.*" %>
<!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=gbk">
<title>index</title>
</head>
<script language="javascript" type="text/javascript">

function reverseSelect()
{

    for(var i=0;i<songList.elements.length;i++)
        if(songList.elements[i].checked == true)
            songList.elements[i].checked = false;
        else
            songList.elements[i].checked = true;
}
function AllSelect()
{
    for(var i=0;i<songList.elements.length;i++)
        songList.elements[i].checked=true;
}
</script>
<body>
<h2 align="center"><em><strong><font color="#FF0000">線上音樂播放示範</font></strong></em></h2>
<form name="songList" method="post" action="Process.jsp">
    <div id="Layer1" style="position:absolute; width:783px; height:154px; z-index:1; left: -5px; top: 141px;">
    <table width="780" border="0" cellspacing="0" cellpadding="5">
     <%
      DBOperate dbo=new DBOperate();
     dbo.setLength(10);
      String hql="from  Music order by orders "; 
      List mylist=dbo.findSQLHibernate(hql,1);
      int n=mylist.size();
      for(int i=0;i<mylist.size();i++)
      {
         Music music=(Music)mylist.get(i);
  
      %>
       <tr>
        <td width="714"><ul><li><%=new String(music.getName().getBytes("iso-8859-1"),"GB2312")%></li></ul></td>
        <td width="25"><input type="checkbox" name="name<%=i%>" value="<%=music.getId() %>"></td>
      </tr>
     <%
         }
      %>
    </table>
  </div>
      <input type="button" name="All" value=" 全 選 " onClick="AllSelect()">
      <input type="button" name="Reverse" value=" 反 選 " onClick="reverseSelect()">
      <input type="reset" name="Reset" value=" 重 置 ">
      <input type="submit" name="Submit" value=" 播 放 ">
      <input type="hidden"  name="size" value="<%=n%>">
</form>

 

</body>
</html>

 

Process.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312"%>
<%@ page import="com.pp.db.*" %>
<%@ page import="my.bean.*" %>
<%@ page import="com.jetsum.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'Process.jsp' starting page</title>
   
    <script type="text/javascript" src="net.js"></script>
    <style type="text/css">
body,td,th {
font-size: 12px;
}
.a1{background-color:#99FFCC;}
.a2{background-color:#FFFFFF;}
a:visited {color: #000000;text-decoration: none;font-size: 9pt;}
a:link {text-decoration: none;color: #000000;font-size: 9pt;}
a:hover {text-decoration: underline;font-size: 9pt;color: #FF0000;}
#showList{display:none1;border:1px solid #99CCFF;position:absolute;float:left;top:21px;z-index:20;left:0px;width:214px;filter:alpha(opacity=0);}
#playlist{height:272px;overflow-y: scroll;}
#manage{position:absolute;float:left;top:21px;z-index:20;left:223px;filter:alpha(opacity=0);}
/*a:visited {font-size: 9pt;color: #996600;}*/
</style>
  </head>
 
 <body onload="PlayMusic()"> 
    <%
      DBOperate dbo=new DBOperate();
       Music music=new Music();
      int size=0;
      
       String sid="";
         String strid="";
      if(request.getParameter("size")!=null)
      {
        size= PubFunc.parseInt(request.getParameter("size"));
       }
      for(int i=0;i<size;i++)
        {
     
          if(request.getParameter("name"+i)!=null)
          {
            // System.out.println("選了我,我要播放了!!!");
           String id=request.getParameter("name"+i);
             sid=id+",";
             strid+=sid;
          }
        }
            System.out.println("strid:"+strid);
        if(strid.trim().equals("")||strid.trim()=="")
        {
         System.out.println("狗屁都不用播放");
        }
        else
        {
          String hql="from Music where id in("+strid.substring(0,strid.length()-1)+")";
        
          List mylist= dbo.findSQLHibernate(hql,1);
  
     
     %>

 <!--播放器 開始-->
 

<script language="javascript" for="MediaPlayerObject" event="playStateChange(NewState)">
  switch (NewState)
{
        case 1:
                //alert("");
                break;
        case 2:
                //alert("Paused");
                break;
        case 3:
                //alert("Playing");
                break; 
         case 8:
                //alert("stopped");
                setTimeout("PlayMusic();",1000);   break;    
        default:
                //alert(NewState.toString());
                break;
}

</script>
<script language="javascript">
function PlayMusic()
{
    var player = document.getElementById("MediaPlayerObject");
    var index = PlayListSelect.selectedIndex + 1;
    if (index > PlayListSelect.options.length -1)
        {
                index = 0;
        }
     PlayListSelect.selectedIndex = index;
     player.URL = PlayListSelect.options[index].value;
     //  alert(player.URL);
     player.controls.play();
        FillLyric(PlayListSelect,lyric);
}

function PlayLast()   // 上一曲
 {     
 var player = document.getElementById("MediaPlayerObject");   
 var index = PlayListSelect.selectedIndex - 1;   
 if (index <0)     
    {     
     index = PlayListSelect.options.length-1;    
    }    
 PlayListSelect.selectedIndex = index;
 player.URL = PlayListSelect.options[index].value;    
   //alert(player.URL);  
 player.controls.play(); 
 FillLyric(PlayListSelect,lyric);
 }
 function FillLyric(oElem,oTarget){
    // alert("strat ajax");
        var strValue = oElem.options[oElem.selectedIndex].value;
        alert(strValue);
        var url = 'getLyric.jsp';
        var strParams = 'q=' + strValue +
                        "&e=" + oTarget.name;
        var loader1 = new
        net.ContentLoader(url,FillDropDown,null,
                    "POST",strParams);
      }
      function FillDropDown(){
       // alert("back");
        var xmlDoc = this.req.responseXML.documentElement;
        //alert(this.req.responseText);       //檢查一下傳回值
     //  alert(xmlDoc);
        var xSel = xmlDoc.getElementsByTagName("selectElement")[0];
      //  alert(xSel);
        var strFName = xSel.childNodes[0].firstChild.nodeValue;
        var strEName = xSel.childNodes[1].firstChild.nodeValue;
  //alert("strFName"+strFName);
  //alert("strEName"+strEName);
  var strText=xSel.childNodes[1].firstChild.nodeValue;
  document.getElementById(strFName).value=strEName;
      }
</script>
        </head>
        <body onload="PlayMusic()">
                <object id="MediaPlayerObject" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" style="width: 300px; height:63 px;">
                        <param name="DisplayBackColor" value="0xffffff">
                        <param name="DisplayForeColor" value="16777215">
                        <param name="rate" value="1">
                        <param name="balance" value="0">
                        <param name="currentPosition" value="0">
                        <param name="defaultFrame" value>
                        <param name="playCount" value="1">
                        <param name="autoStart" value="-1">
                        <param name="currentMarker" value="0">
                        <param name="invokeURLs" value="-1">
                        <param name="baseURL">
                        <param name="volume" value="75">
                        <param name="mute" value="0">
                        <param name="uiMode" value="full">
                        <param name="stretchToFit" value="-1">
                        <param name="windowlessVideo" value="0">
                        <param name="enabled" value="-1">
                        <param name="enableContextMenu" value="-1">
                        <param name="fullScreen" value="0">
                        <param name="SAMIStyle">
                        <param name="SAMILang">
                        <param name="SAMIFilename">
                        <param name="captioningID">
                        <param name="enableErrorDialogs" value="0">
                </object>

                <br>
                ---連播列表---
                <br>
                <select multiple="multiple" id="PlayListSelect">
                <%
                        for(int i=0;i<mylist.size();i++)
                      {
                       music=(Music)mylist.get(i);
                          %>
                           <option value="<%=new String(music.getUrl().getBytes("iso-8859-1"),"GB2312") %>"><%=new String(music.getName().getBytes("iso-8859-1"),"GB2312") %></option>
                           <%
                       }
                      }
                 %>
                    
                </select>
<br>
 <button onclick="PlayLast()">上一曲</button>
 <button onclick="PlayMusic()">下一曲</button> 

 <!--播放器 結束-->
    <!--歌詞 開始-->
  <textarea cols="20" rows="10" name="lyric" id="lyric">
  </textarea>
   <!--歌詞 結束-->
  </body>
</html>

getLyric.jsp
<%@ page language="java" import="java.util.*,java.sql.*,java.io.*" pageEncoding="GB2312"%>
<jsp:directive.page import="com.pp.db.DBOperate,my.bean.*"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'DoubleComboXML.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
   <%
   request.setCharacterEncoding("GB2312");
    String strQuery  =new String(request.getParameter("q").getBytes("iso-8859-1"),"GB2312");
    // String strQuery  =request.getParameter("q");
    String strElem=request.getParameter("e");
System.out.println("strQuery"+strQuery);
  DBOperate dbo=new DBOperate();
  String hqlmusic="from Music where url='"+strQuery+"'";   //是按照歌曲的地址來找歌,所有url不能相同
  System.out.println("hqlmusic"+hqlmusic);
 List listmusic =dbo.findSQLHibernate(hqlmusic,1);
        StringBuilder strXML =new StringBuilder("<?xml version='1.0' ?>");
        if(listmusic.size()>0)
        {
          Music music = (Music)listmusic.get(0);
           strXML.append("<selectChoice>");
            strXML.append("<selectElement>");
             strXML.append("<formName>"+strElem+"</formName>");
              strXML.append("<formElem>"+music.getLyric()+"</formElem>");
               strXML.append(" </selectElement>");
                strXML.append(" </selectChoice>");
        }
         response.setContentType("text/xml");
         PrintWriter out2= response.getWriter();
         out2.write(strXML.toString());
         out2.close();
    %>
  </body>
</html>

附件: musichoust中文亂碼版.rar

ajax中文問題
  var strValue = music.value;    //歌曲的路徑
        alert(strValue);          //    E:/music/張敬軒-分手前的雨天.mp3
        var url = 'getLyric.jsp';
        var strParams = 'q=' + strValue;  //傳值
        //發送 
      var loader1 = new
        net.ContentLoader(url,FillDropDown,null,
                    "POST",strParams);

getLyric.jsp
//取值
String strQuery  =new String(request.getParameter("q").getBytes("iso-8859-1"),"GB2312");//出亂碼

了...

AJAX只支援UTF-8?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.