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