Ajax Effect Based on jquery + Servlet

Source: Internet
Author: User

Here we will implement a login Effect Using ajax asynchronous implementation in the future. Let's just look at the Code:

Logon page:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Jquery code for Ajax implementation:

Function verify () {// alert (""); // method 1 to solve the Chinese Garbled text problem. encodeuri is performed on the data sent from the page, the server uses new string (old. getbytes ("iso8859-1"), "UTF-8") // var url = "ajaxserver? Name = "+ encodeuri ($ (" # username "). val (); // encodeuri handles Chinese Garbled text; // method 2 to solve Chinese Garbled text. the data sent by the page is processed by encodeuri twice, and the server uses urldecoder. decode (old, "UTF-8"); var input = $ ("input [name = 'type']"); // The value selected by the single button cannot be obtained, so we can only get all the values first, and judge for (I = 0; I <input. length; I ++) {If (input [I]. checked) {// alert (input [I]. value); var url = "users? Name = "+ encodeuri ($ (" # username "). val () + "& Password =" + encodeuri ($ ("# userpassword "). val () + "& type =" + encodeuri (input [I]. value); // encodeuri for handling Chinese garbled characters} url = converturl (URL); // obtain the function return value 'login? Uname = '+ uname +' & psw = '+ psw $. get (URL, null, function (data) {$ ("# result1" ).html (data); // simplified version}); // alert (URL );} // Add a timestamp to the URL and cheat the browser. The function converturl (URL) {// obtain the timestamp var timstamp = (new date ()). valueof (); // splice the timestamp information to the URL if (URL. indexof ("? ")> = 0) {// use indexof to determine whether the URL address has a question mark (url = URL +" & t = "+ timstamp;} else {url = URL + "? T = "+ timstamp;} return URL ;}

The processing code of the backend servlet:

Package COM. xidian. BBS. servlet; import Java. io. ioexception; import Java. io. printwriter; import java.net. urldecoder; import Java. SQL. connection; import Java. SQL. resultset; import Java. SQL. statement; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. servlet. HTTP. httpsession; impor T COM. xidian. BBS. util. dbaccess; @ suppresswarnings ("serial") public class users extends httpservlet {public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); Request. setcharacterencoding ("UTF-8"); response. setcharacterencoding ("UTF-8"); printwriter out = response. getwriter (); // 1. take the parameter S passed in JS Tring name1 = request. getparameter ("name"); string password1 = request. getparameter ("password"); string type1 = request. getparameter ("type"); // string name = new string (old. getbytes ("iso8859-1"), "UTF-8"); // process Chinese garbled 1, need to match the encodeuri in the front-end JS file with string name2 = urldecoder. decode (name1, "UTF-8"); // process Chinese Garbled text 2 string password2 = urldecoder. decode (password1, "UTF-8"); string type2 = urldecoder. decode (type1, "UTF-8"); If (Na Me2 = NULL | name2.length () = 0) {out. println ("the user name cannot be blank !! "); Return; // do not execute the following code} else if (password2 = NULL | password2.length () = 0) {out. println ("the password cannot be blank !! "); Return;} // 3. check statement SQL = NULL; resultset rs = NULL; try {connection con = dbaccess. getconnection (); SQL = con. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable); If (type2.equals ("consortium member") {httpsession session = request. getsession (); Session. setattribute ("bs_type", "consortium member"); RS = SQL .exe cutequery ("select * From yh_yhxx"); While (RS. next () {string yhxx_id = Rs. gets Tring ("yhxx_id"); string yhxx_pwd = Rs. getstring ("yhxx_psw"); system. out. println (yhxx_id + "" + yhxx_pwd); // 2. check whether the parameter is correct. // 3. verify if (name2.equals (yhxx_id) & password2.equals (yhxx_pwd) {// 4. unlike traditional applications, this step returns data of interest to the page end. Instead of returning a new page to the page, the writing method has not changed, and the session has changed in essence. setattribute ("username", name2); out. println ("<script language = 'javascript '> window. location. href = 'index. JSP '; </SCRIPT> "); out. flush (); out. close () ;}} else {httpsession session = request. getsession (); Session. setattribute ("bs_type", "Forum member"); RS = SQL .exe cutequery ("select * from users"); While (RS. next () {If (name2.equals (RS. getstring ("userid") & password2.equa Ls (RS. getstring ("upwd") & RS. getint ("if_forbid") = 0) {session. setattribute ("username", name2); out. println ("<script language = 'javascript '> window. location. href = 'index. JSP '; </SCRIPT> "); out. flush (); out. close ();} else if (name2.equals (RS. getstring ("userid") & password2.equals (RS. getstring ("upwd") & RS. getint ("if_forbid") = 1) {out. println ("this user has been banned !! "); Out. Flush (); Out. Close () ;}} out. println (" incorrect user name or password !! ");} Catch (exception e) {e. printstacktrace () ;}} public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {doget (request, response );}}

 

 

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.