Java 調用 php介面(Ajax)

來源:互聯網
上載者:User

由於項目裡面需要用到Java調用PHP的儲值介面,所以學習了一下,以下這個Demo是個小小的例子,寫下來做個筆記>

 

jsp頁面:

 

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"
 contentType="text/html; charset=GBK"%>

<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <base href="<%=basePath%>" />
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <meta http-equiv="x-ua-compatible" content="ie=7" />

  <title>Lost Crown</title>
  <link rel="icon" href="favicon.ico" />
  <link rel="shortcut icon" href="favicon.ico" />
<script type="text/javascript">
function getXmlHttp(){
    var http_request = false;
    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType('text/xml');
 }
   }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e) {
            }
        }
    }
    if (!http_request){
        alert("不能建立對象!");
        return false;
    }
    return http_request;
}

function result(){
    if(req.readyState == 4 && req.status == 200){
     var names =  req.responseText;
     alert("Hello:"+names);
    }    
}

function checkTest(){
 var name = document.getElementById("name").value;
    var ul = "http://localhost:8011/TestAjax/index.php?name=" + name;
    req = getXmlHttp();
    req.onreadystatechange = result;
    req.open("POST",ul,true);
    req.setRequestHeader('If-Modified-Since', '0');
    req.send(null);
}
</script>
</head>
 <body> 
  <center>
   <table width="300" border=1>
   <tr align="center">
    <td colspan="2">
     It's A Ajax Test
    </td>
   </tr>
   <tr align="center">
    <td>
     使用者名稱
    </td>
    <td>
     <input type="text" name="name" id="name" value="Jerry"/>
    </td>
   </tr>
   <tr align="center">
    <td colspan="2">
     <input type="button" name="submit" id="submit" value="提交" onclick="checkTest();"/>
    </td>
   </tr>
  </table>
  </center>
 </body>
</html>

php頁面:

<?php

$username = $_REQUEST['name'];

echo($username);

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.