js調(diào)用后臺servlet方法實(shí)例
1.使用document.form.action方式
相關(guān)源碼如下:
*.js
[javascript]
<span style="white-space:pre"> </span> document.getElementById("sendPerson").value = SendPerson;
document.getElementById("currentTime").value = currentTime();
document.getElementById("message").value = message;
document.getElementById("recvPerson").value = recvPerson;
document.chatform.action = "ToHistoryServlet";
document.chatform.submit();
*.html
[html]
<!--the tag below is the params to the userHistory dml@2012.8.27-->
<input type="hidden" name="sendPerson" id="sendPerson"> <input
type="hidden" name="currentTime" id="currentTime"> <input
type="hidden" name="message" id="message"> <input
type="hidden" name="recvPerson" id="recvPerson">
注意的是,input需指定name屬性,這么servlet才可以獲取到參數(shù)值
*.java
[java]
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { chabaoo.cn
String sendPerson = request.getParameter("sendPerson");
String recvPerson = request.getParameter("recvPerson");
String sendTime = request.getParameter("currentTime");
String message = request.getParameter("message");
Message msg = new Message();
msg.setMessage(message);
msg.setRecvPerson(recvPerson);
msg.setSendPerson(sendPerson);
msg.setSendTime(sendTime);
HistoryHandle.addMessage(msg);
}
這個(gè)缺點(diǎn)是頁面就跳走了,要是希望保持原頁面,可以參照方法2
2.jquery調(diào)用后臺方法
[javascript]
$.ajax({
type : "POST",
contentType : "application/json",
url : "ToHistoryServlet?sendPerson=" + SendPerson + "¤tTime="
+ currentTime() + "&message=" + message + "&recvPerson="
+ recvPerson,
dataType : 'json',
success : function(result) {
alert(result.d);
}
});
代碼量小,使用方便,值得推薦。。。
- Servlet+Ajax實(shí)現(xiàn)智能搜索框智能提示功能
- Servlet 與 Ajax 交互一直報(bào)status=parsererror的解決辦法
- Ajax+Servlet+jsp顯示搜索效果
- jQuery Ajax傳值到Servlet出現(xiàn)亂碼問題的解決方法
- 關(guān)于Ajax技術(shù)中servlet末尾的輸出流
- Jquery+ajax+JAVA(servlet)實(shí)現(xiàn)下拉菜單異步取值
- 探討.get .post .ajax ztree 還有后臺servlet傳遞數(shù)據(jù)的相關(guān)知識
- 實(shí)例解讀Ajax與servlet交互的方法
- 深入Ajax代理的Java Servlet的實(shí)現(xiàn)詳解
- JS請求servlet功能示例
相關(guān)文章
談?wù)別ncodeURI和encodeURIComponent以及escape的區(qū)別與應(yīng)用
encodeURI和encodeURIComponent以及escape,這三個(gè)都是用來編碼的,本篇文章給大家介紹encodeURI和encodeURIComponent以及escape的區(qū)別與應(yīng)用,感興趣的朋友一起學(xué)習(xí)吧2015-11-11JavaScript實(shí)現(xiàn)圖片DIV豎向滑動(dòng)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)圖片DIV豎向滑動(dòng)的方法,涉及javascript操作div層的相關(guān)技巧,需要的朋友可以參考下2015-04-04關(guān)于flash遮蓋div浮動(dòng)層的解決方法
關(guān)于flash遮蓋div浮動(dòng)層2010-07-07微信小程序?qū)崿F(xiàn)分類菜單激活狀態(tài)隨列表滾動(dòng)而自動(dòng)切換效果詳解
這篇文章主要介紹了微信小程序分類菜單激活狀態(tài)跟隨列表滾動(dòng)自動(dòng)切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01javascript實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02js實(shí)現(xiàn)中文實(shí)時(shí)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)中文實(shí)時(shí)時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01