導航:首頁 > 編程語言 > ajax調用java後台方法

ajax調用java後台方法

發布時間:2024-08-29 07:23:58

⑴ 後台的java action類要怎麼傳值給ajax頁面

在action類中用out.print()輸出值。
到ajax頁面直接可以獲取值。

⑵ java如何用Ajax獲取後台List的值,在頁面如何用JS循環接收麻煩大俠們講講!

首先要用JSONArray ja = JSONArray.fromObject(list);封裝,然後PrintWriterout=response.getWriter();
out.print(ja);
out.flush();
out.close();寫出
這時候在js裡面var text = xmlHttp.responseText; //字元串(json格式的)
var obj = JSON.parse(text);
var array = obj.list; //數組[存放若干個js對象
for(var i=0;i<array.length;i++){
var e = array[i];
var id = e.Id;
var name = e.name;
}

⑶ ajax和java的結合使用:看了很多例子都是前台通過ajax發送請求,後台處理後返回的都是一個字元串變數

$.post("/myAjax/LoginServlet",{status:"checkCype",ctype:ctype},function(data) {
//循環數組
$(data.rows).each(function (i, n) {
//i是數組的下標,n是數組元素

}

⑷ jquery 調用 Java 後台方法

jquery可以通過ajax調用後台。

ajax請包含
$.ajax(url,[settings])
load(url,[data],[callback])
$.get(url,[data],[fn],[type])
$.getJSON(url,[data],[fn])
$.getScript(url,[callback])
$.post(url,[data],[fn],[type])
這些種
具體使用方法可以參照jquery的api
jquery 在線 api http://jquery.cuishifeng.cn/

⑸ ajax調用java後台的一個方法

ajax調用java後台的方法,其實是通過url鏈接來訪問,示例如下:

packagecom.xxxx.xxxx.servlet;

importjava.io.IOException;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;

importjavax.naming.Context;
importjavax.naming.InitialContext;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.sql.DataSource;

{
=1L;
privatestaticConnectionconn=null;
=null;

publicoaLoginLimitedServlet(){
super();
}

publicvoiddestroy(){
super.destroy();
}


publicstaticStringgetCount(Stringuserid)
{
Stringv_sql=".....";
Stringv_count="";

try{
pstmt=conn.prepareStatement(v_sql);
pstmt.setString(1,userid);
ResultSetrs=pstmt.executeQuery();
while(rs.next()){
v_count=rs.getString(1);
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}finally{
try{
pstmt.close();
conn.close();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returnv_count;
}

(){
Contextctx=null;
try{
ctx=newInitialContext();
DataSourceds=(DataSource)ctx.lookup("jndiname");
conn=ds.getConnection();
}catch(Exceptione){
e.printStackTrace();
}
returnconn;
}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringv_userid=request.getParameter("userid");
System.out.println(v_userid);
getConnection();
Stringv_count=getCount(v_userid);
response.setCharacterEncoding("UTF-8");
response.getWriter().write(v_count);
response.flushBuffer();
}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}


}

如果要前端能夠訪問到該servlet,需要將該servlet注冊到web.xml文件中。需要在web.xml文件中添加以下內容
[html]viewplain
<servlet>
<servlet-name>oaLoginLimitedServlet</servlet-name>
<servlet-class>com.xxxx.xxxx.servlet.oaLoginLimitedServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>oaLoginLimitedServlet</servlet-name>
<url-pattern>/oaLoginLimitedServlet</url-pattern>
</servlet-mapping>

重啟相關服務。
通過ajax就可以調用了。

[html]viewplain
varmsg=$.ajax({
type:"post",
url:....+'/oaLoginLimitedServlet?userid='+$('#act').val(),
async:false
}).responseText;
閱讀全文

與ajax調用java後台方法相關的資料

熱點內容
黑月編譯器優缺點 瀏覽:683
線刷包為啥要解壓 瀏覽:147
伺服器滑道怎麼安裝 瀏覽:698
手游發貨伺服器是什麼 瀏覽:709
文件夾的各個名稱 瀏覽:722
安卓系統哪個文件夾存放密碼文件 瀏覽:361
傳真伺服器什麼意思 瀏覽:249
stm3251單片機 瀏覽:279
怎麼做程序員男友 瀏覽:826
信道編解碼作用 瀏覽:838
程序員零基礎學習 瀏覽:201
網卡加密視頻 瀏覽:628
命令按鈕的代碼 瀏覽:853
androidjson編碼 瀏覽:500
筆記本rpc伺服器不可用怎麼進入桌面 瀏覽:946
哪個app開發好 瀏覽:266
java封裝與調用方法 瀏覽:288
自學編程找不到工作 瀏覽:356
招行app余額在哪裡 瀏覽:832
編譯target 瀏覽:910