精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

Ajax實現(xiàn)登錄案例

ajax登錄案例,供大家參考,具體內(nèi)容如下

msg

package com.lbl.msg;

public class msg {
  string msg;
  int code;

  public msg() {
  }

  public msg(string msg, int code) {
    this.msg = msg;
    this.code = code;
  }

  public string getmsg() {
    return msg;
  }

  public void setmsg(string msg) {
    this.msg = msg;
  }

  public int getcode() {
    return code;
  }

  public void setcode(int code) {
    this.code = code;
  }
}

registerservlet

package com.lbl.servlet;

import com.fasterxml.jackson.databind.objectmapper;
import com.lbl.msg.msg;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;

@webservlet("/register")
public class registerservlet extends httpservlet {
  protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    doget(request, response);
  }

  protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
    //1:獲取請求發(fā)送的數(shù)據(jù)
    string username = request.getparameter("username");
    response.setcontenttype("text/html;charset=utf-8");
    //2:判斷用戶名是否注冊
    if("abc123".equals(username)){
      msg msg = new msg("用戶名已經(jīng)注冊",0);
      //用戶名已經(jīng)注冊 {"flag":false,"info":"用戶名已經(jīng)注冊"};
//      string jsonstr = "{\"flag\":false,\"info\":\"用戶名已經(jīng)注冊\"}";
      //響應回瀏覽器
      response.getwriter().write(new objectmapper().writevalueasstring(msg));
    }else{
      msg msg = new msg("可以注冊",1);
      //用戶名沒有注冊
//      string jsonstr = "{\"flag\":true,\"info\":\"可以注冊\"}";
      //響應回瀏覽器
      response.getwriter().write(new objectmapper().writevalueasstring(msg));
    }

  }
}

json_register.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>title</title>
  <script src="js/jquery-3.3.1.js"></script>
  <script type="application/javascript">
    $(function () {
      //1: 給用戶名輸入框綁定失去焦點事件(onfocus onblur)
      $("#username").on("blur",function () {
        //獲取用戶名輸入框數(shù)據(jù)
        var username = $("#username").val();
        //2:向服務器發(fā)出異步請求,讓服務器去查詢用戶名是否存在
        $.post(
          "register", //表示服務器的servlet路徑
          "username="+username, //表示向服務器發(fā)送的數(shù)據(jù)
          function (data) { // msg:用戶名已經(jīng)注冊 code:0
            if(data.code==0){
              // alert(data.info);
              $("#spanid").html(data.msg).css("color","green");
            }else {
              $("#spanid").html(data.msg).css("color","red");
            }

          },
          "json"
        );
      });
    });

  </script>
</head>
<body>
<div>
  <font>會員注冊</font>user register
  <form class="form-horizontal" style="margin-top: 5px;">
    <table>
      <tr>
        <td>用戶名</td>
        <td>
          <input type="text" id="username" name="username" placeholder="請輸入用戶名">
          <span id="spanid">
        </td>
      </tr>
      <tr>
        <td>密碼</td>
        <td>
          <input type="password" placeholder="請輸入密碼">
        </td>
      </tr>
    </table>
    <input type="submit" value="注冊"/>
  </form>
</div>

</body>
</html>

運行效果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持碩編程。

相關(guān)文章