AJAX is used to determine whether the user is registered

亚连
Release: 2018-05-23 10:02:12
Original
1502 people have browsed it

This article mainly introduces AJAX in detail to determine whether the user is registered, which has certain reference value. Interested friends can refer to it

On many registration pages, we may We will encounter the following situations. 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
姓名:
密码:
Copy after login

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(); } } } }
Copy after login

You also need to configure it in web.xml

    index.jsp   CheckServlet CheckServlet   CheckServlet /CheckServlet  
Copy after login

The above I compiled it for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use ajax to pass json to the front desk and solve the problem of question marks and garbled characters in Chinese

Perfect solution to ajax cross-domain requests Parsererror error

ajax submits the mobile phone number to the database for verification and returns the status value

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!