实现登录注册功能

  • 注册功能


    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException; public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //解决中文乱码问题
    request.setCharacterEncoding("UTF-8"); //获取表单数据
    String userName=request.getParameter("username");
    String userEmail=request.getParameter("email");
    String userPwd=request.getParameter("password"); //创建数据库连接对象
    Connection connection=null;
    PreparedStatement preparedStatement=null; try {
    // 连接数据库
    Class.forName("com.mysql.cj.jdbc.Driver"); // 获取连接
    connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
    "root","123456"); // 获取预编译数据库操作对象
    String sql="insert into t_userInfo(username,email,userpwd)values(?,?,?)";
    preparedStatement=connection.prepareStatement(sql); //给预编译数据库操作对象插入值
    preparedStatement.setString(1,userName);
    preparedStatement.setString(2,userEmail);
    preparedStatement.setString(3,userPwd); //执行SQL语句
    int count=preparedStatement.executeUpdate(); // 显示执行结果
    if(count!=0){
    //跳转
    response.sendRedirect("/myWeb/RegisterSuccess.html");
    } } catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
    }finally {
    //释放资源
    if(preparedStatement!=null){
    try {
    preparedStatement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if(connection!=null){
    try {
    connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
  • 登录功能

    
    
       import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.*; public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //解决中文乱码问题
    request.setCharacterEncoding("UTF-8"); //获取用户提交的表单数据
    String userEmail=request.getParameter("email");
    String userPwd=request.getParameter("password");
    //创建数据库连接对象
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null; try { // 连接数据库
    Class.forName("com.mysql.cj.jdbc.Driver"); // 获取连接
    connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",
    "root","123456"); // 获取预编译数据库操作对象
    String sql="select email,userpwd from t_userInfo where email=? and userpwd=?";
    preparedStatement=connection.prepareStatement(sql); //给预编译语句传参
    preparedStatement.setString(1,userEmail);
    preparedStatement.setString(2,userPwd); //执行SQL语句
    resultSet=preparedStatement.executeQuery(); //处理查询结果集
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out=response.getWriter();
    //HTML代码
    out.print(" <!DOCTYPE html>");
    out.print("<meta http-equiv=\"content-type\" content=\"text/html\" charset=\"utf-8\"/> ");
    out.print(" <head>");
    out.print(" <meta charset='UTF-8'>");
    out.print(" <title>用户信息</title>");
    out.print(" </head>");
    out.print(" <body>");
    out.print(" <h3 align='center'>用户信息表</h3>");
    out.print(" <hr width='60%'>");
    out.print(" <table border='1' align='center' width='50%'>");
    out.print(" <tr align='center'>");
    out.print(" <th>序号</th>");
    out.print(" <th>用户邮箱</th>");
    out.print(" <th>用户密码</th>");
    out.print(" </tr>");
    int i=1;
    while(resultSet.next()){
    String email=resultSet.getString("email");
    String pwd=resultSet.getString("userpwd");
    out.print(" <tr align='center'>");
    out.print(" <th>"+(i++)+"</th>");
    out.print(" <th>"+email+"</th>");
    out.print(" <th>"+pwd+"</th>");
    out.print(" </tr>");
    } out.print(" </table>");
    out.print(" </body>");
    out.print(" </html>"); } catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
    }finally { //释放资源
    if(resultSet!=null){
    try {
    resultSet.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    if(preparedStatement!=null){
    try {
    preparedStatement.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    } if(connection!=null){
    try {
    connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    } }
    }

JAVAEE_Servlet_20_登录注册功能的更多相关文章

  1. Java Spring+Mysql+Mybatis 实现用户登录注册功能

    前言: 最近在学习Java的编程,前辈让我写一个包含数据库和前端的用户登录功能,通过看博客等我先是写了一个最基础的servlet+jsp,再到后来开始用maven进行编程,最终的完成版是一个 Spri ...

  2. Node.js实现登录注册功能

    使用Node.js + Navicat for mysql实现的登录注册功能 数据库中存在有”user_id,user_name,password,user_img,user_number“字段,其中 ...

  3. SSM 实现登录注册功能

    1.上一篇SSM框架搭建好了之后就要开始写功能了,现在来写一个简单的登录注册功能 这几个包是自己手动创建的,然后往里面写代码 2.代码详情 package com.maike.controller; ...

  4. flask 开发用户登录注册功能

    flask 开发用户登录注册功能 flask开发过程议案需要四个模块:html页面模板.form表单.db数据库操作.app视图函数 1.主程序 # app.py # Auther: hhh5460 ...

  5. vue koa2 mongodb 从零开始做个人博客(一) 登录注册功能前端部分

    0.效果演示 插入视频插不进来,就很烦.可以出门右拐去优酷看下(点我!). 1.准备工作 1.1前端框架 前端使用了基于vue.js的nuxt.js.为什么使用nuxt.js? 首先我做的是博客的项目 ...

  6. JAVAEE——宜立方商城11:sso登录注册功能实现、通过token获得用户信息、Ajax跨域请求(jsonp)

    1. 学习计划 第十一天: 1.sso注册功能实现 2.sso登录功能实现 3.通过token获得用户信息 4.Ajax跨域请求(jsonp) 2. Sso系统工程搭建 需要创建一个sso服务工程,可 ...

  7. App登录注册功能,怎样做到用户体验最佳?

    用户登录系统,可以细分为三项功能模块,分别是:登录.注册和密码找回.本文作者将结合自身经历,谈谈他在做这块的时候一些想法,主要是涉及业务流程. 登录和注册功能,不论是PC端还是移动端,大多数产品都会涉 ...

  8. PHP实现用户登录注册功能

    初学php做了一些比较常见且有用的页面,放在上面记录一下咯 我是用了bootstrap框架里面的模态框做注册登陆页面,这样页面比较美观 页面效果: 注册成功条件/功能: 1)用户名不能冲突 2)两次密 ...

  9. 一个关于vue+mysql+express的全栈项目(三)------ 登录注册功能的实现(已经密码安全的设计)

    本系列文章,主要是一个前端的视角来实现一些后端的功能,所以不会讲太多的前端东西,主要是分享做这个项目学到的一些东西,,,,, 好了闲话不多说,我们开始搭建后端服务,这里我们采用node的express ...

随机推荐

  1. 保证软件开发过程遵循ISO 26262功能安全标准的十个主要进阶步骤

    保证软件开发过程遵循ISO 26262功能安全标准的十个主要进阶步骤 为保障汽车软件质量,使软件开发符合ISO 26262功能安全标准,需要我们对开发流程做出改进,并与2018年的更新同步. 本视频课 ...

  2. LDAP + Samba 安装配置流程

    LDAP + Samba 安装配置 基础环境:Ubuntu18.04 安装samba root@cky:~# apt install samba smbldap-tools -y 查看版本 root@ ...

  3. 看完我的笔记不懂也会懂----less

    目录 Less学习 语法篇 注释 变量 映射(Maps) @规则嵌套和冒泡 less中的嵌套规则 less中的混合 less的运算 extend延伸/继承 less忽略编译(转义) 导入(Import ...

  4. 不用代码趣讲 ZooKeeper 集群

    本文作者:HelloGitHub-老荀 Hi,这里是 HelloGitHub 推出的 HelloZooKeeper 系列,免费开源.有趣.入门级的 ZooKeeper 教程,面向有编程基础的新手. 项 ...

  5. HDOJ-6641(欧几里得+异或运算)

    TDL HDOJ-6641 关于题意,就是要找出符合f的第m大的数,而且后面还要满足异或等式. 通过观察题目,可以发现n太大了,所以不能直接枚举.当然因为m比较小,所以可以转换思路k^n,这个数最大不 ...

  6. ArrayList源码分析笔记

    ArrayList源码分析笔记 先贴出ArrayList一些属性 public class ArrayList<E> extends AbstractList<E> imple ...

  7. PTA甲级—常用技巧与算法

    散列 1078 Hashing (25 分) Quadratic probing (with positive increments only) is used to solve the collis ...

  8. 手把手教你集成华为机器学习服务(ML Kit)人脸检测功能

    当给自己拍一张美美的自拍照时,却发现照片中自己的脸不够瘦.眼睛不够大.表情不够丰富可爱-如果此时能够一键美颜瘦脸并且添加可爱的贴纸的话,是不是很棒? 当家里的小孩观看iPad屏幕时间过长或者眼睛离屏幕 ...

  9. 鸿蒙应用程序Ability(能力)看这一篇就够

    本节概述 什么是Ability Ability分类 Ability生命周期 Ability之间跳转 什么是Ability Ability意为能力,是HarmonyOS应用程序提供的抽象功能.在Andr ...

  10. C# 应用 - 封装类访问 Mysql 数据库

    个人经历的项目主要都是用 Postgresql 或 Oracle 数据库,本文非原创,从他处整理而来. 1. 库类 mysql.data.dll using MySql.Data.MySqlClien ...