need
Verify whether the account password entered by the user exists in the database through ajax asynchronous refresh page.
Technology stack
JSP+Servlet+Oracle
Specific code
JSP section:
<%@ 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></head><script> function createXMLHttpRequest() { try { xmlHttp = new XMLHttpRequest();//Other browsers other than ie use ajax } catch (tryMS) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");//ie browser adaptation} catch (otherMS) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//ie browser adaptation} catch (failed) { xmlHttp = null; } } return xmlHttp; } //Submit request var xmlHttp; function checkUserExists() { var u = document.getElementById("uname"); var username = u.value; if (username == "") { alert("Please enter username"); u.focus(); return false; } //Access the string var url = "loginServlet"; //Create the core xmlhttprequest component xmlHttp = createXMLHttpRequest(); //Set the callback function xmlHttp.onreadystatechange = proessRequest; //Initialize the core xmlHttp.open("post", url, true); //Set the request header xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); //Send the request xmlHttp.send("uname="+username); } //Callback function function burdenRequest() { if (xmlHttp.status==200 && xmlHttp.readyState == 4) { var b = xmlHttp.responseText;//Get the output result of the server if (b=="true") { document.getElementById("alert").innerHTML = "<font color='red'>username already exists! </font>"; }else { document.getElementById("alert").innerHTML = "<font color='blue'>username can be used! </font>"; } } } }</script><body> Please enter the username: <input id="uname" name="uname" type="text" onblur="checkUserExists()" /><div id="alert" style="display:inline"></div></body></html> There is no Dao layer here, just use the servlet and service layer for verification.
Here is the code for JDBC query under service:
import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.stx.service.User;import com.stx.service.ConnectionManager;public class ajaxService { public boolean searchUser (String uname) { //jdbc query whether the user name exists boolean isFalse = false; Connection connection = null; Statement stmt = null; ResultSet rs = null; connection = ConnectionManager.getConnection(); try { stmt = connection.createStatement(); String sql = "select * from user_b where uname='"+uname+"'";//sql statement rs = stmt.executeQuery(sql); isFalse=rs.next(); } catch (SQLException e) { e.printStackTrace(); } finally { ConnectionManager.closeResultSet(rs); ConnectionManager.closeStatement(stmt); ConnectionManager.closeConnection(connection); } return isFalse; }}JDBC connection code:
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class ConnectionManager { private final static String DRIVER_CLASS = "oracle.jdbc.OracleDriver"; private final static String URL = "jdbc:oracle:thin:@localhost:1521:orcl"; private final static String DBNAME = "ibook"; private final static String PASSWORD = "qwer"; public static Connection getConnection() { Connection connection = null; try { Class.forName(DRIVER_CLASS); connection = DriverManager.getConnection(URL, DBNAME, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } public static void closeResultSet(ResultSet rs) { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void closeConnection(Connection connection) { try { if (connection != null && !connection.isClosed()) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); } }}About user class:
public class User { private String uname; public User() { super(); } public User(String uname) { super(); this.uname = uname; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; }About the control layer servlet:
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.stx.service.ajaxService;/** * Servlet implementation class loginServlet */public class loginServlet extends HttpServlet { private static final long serialVersionUID = 1L; private ajaxService ajaxService = new ajaxService(); /** * @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 { request.setCharacterEncoding("UTF-8"); String uname = request.getParameter("uname");//Get the input user name boolean isUname = ajaxService.searchUser(uname);//Calling query method in service response.setCharacterEncoding("UTF-8");//Set character encoding PrintWriter out = response.getWriter(); out.print(isUname); out.flush(); out.close();//Close resources} /** * @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); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.