Jsp+ajax to send a GET request _jsp programming

Source: Internet
Author: User

This article illustrates how Ajax sends get requests and then receives processing via JSP pages. Share to everyone for your reference. The implementation methods are as follows:

Ajax send GET request

Here with an example of Ajax send GET request, the instance specific requirements for a registration page, when the user filled out the user name, the input box after the loss of focus will be through Ajax to the background to send authentication information, if the user name is not admin to pass validation, otherwise not through validation.

First look at the JSP page specific information:

Copy Code code as follows:
<form action= "Servlet/loginservlet" method= "POST" >
<table>
<tr>
<td> User Account:</td>
<td><input type= "text" name= "username" onblur= "checkUser (This)"/></td>
</tr>
<tr>
<td> User Password:</td>
<td><input type= "password" name= "password"/></td>
</tr>
<tr>
<td><input type= "Submit" value= "registered"/></td>
<td><input type= "reset" value= "reset" ></td>
</tr>
</table>
</form>

The background processing information here is handled by a servlet

First look at the Web.xml configuration information

Copy Code code as follows:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>login. Loginservlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>

Then look at the Doget method of the specific servlet class

Copy Code code as follows:
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Response.setcontenttype ("Text/html;charaet=utf-8");
PrintWriter out = Response.getwriter ();
String Name=request.getparameter ("username");
SYSTEM.OUT.PRINTLN (name);
if (name.equals ("admin"))
Out.print (FALSE);
Else
Out.print (TRUE);
Out.flush ();
Out.close ();
}

Do a simple validation in the Servlet class.

In the JSP form, the event that loses focus is set on the input box for the input user name, which is the onblur event. Look at the JavaScript code below.

Copy Code code as follows:
<script type= "Text/javascript" >
Create XMLHttpRequest
function Createxmlhttprequest () {
if (window. XMLHttpRequest) {
return new XMLHttpRequest ();
}else{
return new ActiveXObject ("Microsoft.XMLHTTP");
}
}
Call this method when the user account input box loses focus
function CheckUser (obj) {
Gets the value entered in the input box
var user = Obj.value;
If the value in the input box is blank, the window prompts and the input box gets the focus
if (!user) {
Alert ("User name cannot be empty!") ");
Obj.focus ();
Return
}
When not empty, use AJAX requests to send information to the background to verify that the user name is available
Get request string
var url= "Servlet/loginservlet?username=" +user;
Call method Create XMLHttpRequest Object
XMLHttpRequest = Createxmlhttprequest ();
Set callback function
Xmlhttprequest.onreadystatechange=finish;
Initialize XMLHttpRequest
Xmlhttprequest.open ("Get", url,true);
Send Request
Xmlhttprequest.send (NULL);
}
callback function
function Finish () {
if (xmlhttprequest.readystate = = 4&& Xmlhttprequest.status = = 200) {
var result = Xmlhttprequest.responsetext;
if (result = = "true") {
Alert ("User name is available!") ");
}else{
Alert ("User name is not available!") ");
}
}
}
</script>

I hope this article will help you with the Jsp+ajax program design.

Related Article

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.