Check database if true => image not-available.jpg
Check database else false => image available.jpg
Class: DBUtil.java
Java 2016
private static Connection connection = null;
public static Connection getConnection() {
if (connection != null) {
return connection;
} else {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "1234567");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
// //Test connection
// public static void main(String[] args) {
// System.out.println(getConnection());
// }
Web Pages: Register.jsp
Java 2016
<form action="">
<div>
<span>Username*</span><br>
<input type="text" name="username" id="username"><span id="user-result"></span><br>
</div>
<div>
<span>Email*</span><br>
<input type="text" name="email" id="email"><br>
</div>
<div>
<span>Password*</span><br>
<input type="password"><br>
<input type="submit" value="Register">
</div>
</form>
Ajax in Register.jsp
Java 2016
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var x_timer;
$("#username").keyup(function (e) {
clearTimeout(x_timer);
var user_name = $(this).val();
x_timer = setTimeout(function () {
check_username_ajax(user_name);
}, 1000);
});
function check_username_ajax(username) {
$("#user-result").html('<img src="img/ajax-loader.gif" />');
$.post('CheckUserSevlet', {'username': username}, function (data) {
$("#user-result").html(data);
});
}
});
</script>
Servlet: CheckUserSevlet.java
Java 2016
UserDAO usersDAO = new UserDAO();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (usersDAO.checkUser(request.getParameter("username"))) {
response.getWriter().write("<img src=\"img/not-available.png\" />");
} else {
response.getWriter().write("<img src=\"img/available.png\" />");
}
}
Class: UserDAO.java
Java 2016
public boolean checkUser(String username) {
Connection conn = DBUtils.getConnection();
try {
PreparedStatement stmt = conn.prepareStatement("Select * from product.users where user_name='" + username + "'");
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
conn.close();
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
//Test boolean
// public static void main(String[] args) {
// UserDAO d = new UserDAO();
// System.out.println(d.checkUser("tony"));
// }
web.xml
Java 2016
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>CheckUserSevlet</servlet-name>
<servlet-class>servlet.CheckUserSevlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckUserSevlet</servlet-name>
<url-pattern>/CheckUserSevlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
0 nhận xét:
Post a Comment