Jsp + ajax Method for sending GET requests

Source: Internet
Author: User

Jsp + ajax Method for sending GET requests

The example in this article describes how ajax sends a GET request and then receives the processing through the jsp page. Share it with you for your reference. The specific implementation method is as follows:

Ajax sends a GET request

Here, we use an instance to demonstrate how Ajax sends a get request. The instance must be a registration page. When the user fills in the user name, the input box will send verification information to the background through Ajax if the focus is lost, if the user name is not admin, it is verified; otherwise, it is not verified.

The following describes the details of the JSP page:

The Code is 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 = "register"/> </td>
<Td> <input type = "reset" value = "reset"> </td>
</Tr>
</Table>
</Form>

 

The backend processing information is processed using Servlet.

First, check the web. xml configuration information.

The Code is 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, let's look at the specific servlet class's doGet method.

The Code is 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 ();
}

 

Perform a simple verification in the Servlet class.

In the JSP form, a focus loss event is set for the input box of the user name, that is, the onblur event. The following describes the javascript code.

The Code is 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 ){
// Obtain the value entered in the input box
Var user = obj. value;
// If the value in the input box is empty, a prompt is displayed, and the focus of the input box is obtained.
If (! User ){
Alert ("the user name cannot be blank! ");
Obj. focus ();
Return;
}
// If it is not empty, use Ajax requests to send information to the background to verify whether the user name is available
// Get request string
Var url = "servlet/LoginServlet? Username = "+ user;
// Call a method to create an XMLHttpRequest object
XmlHttpRequest = createXmlHttpRequest ();
// Set the callback function
XmlHttpRequest. onreadystatechange = finish;
// Initialize xmlhttprequest
XmlHttpRequest. open ("GET", url, true );
// Send the request
XmlHttpRequest. send (null );
}
// Callback function
Function finish (){
If (XmlHttpRequest. readyState = 4 & XmlHttpRequest. status = 200 ){
Var result = XmlHttpRequest. responseText;
If (result = "true "){
Alert ("the user name is available! ");
} Else {
Alert ("the user name is unavailable! ");
}
}
}
</Script>

 

I hope this article will help you with jsp + Ajax programming.

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.