加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax进行用户验证

发布时间:2020-12-16 03:36:50 所属栏目:百科 来源:网络整理
导读:index: !DOCTYPE htmlhtml lang="en"head meta charset="UTF-8" titleTitle/title script type="text/javascript" src="lib/jquery-3.1.1.js"/script script type="text/javascript" src="lib/tmp.js"/script/headbody input type="text" id="name" onmouSEO

index:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="lib/jquery-3.1.1.js"></script>
    <script type="text/javascript" src="lib/tmp.js"></script>
</head>

<body>
    <input type="text" id="name" onmouSEOut="solve()"/>


    <div id="result">
    </div>
</body>
</html>

tmp.js:


/*
$(function(){
    $('#submit').click(function () {
        console.log($("#name").val());
    });
});*/


function solve(){
    var obj1 = $("#name").val();
    $.get("AjaxServlet?name="+obj1,null,callback);
}

function callback(data){
    var req = $("#result");
    req.html(data);
}


AjaxServlet:


package com.dqd;

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;
import java.io.PrintWriter;

/**
 * Created by Dqd on 2016/11/1.
 */
@WebServlet(name = "AjaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request,IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String name=request.getParameter("name");

       if(name!=null){
           if(name.equals("dqd")){
               out.println("填写正确");
           }else{
               out.println("填写不正确");
           }
       }else
           out.println("用户不为空");
        //out.print("<a href='index.html'>返回登录页面</a>");
    }
}



还有配置web.xml文件注意name-class要写上全名

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读