<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA

    人生若只如初見,何事秋風(fēng)悲畫扇。

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      50 隨筆 :: 25 文章 :: 157 評論 :: 0 Trackbacks

    首先看下看下相對簡單些的--向服務(wù)器發(fā)送一個包含有名/值對的簡單查詢串

    在這種情況下XHP即可以用GET也可以用POST。

    GET

    function ?doRequestUsingGET()? {
    ????createXMLHttpRequest();
    ????
    ????
    var ?queryString? = ? " GetAndPostExample? " ;
    ????queryString?
    = ?queryString? + ?createQueryString()?
    ????????
    + ? " &timeStamp= " ? + ? new ?Date().getTime();
    ????xmlHttp.onreadystatechange?
    = ?handleStateChange;
    ????xmlHttp.open(
    " GET " ,?queryString,? true );
    ????xmlHttp.send(
    null );
    }


    POST

    function ?doRequestUsingPOST()? {
    ????createXMLHttpRequest();
    ????
    ????
    var ?url? = ? " GetAndPostExample?timeStamp= " ? + ? new ?Date().getTime();
    ????
    var ?queryString? = ?createQueryString();
    ????
    ????xmlHttp.open(
    " POST " ,?url,? true );
    ????xmlHttp.onreadystatechange?
    = ?handleStateChange;
    ????xmlHttp.setRequestHeader(
    " Content-Type " ,? " application/x-www-form-urlencoded " );????
    ????xmlHttp.send(queryString);
    }


    queryString就是名/值對的參數(shù)形式了(如name=LiLin&age=23),在調(diào)用OPEN方法中,當(dāng)請求方法是用POST的時候?yàn)榱舜_保服務(wù)器知道請求體中有請求參數(shù),需要調(diào)用setRequestHeader,將Content-Type值設(shè)置為
    application/x-www-form-urlencoded.當(dāng)然也可不放在請求體中(那就不要用POST啦?。?/p>

    此時server處理:

    import ?java.io. * ;
    import ?java.net. * ;
    import ?javax.servlet. * ;
    import ?javax.servlet.http. * ;

    public ? class ?GetAndPostExample? extends ?HttpServlet? {
    ????
    ????
    protected ? void ?processRequest(HttpServletRequest?request,?
    ????????????HttpServletResponse?response,?String?method)
    ????
    throws ?ServletException,?IOException? {
    ????????
    ????????
    // Set?content?type?of?the?response?to?text/xml
    ????????response.setContentType( " text/xml " );
    ????????
    ????????
    // Get?the?user's?input
    ????????String?firstName? = ?request.getParameter( " firstName " );
    ????????String?middleName?
    = ?request.getParameter( " middleName " );
    ????????String?birthday?
    = ?request.getParameter( " birthday " );
    ????????
    ????????
    // Create?the?response?text
    ????????String?responseText? = ? " Hello? " ? + ?firstName? + ? " ? " ? + ?middleName
    ????????????????
    + ? " .?Your?birthday?is? " ? + ?birthday? + ? " . "
    ????????????????
    + ? " ?[Method:? " ? + ?method? + ? " ] " ;
    ????????
    ????????
    // Write?the?response?back?to?the?browser
    ????????PrintWriter?out? = ?response.getWriter();
    ????????out.println(responseText);

    ????????
    // Close?the?writer
    ????????out.close();
    ????}

    ????
    ????
    protected ? void ?doGet(HttpServletRequest?request,?HttpServletResponse?response)
    ????
    throws ?ServletException,?IOException? {
    ????????
    // Process?the?request?in?method?processRequest
    ????????processRequest(request,?response,? " GET " );
    ????}

    ????
    ????
    protected ? void ?doPost(HttpServletRequest?request,?HttpServletResponse?response)
    ????
    throws ?ServletException,?IOException? {
    ????????
    // Process?the?request?in?method?processRequest
    ????????processRequest(request,?response,? " POST " );
    ????}

    }


    對get and post方法都用processRequest來處理。



    要向服務(wù)器發(fā)送相關(guān)復(fù)雜的查詢串,可以將模型變化為XML發(fā)送到server 。

    client端:

    function ?createXML()? {
    ????
    var ?xml? = ? " <pets> " ;
    ????
    ????
    var ?options? = ?document.getElementById( " petTypes " ).childNodes;
    ????
    var ?option? = ? null ;
    ????
    for ( var ?i? = ? 0 ;?i? < ?options.length;?i ++ )? {
    ????????option?
    = ?options[i];
    ????????
    if (option.selected)? {
    ????????????xml?
    = ?xml? + ? " <type> " ? + ?option.value? + ? " <\/type> " ;
    ????????}

    ????}

    ????
    ????xml?
    = ?xml? + ? " <\/pets> " ;
    ????
    return ?xml;
    }


    function ?sendPetTypes()? {
    ????createXMLHttpRequest();
    ????
    ????
    var ?xml? = ?createXML();
    ????
    var ?url? = ? " PostingXMLExample?timeStamp= " ? + ? new ?Date().getTime();
    ????
    ????xmlHttp.open(
    " POST " ,?url,? true );
    ????xmlHttp.onreadystatechange?
    = ?handleStateChange;
    ????xmlHttp.setRequestHeader(
    " Content-Type " ,? " application/x-www-form-urlencoded " );????
    ????xmlHttp.send(xml);
    }



    createXML方法無非就是將內(nèi)容以DOM的樣式存到var xml(變量)里。有時也可能出現(xiàn)client直接將本地的一個XML文件
    直接以DOM(當(dāng)然可以edit)的樣式傳送.(也放這個時個的Content-Type應(yīng)該為text/xml了!)
    這時可能要用到ActiveXObject("MSXML2.DOMDocument.3.0")這樣一個控件了。
    關(guān)于這個控件有個方法可以在各broswer中通用的JS代碼:

    // --------------------------------------------------------------------
    //
    ?Function:?CreateXMLDOM
    //
    //
    ?Purpose:?Creates?a?new?XML?DOM.
    //
    //
    ?Parameters:?None
    //
    //
    ?Returns:?XMLDOM?object?OR?null
    //
    --------------------------------------------------------------------
    function ?CreateXmlDOM()
    {
    ?
    var ?oXML? = ? new ?ActiveXObject(GetXmlParserProgID());
    ?
    try
    ?
    {
    ??oXML.setProperty(
    " AllowXsltScript " ,? true );
    ?}

    ?
    catch (err) {}
    ?
    ?oXML.async?
    = ? false ;
    ?oXML.validateOnParse?
    = ? false ;
    ?oXML.resolveExternals?
    = ? false ;
    ?oXML.setProperty(
    " SelectionLanguage " ,? " XPath " );
    ?
    try ? {oXML.setProperty( " NewParser " ,? true );} ? catch (e) {}

    ?
    return ?oXML;
    }


    // --------------------------------------------------------------------
    //
    ?Function:?GetXmlParserProgID
    //
    //
    ?Purpose:
    //
    ?Gets?the?ProgID?of?the?highest?available?version?of?the?
    //
    ?Microsoft?XML?parser.
    //
    //
    ?Parameters:?None
    //
    //
    ?Returns:?String?(i.e.?"Msxml2.DOMDocument.4.0")
    //
    //
    --------------------------------------------------------------------
    function ?GetXmlParserProgID()
    {
    ?
    var ?MAX_MAJOR_PARSER_VERSION? = ? 10 ;
    ?
    var ?MIN_MAJOR_PARSER_VERSION? = ? 0 ;
    ?
    var ?MAX_MINOR_PARSER_VERSION? = ? 9 ;
    ?
    var ?MIN_MINOR_PARSER_VERSION? = ? 0 ;
    ?
    ?
    var ?sProgID? = ?g_sXmlParserProgID;
    ?
    var ?bFound? = ? false ;

    ?
    if ?( ! sProgID)
    ?
    {
    ??
    // ?Iterate?through?possible?versions
    ???? for ?( var ?nMajor? = ?MAX_MAJOR_PARSER_VERSION;?nMajor? >= ?MIN_MAJOR_PARSER_VERSION;?nMajor -- )
    ??
    {
    ???
    for ?( var ?nMinor? = ?MAX_MINOR_PARSER_VERSION;?nMinor? >= ?MIN_MINOR_PARSER_VERSION;?nMinor -- )
    ???
    {
    ????
    // ?Set?up?the?classname?for?the?version?that?we're?trying?to?instantiate
    ????sProgID? = ? " Msxml2.DOMDocument. " ? + ?nMajor? + ? " . " ? + ?nMinor;

    ????
    try
    ????
    {?
    ?????????
    if ?( new ?ActiveXObject(sProgID))?
    ?????
    {
    ??????bFound?
    = ? true ;
    ??????
    break ;
    ?????}

    ????}

    ????
    catch (e)
    ????
    {
    ????}

    ???}


    ???
    if ?(bFound)
    ???
    {
    ????
    // ?store?in?a?global?variable?to?speedup?subsequent?calls
    ????g_sXmlParserProgID? = ?sProgID;
    ????
    break ;
    ???}

    ??}

    ?}

    ?
    ?
    return ?sProgID;
    }


    然后直接用其load方法(本地)。

    var ?xmlDoc? = ? new ?ActiveXObject( " MSXML2.DOMDocument.3.0 " );
    xmlDoc.load(local_XML_FileName);

    當(dāng)然也可以直接從server取來(用get方法即可),然后以responseText的方法

    xmlht.Open( " GET " ,server_XML_FileName, true );
    xmlht.onreadystatechange?
    = ?stateChange;
    xmlht.Send(
    null );
    ?
    function ?handleStateChange()? {
    ????
    if (xmlHttp.readyState? == ? 4 )? {
    ????????
    if (xmlHttp.status? == ? 200 )? {
    ?????????????xmlDoc.loadXML(xmlht.responseText);
    ????????}

    ????}

    }

    實(shí)際上xmlDoc.loadXML(xmlht.responseText)所得到的就是一個于內(nèi)存中的DOM了,而直接用responseXML的話就直接可以解析為一個DOM了!(注意load(FILE)與loadXML(DOM)是不同的)


    此時servert process :

    import ?java.io. * ;
    import ?javax.servlet. * ;
    import ?javax.servlet.http. * ;
    import ?javax.xml.parsers.DocumentBuilderFactory;
    import ?javax.xml.parsers.ParserConfigurationException;
    import ?org.w3c.dom.Document;
    import ?org.w3c.dom.NodeList;
    import ?org.xml.sax.SAXException;

    public ? class ?PostingXMLExample? extends ?HttpServlet? {
    ????
    ????
    protected ? void ?doPost(HttpServletRequest?request,?HttpServletResponse?response)
    ????
    throws ?ServletException,?IOException? {
    ????????
    ????????String?xml?
    = ?readXMLFromRequestBody(request);
    ????????Document?xmlDoc?
    = ? null ;
    ????????
    try ? {
    ????????????xmlDoc?
    = ?
    ????????????????????DocumentBuilderFactory.newInstance().newDocumentBuilder()
    ????????????????????.parse(
    new ?ByteArrayInputStream(xml.getBytes()));
    ????????}

    ????????
    catch (ParserConfigurationException?e)? {
    ????????????System.out.println(
    " ParserConfigurationException:? " ? + ?e);
    ????????}

    ????????
    catch (SAXException?e)? {
    ????????????System.out.println(
    " SAXException:? " ? + ?e);
    ????????}


    ????????
    /* ?Note?how?the?Java?implementation?of?the?W3C?DOM?has?the?same?methods
    ?????????*?as?the?JavaScript?implementation,?such?as?getElementsByTagName?and?
    ?????????*?getNodeValue.
    ?????????
    */

    ????????NodeList?selectedPetTypes?
    = ?xmlDoc.getElementsByTagName( " type " );
    ????????String?type?
    = ? null ;
    ????????String?responseText?
    = ? " Selected?Pets:? " ;
    ????????
    for ( int ?i? = ? 0 ;?i? < ?selectedPetTypes.getLength();?i ++ )? {
    ???????????type?
    = ?selectedPetTypes.item(i).getFirstChild().getNodeValue();
    ???????????responseText?
    = ?responseText? + ? " ? " ? + ?type;
    ????????}

    ????????
    ????????response.setContentType(
    " text/xml " );
    ????????response.getWriter().print(responseText);
    ????}

    ????
    ????
    private ?String?readXMLFromRequestBody(HttpServletRequest?request) {
    ????????StringBuffer?xml?
    = ? new ?StringBuffer();
    ????????String?line?
    = ? null ;
    ????????
    try ? {
    ????????????BufferedReader?reader?
    = ?request.getReader();
    ????????????
    while ((line? = ?reader.readLine())? != ? null )? {
    ????????????????xml.append(line);
    ????????????}

    ????????}

    ????????
    catch (Exception?e)? {
    ????????????System.out.println(
    " Error?reading?XML:? " ? + ?e.toString());
    ????????}

    ????????
    return ?xml.toString();
    ????}

    }


    DOM,JDOM,JAXP隨便你自己選好了!

    參考資料:foundations-of-ajax

    相關(guān)鏈接: Ajax---通過JSON與服務(wù)器通信(發(fā)送請求和處理響應(yīng))

    posted on 2006-03-29 15:04 Jkallen 閱讀(3940) 評論(0)  編輯  收藏 所屬分類: AJAX
    主站蜘蛛池模板: 四虎永久在线精品免费观看视频| 国产区在线免费观看| 中文字幕免费观看| 麻豆亚洲AV永久无码精品久久| 永久免费AV无码网站国产| 亚洲线精品一区二区三区| a级片免费在线播放| 久久免费视频99| 亚洲AV人无码综合在线观看 | 人人狠狠综合久久亚洲高清| 免费A级毛片无码久久版| 精品国产_亚洲人成在线高清| 国产精品无码免费专区午夜| 亚洲日韩小电影在线观看| 精品一卡2卡三卡4卡免费视频| 亚洲国产精品国自产拍AV| 在线观看免费av网站| 亚洲福利电影在线观看| 啦啦啦高清视频在线观看免费| 国产亚洲精品美女久久久| 日韩视频在线观看免费| 亚洲精品成人久久| 午夜成年女人毛片免费观看| 日韩亚洲翔田千里在线| AV在线播放日韩亚洲欧| 亚洲乱码中文字幕在线| 亚洲国产一区视频| 日本在线免费观看| 在线亚洲午夜片AV大片| 亚洲男人天堂2020| 久久A级毛片免费观看| 亚洲国产日韩视频观看| 亚洲精品NV久久久久久久久久| 久久久久国色av免费看| 亚洲一区二区三区成人网站 | 国产免费直播在线观看视频| 国产精品免费在线播放| 亚洲国色天香视频| 亚洲av片一区二区三区| 麻豆高清免费国产一区| 亚洲永久永久永久永久永久精品|