jQuery is a popular js library nowadays, which can produce ideal effects with simple code, just like what the official website says "write less, do more". Jquery has rewritten the previous way of writing JavaScript to a certain extent. I use jquery to achieve the effect of using ajax to achieve asynchronous communication in the previous article, and feel the charm of jquery.
First you need to download the latest js file of jquery and introduce it into the file. You can also download it here: Click me to download.
This communication uses jquery’s jQuery.post(url,[data], [callback],[type]) method, this Is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request is successful. The parameters are: url, [data], [callback], [type] and the corresponding parameter types are String, Map, Function, String:
●url: Send request address.
●data: Key/value parameters to be sent.
●callback: Callback function when sending successfully.
●type: Return content format, xml, html, script, json, text, _default)
Create a new jsp file jqueryDemo.jsp, the code is as follows:
Create a new servlet file JqueryServlet.java, the code is as follows:
好了,现在可以运行了,打开服务器,运行此jsp文件,页面如下所示:
当输入admin时,页面如下所示:
当输入其他的字符时,页面如下所示:
可以看出jquery能够实现ajax的功能,并且代码更简洁了。
只是,最后本人有一个问题迟迟没有解决,那就是输入中文时传到后台的值乱码,按照网上的好多办法都没有解决掉,不知道为什么,谁有更好的方法希望能给我推荐一下,本人不胜感激。
这是本人学习的结果,允许转载,欢迎交流,但转载务必给出本文章的链接地址