Home  >  Article  >  Web Front-end  >  AJAX interacts with the database to determine whether the user is registered

AJAX interacts with the database to determine whether the user is registered

php中世界最好的语言
php中世界最好的语言Original
2018-04-02 16:35:301236browse

This time I will bring you AJAX to interact with the database to determine whether the user is registered, and AJAX to interact with the database to determine whether the user is registered. What are the precautions? Here is a practical case, let's take a look.

On many registration pages, we may encounter the following situation. When we register a user name, we may be prompted that the user name has been registered. The implementation is to apply AJAX technology.

First write a login page






用户ID 
姓名:
密码:

Then write a servlet Java code

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckServlet extends HttpServlet{
public static final String DBDRIVER = "oracle.jdbc.OracleDriver";
public static final String DBURL = "jdbc:oracle:thin:@59.173.240.149:1521:heer";
public static final String DBUSER = "hnsyu_dev";
public static final String DBPASS = "hnsyuok";
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("gbk");
response.setContentType("text/html");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
PrintWriter out = response.getWriter();
String userid = request.getParameter("userid");
try {
Class.forName(DBDRIVER);
connection = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = "select count(userid) from userdemo where userid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,userid);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
if(resultSet.getInt(1)>0){
out.print("false");
System.out.println("true");
}else {
out.print("false");
}
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

It also needs to be configured in web.xml



  
 
  index.jsp
 
  
 CheckServlet
 CheckServlet
 
 
 CheckServlet
 /CheckServlet
  

Believe it After reading the case in this article, you have mastered the method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Why garbled characters appear when ajax passes json

Handling ajax cross-domain request parsererror error

The above is the detailed content of AJAX interacts with the database to determine whether the user is registered. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn