日期:2014-05-16 浏览次数:20283 次
今天做历史记录查询功能,遇到了前后台方法互相调用的问题,研究了一下,把成功经验记录一二。
1.使用document.form.action方式
相关源码如下:
*.js
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
<!--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">
*.java
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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.jquery调用后台方法
$.ajax({ type : "POST", contentType : "application/json", url : "ToHistoryServlet?sendPerson=" + SendPerson + "¤tTime=" + currentTime() + "&message=" + message + "&recvPerson=" + recvPerson, dataType : 'json', success : function(result) { alert(result.d); } });