IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> “21天好习惯”第一期-1 -> 正文阅读

[JavaScript知识库]“21天好习惯”第一期-1

学习内容一:ajax实现无刷新用户名验证

实现无刷新用户名验证。当用户名文本框失去焦点时,发送请求到服务器,判断用户名是否存在,如果已经存在提示 “用户名已被使用”,如果不存在则提示 “用户名可以使用”。

1.前后端用json交换数据。

2.同时用原生js和jquery库两种方式实现

代码:

(1):registerServlet(负责输入用户名的后端判断、处理)

package Servlet1;

import JDBC1.JDBCDemo1;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.out;

@WebServlet(name = "registerServlet", value = "/registerServlet")
public class registerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        JDBCDemo1 jdbcDemo1 = new JDBCDemo1();
        int count = jdbcDemo1.findInMysql(username);
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        ObjectMapper mapper = new ObjectMapper();
        PrintWriter out= response.getWriter();
        if(count!=0){
            String resJSON = mapper.writeValueAsString("用户名已被使用");
            response.getWriter().write(resJSON);
        }else{
            count = jdbcDemo1.insertIntoMysql(username);
            if(count!=0){
                String resJSON = mapper.writeValueAsString("用户名可以使用");
                response.getWriter().write(resJSON);
            }else{
                String resJSON = mapper.writeValueAsString("用户名不可以使用");
                response.getWriter().write(resJSON);
            }
        }
}
}

(2):JDBCDemo1(负责链接数据库并进行用户名的查找及插入)

package JDBC1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo1 {
    public int findInMysql(String username){
        Connection connection = null;
        Statement statement = null;
        int count = 0;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String sql = "update stu set username = '"+username+"' where username='"+username+"'";
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbdb", "root", "yyyyy");
            statement = connection.createStatement();
            count = statement.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return count;
        }
    }
    public int insertIntoMysql(String username){
        Connection connection = null;
        Statement statement = null;
        int count = 0;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String sql = "insert into stu values('"+username+"','password')";
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbdb", "root", "yyyyy");
            statement = connection.createStatement();
            count = statement.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return count;
        }
    }
}

(3):register(用原生JS实现无刷验证用户名的简单Html界面)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>javascript</title>
</head>
<body>
<script type="text/javascript">
    function fun(){
        key = document.getElementById("key").value;
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","registerServlet?username="+key,true);
        xmlhttp.send();
        xmlhttp.onreadystatechange=function (){
            if(xmlhttp.readyState==4 && xmlhttp.status==200){
                var responseText = xmlhttp.responseText;
                alert(responseText);
            }
        }
        return false;
    }
</script>
<h1>请输入账号</h1>
<form action="" method="post">
    账号:<input name="username" type="text" id="key" οnblur="return fun();"><br>
    密码:<input name="password" type="password" id="password1"><br><br>
    <input type="submit" οnclick="return fun();">
</form>

</body>
</html>

(4):register2(用JQuery库实现无刷验证用户名的简单Html界面)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajax</title>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    function fun(){
        key = document.getElementById("key").value;
        $.ajax({
            "url":"registerServlet?username="+key,
            "type":"get",
            "success":function(result){
                alert(result)
            }
        });
        return false;
    }
</script>
<h1>请输入账号</h1>
<form action="" method="post">
    账号:<input name="username" type="text" id="key" οnblur="return fun();"><br>
    密码:<input name="password" type="password" id="password1"><br><br>
    <input type="submit" οnclick="return fun();">
</form>

</body>
</html>

代码运行界面:

1.网页界面

?

2.输入已被使用的用户名,并使用户名文本框失去焦点

?

3.输入未被使用的用户名,并使用户名文本框失去焦点

学习内容二:算法学习(高精度加、减、乘、除)

1.高精度加法

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<int> add(vector<int> &A,vector<int> &B)
{
? ? if(A.size()<B.size()) ? return add(B,A);

? ? vector<int> C;

? ? int t=0;
? ? for(int i=0;i<A.size();i++)
? ? {
? ? ? ? t+=A[i];
? ? ? ? if(i<B.size()) ?t+=B[i];
? ? ? ? C.push_back(t%10);
? ? ? ? t/=10;
? ? }

? ? if(t) ? C.push_back(1);
? ? return C;
}

