1.首先在index.jsp中写上
<a href="${pageContext.request.contextPath }/demo1.jsp">案例1-检查用户名是否占用</a><br>
2.新建demo11.jsp
在body中写上
<body>
<form action="#" method="get">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" onblur="checkUsername(this)"></td>
<td><span id="username_msg"></span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password"></td>
<td></td>
</tr>
<tr>
<td colspan="2"><input type="submit" id="sub"></td>
</tr>
</table>
</form>
</body>
2.1继续写上
<script type="text/javascript">
function checkUsername(obj){
//创建核心对象
xmlhttp=null;
if(window.XMLHttpRequest){
//code for ie7 ,firefox, opera ,etc.
xmlhttp=new XMLHttpRequest();
}
else if(window.ActiveXObjeect){
//code for ie6,ie5
xmlhttp=new ActiveXObjext("Microsoft.XMLHTTP");
}
//编写回调函数
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
if(xmlhttp.responseText == 1){
document.getElementById("username_msg").innerHTML="<font color='green'>用户名可以使用</font>";
document.getElementById("sub").disabled=false;
}else{
document.getElementById("username_msg").innerHTML="<font color='red'>用户名已被占用</font>";
document.getElementById("sub").disabled=true;
}
}
}
//执行open操作
xmlhttp.open("get", "${pageContext.request.contextPath}/checkUsername4Ajax?username="+obj.value);
//send操作
xmlhttp.send();
}
</script>
3.创建servlet,包名自己随便顶一下,servlet名明为:CheckUsername4AjaxServlet mapping为:checkUsername4Ajax 如下
<servlet-mapping>
<servlet-name>CheckUsername4AjaxServlet</servlet-name>
<url-pattern>/checkUsername4Ajax</url-pattern>
</servlet-mapping>
4. 在 CheckUsername4AjaxServlet 中的doget方法中写入
//0.设置编码
//1.接受用户
String username=request.getParameter("username");
String password=request.getParameter("password");
username=new String(username.getBytes("iso8859-1"),"utf-8");
System.out.println(username);
//2.调用service 完成查询 返回user
User user = null;
try {
user = new UserService().checkUsername4Ajax(username);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//3.判断user是否为空
if(user == null){
response.getWriter().println("1");
}else{
response.getWriter().println("0");
}
}
5.定义一个UserService.java类,包名自己随意定,写上checkUsername4Ajax(username)的方法,如下
public class UserService {
/**
* 检测用户名是否被占用
* @param username
* @return
* @throws SQLException
*/
public User checkUsername4Ajax(String username) throws SQLException {
// TODO 自动生成的方法存根
return new UserDao().getUserByUsername4Ajax(username);
}
}
6.定义一个Userdao.java类,包名自己随意定,写上getUserByUsername4Ajax(username)的方法,如下
public class UserDao {
/**
* 通过用户名获取一个用户
* @param username
* @return
* @throws SQLException
*/
public User getUserByUsername4Ajax(String username) throws SQLException {
// TODO 自动生成的方法存根
QueryRunner qr= new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from user where username=?";
return qr.query(sql, new BeanHandler<>(User.class), username);
}
}
7.定义一个User,java类,包名随便定
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
8.链接数据库的文件dataSourceUtils.java(以文件方式上传)
9.c3p0-config.xml(文件上传)
附件:
1.c3p0-config.xml里面的内容:
<?xml version="1.0" encoding='UTF-8'?>
<c3p0-config>
<!-- 默认配置,如果没有指定则使用这个配置 -->
<default-config>
<!-- 基本配置 -->
<!-- <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
<property name="jdbcUrl">jdbc:oracle:thin:@502Share-pc:1521:orcl</property>
<property name="user">interbs_16</property>
<property name="password">interbs_16</property> -->
<!-- <property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://rm-uf63dht8ci6vxa03lo.mysql.rds.aliyuncs.com:3306/interbs_16</property>
<property name="user">root</property>
<property name="password">WWWqq2_09</property>
-->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://502Sharepc:3306/day1501</property>
<property name="user">root</property>
<property name="password">123456</property>
<!--扩展配置-->
<property name="checkoutTimeout">30000</property>
<property name="idleConnectionTestPeriod">30</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- 命名的配置 -->
<named-config name="itcast">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/xxxx</property>
<property name="user">root</property>
<property name="password">1234</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">20</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">40</property>
<property name="maxStatements">20</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>