Registering users with Java and saving them in the database

Source: Internet
Author: User
Tags stub

Registration page

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>User Registration</title><Scripttype= "Text/javascript">//Verify the registration information on this page so that it cannot be emptyfunctionCheck () {varUID=document.getElementById ("userid"); if(Uid.value== "") {alert ("user code cannot be empty"); return false; }     if(Uform.usermingc.value== "") {alert ("user name cannot be empty"); return false; }    if(Uform.password.value== "") {alert ("The password cannot be empty"); return false; }    if(Uform.quer.value!=uform.password.value) {alert ("Inconsistent login password and confirmation password"); return false; }       return true; }</Script></Head><Body><formID= "Uform"Action= "Saveuser"onsubmit= "return check ()" >User code:<inputID= "userid"type= "text"name= "userid"width=30><BR><BR>User name:<inputID= "USERMINGC"type= "text"name= "username"width=30><BR><BR>Login Password:<inputID= "Password"type= "Password"name= "Password"width=30><BR><BR>Confirm Password:<inputID= "Quer"type= "Password"name= "Queren"width=30><BR><BR><inputtype= "Submit"value= "Submit"></form></Body></HTML>

Save interface

 PackageCom.hanqi;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJava.sql.*;/*** Servlet Implementation class Saveuser*/@WebServlet ("/saveuser") Public classSaveuserextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /**     * @seeHttpservlet#httpservlet ()*/     PublicSaveuser () {Super(); //TODO auto-generated Constructor stub    }    /**     * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response)*/    protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html; Charset=utf-8 "); String userid= Request.getparameter ("userid"); String username= Request.getparameter ("username"); String PW= Request.getparameter ("Password"); if(UserID = =NULL|| Userid.trim (). Length () = = 0) {Response.getwriter (). Append ("User ID cannot be empty"); }        Else if(Username = =NULL|| Username.trim (). Length () = = 0) {Response.getwriter (). Append ("User name cannot be empty"); }        Else        {            Try             {                //Registering Database DriversClass.forName ("Oracle.jdbc.driver.OracleDriver"); //Connection URLString url = "Jdbc:oracle:thin: @localhost: 1521:orcl" ; //Drive Manager Factory modeConnection conn = drivermanager.getconnection (URL, "test", "test"); if(Conn! =NULL)                {                    //manipulating DatabasesString sql = "INSERT into T_user (User_id,user_name,password) VALUES (?,?,?)" ; //classes that execute SQL statementsPreparedStatement pps =conn.preparestatement (SQL); Username=NewString (Username.getbytes ("iso-8859-1"), "UTF-8"); Pps.setstring (1, UserID); Pps.setstring (2, username); Pps.setstring (3, PW); introw = Pps.executeupdate ();//executes the SQL statement and returns the number of rows of data                                        if(Row > 0) {Response.getwriter (). Append ("Save" +row+ "bar Data succeeded"); }                    Else{response.getwriter (). Append ("Save data Failed"); }                                        //Freeing ResourcesPps.close ();                Conn.close (); }                Else{response.getwriter (). Append ("Failed to connect to database"); }            }            Catch(Exception e) {e.printstacktrace ();            Response.getwriter (). Append (E.getmessage ()); }} response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); }    /**     * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/    protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stubdoget (request, response); }}

Registering users with Java and saving them in the database

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.