int main()
{
? ? string a,b;
? ? vector<int> A,B;
? ? cin >> a >> b;
? ? for(int i=a.size()-1;i>=0;i--)
? ? ? ? A.push_back(a[i]-'0');
? ? for(int i=b.size()-1;i>=0;i--)
? ? ? ? B.push_back(b[i]-'0');
? ? vector<int> C=add(A,B);
? ? for(int i=C.size()-1;i>=0;i--)
? ? ? ? printf("%d",C[i]);
? ? return 0;
}

2.高精度减法

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int cmp(vector<int> &A,vector<int> &B)
{
? ? if(A.size()!=B.size()) ?return A.size()>B.size();
? ? for(int i=A.size()-1;i>=0;i--)
? ? ? ? if(A[i]!=B[i])
? ? ? ? ? ? return A[i]>B[i];
? ? return true;
}

vector<int> sub(vector<int> &A,vector<int> &B)
{
? ? vector<int> C;
? ? for(int i=0,t=0;i<A.size();i++)
? ? {
? ? ? ? t=A[i]-t;
? ? ? ? if(i<B.size())
? ? ? ? ? ? t-=B[i];
? ? ? ? C.push_back((t+10)%10);
? ? ? ? if(t>=0)
? ? ? ? ? ? t=0;
? ? ? ? else
? ? ? ? ? ? t=1;
? ? }
? ? while(C.size()>1&&C.back()==0)
? ? ? ? C.pop_back();
? ? return C;
}

int main()
{
? ? string a,b;
? ? vector<int> A,B;
? ? cin >> a >> b;
? ? for(int i=a.size()-1;i>=0;i--)
? ? ? ? A.push_back(a[i]-'0');
? ? for(int i=b.size()-1;i>=0;i--)
? ? ? ? B.push_back(b[i]-'0');
? ? vector<int> C;
? ? if(cmp(A,B))
? ? {
? ? ? ? C=sub(A,B);
? ? ? ? for(int i=C.size()-1;i>=0;i--)
? ? ? ? ? ? printf("%d",C[i]);
? ? }
? ? else
? ? {
? ? ? ? C=sub(B,A);
? ? ? ? printf("-");
? ? ? ? for(int i=C.size()-1;i>=0;i--)
? ? ? ? ? ? printf("%d",C[i]);
? ? }
? ? return 0;
}

3.高精度乘法

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<int> mul(vector<int> &A,int b)
{
? ? vector<int> C;
? ? int t=0;
? ? for(int i=0;i<A.size()||t;i++)
? ? {
? ? ? ? if(i<A.size()) t+=A[i]*b;
? ? ? ? C.push_back(t%10);
? ? ? ? t/=10;
? ? }
? ? while(C.size()>1 && C.back()==0)
? ? ? ? C.pop_back();
? ? return C;
}

int main()
{
? ? string a;
? ? int b;
? ? vector<int> A,C;
? ? cin >> a;
? ? cin >> b;
? ? for(int i=a.size()-1;i>=0;i--)
? ? ? ? A.push_back(a[i]-'0');
? ? C = mul(A,b);
? ? for(int i=C.size()-1;i>=0;i--)
? ? ? ? printf("%d",C[i]);
? ? return 0;
}

4.高精度除法

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

vector<int> div(vector<int> &A,int b,int &r)
{
? ? vector<int> C;
? ? r=0;
? ? for(int i=A.size()-1;i>=0;i--)
? ? {
? ? ? ? r=r*10+A[i];
? ? ? ? C.push_back(r/b);
? ? ? ? r%=b;
? ? }
? ? reverse(C.begin(),C.end());
? ? while(C.size()>1&&C.back()==0)
? ? ? ? C.pop_back();
? ? return C;
}

int main()
{
? ? string a;
? ? int b;
? ? vector<int> A,C;
? ? cin >> a >> b;
? ? for(int i=a.size()-1;i>=0;i--)
? ? ? ? A.push_back(a[i]-'0');
? ? int r;
? ? C=div(A,b,r);
? ? for(int i=C.size()-1;i>=0;i--)
? ? ? ? printf("%d",C[i]);
? ? cout << endl << r << endl;
? ? return 0;
}

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-10-24 14:50:47  更:2021-10-24 14:51:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 5:55:24-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码