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">
            <td> User account number: </td>
            <td><input type="text" name="username" onblur="checkUser(this)"/></td>
            <td> User password: </td>
            <td><input type="password" name="password"/></td>
            <td><input type="submit" value=" Registration "/></td>
            <td><input type="reset" value=" Reset "></td>

Here, the background processing information is processed by Servlet

First look at web. xml configuration information

</servlet> <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");

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(){
            return new XMLHttpRequest();
            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
            alert(" User name cannot be empty! ");
        // 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
        // Initialization xmlhttprequest"GET",url,true);
        // Send a request
    // Callback function
    function finish(){
        if(XmlHttpRequest.readyState == 4&& XmlHttpRequest.status == 200){
            var result = XmlHttpRequest.responseText;
            if(result =="true"){
                alert(" User name available! ");
                alert(" User name is not available! ");

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

Related articles: