package com; 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(name = "emailServlet",urlPatterns = "/emailServlet") public class emailServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); this.doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); resp.setCharacterEncoding("utf-8"); String email = req.getParameter("email"); System.out.println(email); boolean flag = false; if(email.equals("1391614208@qq.com")){ resp.getWriter().print("true"); }else{ resp.getWriter().print("false"); } } }
<%-- Created by IntelliJ IDEA. User: SuSh1 Date: 2019/10/17 Time: 8:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <script src="js/jquery-1.12.4.min.js"></script> <% String contextPath = request.getContextPath(); request.setAttribute("contextPath",contextPath); %> <script type="text/javascript"> function getXMLhttp() { if (window.XMLHttpRequest){ return new XMLHttpRequest(); } else if (window.ActiveXObject){ return new ActiveXObject("Microsoft.XMLHTTP"); } } function kgc(){ var email = $("#email").val(); if(email == null || email == ""){ $("#emailId").html("邮箱不能为空"); }else{ xmlhttp = getXMLhttp(); var url = "${contextPath}/emailServlet"; var info = "email="+email; $(function () { $.ajax({ "url":url, "type":"POST", "data":info, "dataType":"text", "success":ifsuccess , "error":function () { alert("请联系系统管理员"); } }) }) function ifsuccess(data) { if(data=="true"){ //$("#emailId").html("邮箱不可用"); document.getElementById("emailId").innerText = "邮箱不可用"; //$("#emailId").html("邮箱不可用"); }else{ $("#emailId").html("邮箱可用"); } } } } </script> <title>验证email</title> </head> <body> <form action="" method="post"> 邮箱:<input type="email" name="email" id="email" placeholder="请输入有效邮箱:" onblur=" kgc();"> <input type="submit" value="提交"> <div id="emailId" style="display: inline"></div> </form> </body> </html>