Posted in Javascript onJune 09, 2013
1.使用document.form.action方式
相关源码如下:
*.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才可以获取到参数值
*.java
[java]
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3water.com 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); }
这个缺点是页面就跳走了,要是希望保持原页面,可以参照方法2
2.jquery调用后台方法
[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); } });
代码量小,使用方便,值得推荐。。。
js调用后台servlet方法实例
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@