jsp页面
Reg.jsp
<%@ 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="js/jquery1.11.3.min.js"></script>
<script src="js/reg.js"></script>
</head>
<body>
<form action="TestRegServlet" method="post">
<pre>
用户名:<input type='text' name='uname' /><span id="s1"></span>
密码:<input type='password' name='pwd' />
确认密码:<input type='password' name='repwd' />
<input type='submit' name='sub' value='注册' />
</pre>
</form>
</body>
</html>
js代码
reg.js
$(function(){
$(":text[name='uname']").blur(function(){
var uname=$(this).val();
$.get("CheckUnameServlet?uname="+uname,function(data){
if(data=="1")
$("#s1").html("该用户已存在");
else
$("#s1").html("可以注册");
})
})
})
后台代码
CheckUnameServlet
package com.yyy.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 com.test.util.DbHelper;
@WebServlet("/CheckUnameServlet")
public class CheckUnameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public CheckUnameServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String uname=request.getParameter("uname");
String sql="select * from user where uname=?";
List<Object> paramList=new ArrayList<Object>();
paramList.add(uname);
DbHelper dbHelper=new DbHelper();
List<Map<String, Object>> list= dbHelper.executeQuery(sql, paramList);
if(list!=null && list.size()>0)
response.getWriter().print("1");
else
response.getWriter().print("0");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
TestRegServlet
package com.yyy.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 com.test.util.DbHelper;
@WebServlet("/TestRegServlet")
public class TestRegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public TestRegServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String uname=request.getParameter("uname");
String pwd=request.getParameter("pwd");
DbHelper dbHelper=new DbHelper();
String sql="select * from user where uname=?";
List<Object> paramList=new ArrayList<Object>();
paramList.add(uname);
List<Map<String, Object>> list= dbHelper.executeQuery(sql, paramList);
if(list==null || list.size()==0)
{
String sql2="insert into user values(null,?,?)";
List<Object> paramList2=new ArrayList<Object>();
paramList2.add(uname);
paramList2.add(pwd);
if(dbHelper.executeUpdate(sql2, paramList2)>0)
response.getWriter().println("注册成功");
else
response.getWriter().println("注册失败");
}
else
response.getWriter().println("该用户已注册");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
DbHelper
package com.test.util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.print.attribute.standard.RequestingUserName;
public class DbHelper {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet resultSet;
public DbHelper()
{
getConnection();
}
public void getConnection()
{
try {
if(connection==null || connection.isClosed())
{
Properties properties=new Properties();
InputStream iStream=this.getClass().getResourceAsStream("/db2.properties");
properties.load(iStream);
String driver=properties.getProperty("driver");
String url=properties.getProperty("url");
String uname=properties.getProperty("uname");
String pwd=properties.getProperty("pwd");
Class.forName(driver);
this.connection= DriverManager.getConnection(url, uname, pwd);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public int executeUpdate(String sql,List<Object> paramList)
{
getConnection();
try {
this.preparedStatement=connection.prepareStatement(sql);
if(paramList!=null)
{
for(int i=0;i<paramList.size();i++)
{
this.preparedStatement.setObject(i+1, paramList.get(i));
}
}
return this.preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
close();
}
return 0;
}
public List<Map<String, Object>> executeQuery(String sql,List<Object> paramList)
{
getConnection();
try {
this.preparedStatement=connection.prepareStatement(sql);
if(paramList!=null)
{
for(int i=0;i<paramList.size();i++)
{
this.preparedStatement.setObject(i+1, paramList.get(i));
}
}
this.resultSet= this.preparedStatement.executeQuery();
List<Map<String, Object>> resultList=new ArrayList<Map<String,Object>>();
ResultSetMetaData resultSetMetaData= this.resultSet.getMetaData();
while(resultSet.next())
{
Map<String, Object> map=new HashMap<String,Object>();
for(int i=1;i<=resultSetMetaData.getColumnCount();i++)
{
String columnname= resultSetMetaData.getColumnName(i);
Object columnvalue=resultSet.getObject(columnname);
map.put(columnname, columnvalue);
}
resultList.add(map);
}
return resultList;
} catch (SQLException e) {
e.printStackTrace();
}finally {
close();
}
return null;
}
public void close()
{
if(resultSet!=null)
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(preparedStatement!=null)
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(connection!=null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/itstar?characterEncoding=utf-8
uname=itstar
pwd=yyy123456