Example of asynchronous verification for Ajax Registration

Source: Internet
Author: User

 

 

Database table structure:

 

The table has a record:

 

Project Structure:

 

Register. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Checkservlet. Java

package com.xu.servlets;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 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 = "org.gjt.mm.mysql.Driver";public static final String DBURL = "jdbc:mysql://localhost:3306/mydb";public static final String DBUSER = "root";public static final String DBPASS = "root";private static final long serialVersionUID = 7502494116656742257L;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("utf-8");response.setContentType("text/html");Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;PrintWriter out = response.getWriter();String userid = request.getParameter("userid");try{Class.forName(DBDRIVER);conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);String sql = "SELECT COUNT(userid) FROM user WHERE userid = ?";pstmt = conn.prepareStatement(sql);pstmt.setString(1, userid);rs = pstmt.executeQuery();if(rs.next()){if(rs.getInt(1)>0){out.print("true");}else{out.print("false");}}out.close();}catch (Exception e) {e.printStackTrace();}finally{try{conn.close();}catch (Exception e) {e.printStackTrace();}}}}

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>  <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>CheckServlet</servlet-name>    <servlet-class>com.xu.servlets.CheckServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CheckServlet</servlet-name>    <url-pattern>/CheckServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

Running result:

 

 

Related Article

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.