文檔說明:(基於jquery-mysql的ajax請求用戶名是否存在) 新手上路——記錄點滴javascript
項目目錄:css
wang.dbutil由高手封裝的,借用html
總體項目,如連接過時,請關注留言,單獨發送java
連接:https://pan.baidu.com/s/1x0lDCoTrHvIuPdpYXel4fg 密碼:hlhmmysql
如下是jsp及servlet頁面:jquery
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript"> $(function() { var name = $("#username").val(); $("#username").blur(function() { //alert(name); $.ajax({ type : "post", url : "/ajaxpro1/LoginServlet", data : { username : $("#username").val(), //username:name }, success : function(result) { if(result=="true"){$("#spid").css("display","block")} if(result=="false"){$("#spid").css("display","none")} }, error : function() { alert("請刷新頁面 :)"); } }); }); }); </script> </head> <body> <form action="#"> user:<input type="text" id=username name="username" value="" /><span id="spid">已存在</span> <input type="button" value="submit"> </form> <style> #spid{ display:none; text_color=red; } </style> </body> </html>
package wang.servlet; import java.io.IOException; 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 wang.service.LoginService; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); String name = request.getParameter("username"); System.out.println(name+"loginServlet接收的數據!!!!!!"); LoginService loginService = new LoginService(); try { boolean exist = loginService.getUserName(name); System.out.println(exist+"servlet返回的數據"); //request.getSession().setAttribute("exit", exist); //request.setAttribute("result", exist); //request.getRequestDispatcher("/login.jsp").forward(request, response); String exist1 = new Boolean(exist).toString(); System.out.println(exist1+"String型!!!!!!!111"); response.getWriter().write(exist1); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }