AJAX is used to determine whether a user is registered. ajax is used to determine whether a user is registered.

Source: Internet
Author: User

AJAX is used to determine whether a user is registered. ajax is used to determine whether a user is registered.

On many registration pages, we may encounter the following situations. When registering a user name, we may prompt that the user name has been registered. The implementation is to apply AJAX technology.

First, write a login page.

<Html> 

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

You also need to configure in web. xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name>  <welcome-file-list>  <welcome-file>index.jsp</welcome-file> </welcome-file-list>  <servlet> <servlet-name>CheckServlet</servlet-name> <servlet-class>CheckServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckServlet</servlet-name> <url-pattern>/CheckServlet</url-pattern> </servlet-mapping> </web-app>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.