Today, the history query function encountered the problem of calling methods in the front and back of each other. I studied and recorded the successful experiences one or two. 1. Use document. form. action
The source code is as follows:
*. Js
[Javascript]
The Code is as follows:
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 Code is as follows:
Type = "hidden" name = "currentTime" id = "currentTime">Type = "hidden" name = "message" id = "message">Type = "hidden" name = "recvPerson" id = "recvPerson">
Note that the name attribute must be specified for input so that the servlet can obtain the parameter value.
*. Java
[Java]
The Code is as follows:
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {www.jb51.net
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 );
}
This disadvantage is that the page jumps away. If you want to keep the original page, refer to method 2.
2. jquery calls the background Method
[Javascript]
The Code is as follows:
$. Ajax ({
Type: "POST ",
ContentType: "application/json ",
Url: "ToHistoryServlet? SendPerson = "+ SendPerson +" receivttime ="
+ CurrentTime () + "& message =" + message + "& recvPerson ="
+ RecvPerson,
DataType: 'json ',
Success: function (result ){
Alert (result. d );
}
});
Small amount of code, easy to use, it is recommended...