Method of jsp+ajax Sending GET Request

  • 2021-07-01 08:04:04
  • OfStack

This article describes the example of ajax sent GET request, and then through the jsp page to receive processing. Share it for your reference. The specific implementation method is as follows:

Ajax sends an GET request

Here, an example is used to demonstrate that Ajax sends get request. The specific requirement of the example is a registration page. When the user fills in the user name, the input box will send verification information to the background through Ajax after losing focus. If the user name is not admin, it will pass the verification, otherwise it will not pass the verification.

Let's look at the specific information of JSP page first:

<form action="servlet/LoginServlet" method="post">
    <table>
        <tr>
            <td> User account number: </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=" Registration "/></td>
            <td><input type="reset" value=" Reset "></td>
        </tr>
    </table>
</form>

Here, the background processing information is processed by Servlet

First look at web. xml configuration information

<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

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 form of JSP, the out-of-focus event is set for the input box where the user name is entered, that is, the onblur event. Let's look at the javascript code.

<script type="text/javascript">
    // Create XMLHttpRequest
    function createXmlHttpRequest(){
        if(window.XMLHttpRequest){
            return new XMLHttpRequest();
        }else{
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    // This method is called 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 empty, the pop-up window prompts and lets the input box get focus
        if(!user){
            alert(" User name cannot be empty! ");
            obj.focus();
            return;
        }
        // When it is not empty, use the Ajax Request to send information to the background to verify that the user name is available
        //get Request string
        var url="servlet/LoginServlet?username="+user;
        // Call the method to create XMLHttpRequest Object
        XmlHttpRequest = createXmlHttpRequest();
        // Set callback function
        XmlHttpRequest.onreadystatechange=finish;
        // Initialization xmlhttprequest
        XmlHttpRequest.open("GET",url,true);
        // Send a 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 available! ");
            }else{
                alert(" User name is not available! ");
            }
        }
    }
</script>

I hope this article is helpful to everyone's jsp + Ajax programming.


Related articles: