文档说明:(基于jquery-mysql的ajax请求用户名是否存在) 新手上路——记录点滴
项目目录:
wang.dbutil由高手封装的,借用
整体项目,如链接过期,请关注留言,单独发送
链接:https://pan.baidu.com/s/1x0lDCoTrHvIuPdpYXel4fg
密码:hlhm
以下是jsp及servlet页面:
<%@ 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
*/
("/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);
}
}