Javascript call backend servlet method instance_javascript tips-js tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.