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 [email protected]> <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 { www.2cto.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); } });