Ajax进行用户验证

前端之家收集整理的这篇文章主要介绍了Ajax进行用户验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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要写上全名

原文链接:https://www.f2er.com/ajax/161720.html

猜你在找的Ajax相关文章