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
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"> <inputtype = "hidden" name =" currenttime "id =" currenttime "> <inputtype =" hidden "name =" message "id =" message "> <inputtype =" 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
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 );}
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
$. Ajax ({type: "Post", contenttype: "application/JSON", URL: "tohistoryservlet? Sendperson = "+ sendperson +" effecttime = "+ currenttime () +" & message = "+ message +" & recvperson = "+ recvperson, datatype: 'json', success: function (result) {alert (result. d );}});
CodeSmall amount, easy to use, it is recommended...
If you need to use parameters in the callback function, refer
JS returns parameters in the background method.