Ajax is used to determine if a user is enrolled in an instance tutorial

Source: Internet
Author: User
This article is mainly for everyone in detail the Ajax used to determine whether the user registration, has a certain reference value, interested in small partners can refer to, hope to help everyone.

On many registered pages, we may encounter the following situations, when we register the user name may prompt that the user name is registered, the implementation is the application of Ajax technology.

First write a landing page



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";p ublic static final String dburl = "J Dbc:oracle:thin:@59.173.240.149:1521:heer ";p ublic static final String DBUSER =" Hnsyu_dev ";p ublic static final string Dbpass = "Hnsyuok";p ublic 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);p reparedstatement.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


<?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>

Have you learned it? Feel good hurry up and collect it.

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.