实现登录注册功能

  • 注册功能


    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. ElasticSearcher的安装以及安装过程中出现的问题

    先给出参考链接,带安装成功后再进行总结整个过程. 参考链接:https://blog.csdn.net/fjyab/article/details/81101284 java操作ElasticSear ...

  2. Python 过滤字母和数字

    [前言]在写爬虫时,正则表达式有时候比较难写,一个是自己不熟练,二者数据分析提取数据千奇百怪. 一.好在python有个re模块,提供了很多更加简便的方法:可参考此文档:https://www.cnb ...

  3. 翻译:《实用的Python编程》03_01_Script

    目录 | 上一节 (2.7 对象模型) | 下一节 (3.2 深入函数) 3.1 脚本 在该部分,我们将深入研究编写 Python 脚本的惯例. 什么是脚本? 脚本就是运行和终止一系列语句的程序. # ...

  4. Prometheus时序数据库-磁盘中的存储结构

    Prometheus时序数据库-磁盘中的存储结构 前言 之前的文章里,笔者详细描述了监控数据在Prometheus内存中的结构.而其在磁盘中的存储结构,也是非常有意思的,关于这部分内容,将在本篇文章进 ...

  5. MVC模式从Controller返回内容协商格式(Json或者Xml)

    WebAPI默认的返回格式Json,但是MVC是View,如果在MVC的控制器中,想要返回Json格式该怎么操作呢 在MVC的控制器中返回json数据只需要然会JsonResult而不是ActionR ...

  6. mongoDB服务器连接不上Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException:

    一大早打开node项目就报错,终端报 UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFU ...

  7. MySQL数据库之一

    数据库简介 数据库分类 关系型数据库(SQL):(狭义可以理解为行和列) MySQL,Oracle,Sql Server, DB2 通过表和表之间,行和列之间的关系进行存储 非关系型数据库(NoSQL ...

  8. python基础学习之描述符和装饰器

    描述符的了解: 描述符协议: python描述符是一个"绑定行为"的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有: __get__, __set__, 和__ ...

  9. Kubernetes 实战 —— 03. pod: 运行于 Kubernetes 中的容器

    介绍 pod P53 pod 是 Kubernetes 中最为重要的核心概念,而其他对象仅仅用于 pod 管理. pod 暴露或被 pod 使用. pod 是一组并置的容器,代表了 Kubernete ...

  10. ACM常用的C++ && STL

    内容 c++输入输出 c++ string vector:不定长数组 map:映射 queue:队列 sort:排序 priority_queue:优先队列 c++输入输出 1 #include &l ...