博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web在线聊天室(完结) --- 注册用户+ip地址
阅读量:4136 次
发布时间:2019-05-25

本文共 4445 字,大约阅读时间需要 14 分钟。

注册用户

注册用户

接口设计

请求:POST /register{
name: xxx, password: xxx, nickName: "蔡徐坤", signature: "我擅长唱跳rap篮球",}响应:HTTP/1.1 200 OK{
ok: 1, reason: xxx}

前端异步回调ajax函数

register(){
$.ajax({
url: 'register', type: 'post', contentType: 'application/json', data: JSON.stringify({
name: app.registerForm.inputUsername, password: app.registerForm.inputPassword, nickName: app.registerForm.inputNickName, signature: app.registerForm.inputSignature, }), success: function(data, status) {
if (!data.ok) {
alert('注册失败! ' + data.reason); return; }else {
alert("恭喜你,注册成功!") } app.registerForm.showDialog = false; app.login.showLoginDialog = true; } }) }

编写servlet实现注册业务逻辑

package org.example.servlet;import org.example.dao.UserDao;import org.example.exception.AppException;import org.example.model.User;import org.example.util.Util;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.sql.Timestamp;/** * Created with IntelliJ IDEA. * Description:注册 * User: starry * Date: 2021 -05 -31 * Time: 16:09 */@WebServlet("/register")public class RegisterServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
} @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); resp.setContentType("application/json"); User user = new User(); try {
//1. 解析请求数据:根据接口文档,需要使用反序列化操作 User input = Util.deserialize(req.getInputStream(),User.class); //2. 业务处理:数据库验证账号密码,如果验证通过,创建session,保存用户信息 if (input.getName() == null || input.getName().equals("")) {
throw new AppException("用户名为空"); } if (input.getPassword() == null || input.getPassword().equals("")) {
throw new AppException("密码为空"); } if (input.getNickName() == null || input.getNickName().equals("")) {
throw new AppException("昵称为空"); } int result = UserDao.insertUser(input); user = input; //构造操作成功的正常返回数据:ok-true,业务字段 user.setOk(true); }catch (Exception e) {
e.printStackTrace(); //构造操作失败的错误信息:ok-false,reason:错误信息 user.setOk(false); //自定义异常,自己抛,为中文信息,可以给用户看 if (e instanceof AppException) {
//e捕获到的异常是不是自定义异常 user.setReason(e.getMessage()); }else {
//非自定义异常,英文信息,给前端看“未知错误” user.setReason("未知的错误,请联系管理员"); } } //3. 返回响应数据:从响应对象获取输出流,打印输出到响应体body中 resp.getWriter().println(Util.serialize(user)); }}

操作数据库插入新用户

/**     * 插入注册用户     */    public static int insertUser(User input) {
Connection connection = null; PreparedStatement statement = null; try {
connection = Util.getConnection(); String sql = "insert user(name,password,nickName,iconPath,signature,lastLogout) values(?,?,?,?,?,?)"; statement = connection.prepareStatement(sql); statement.setString(1,input.getName()); statement.setString(2,input.getPassword()); statement.setString(3,input.getNickName()); statement.setString(4,""); statement.setString(5,input.getSignature()); Timestamp time = new Timestamp(System.currentTimeMillis()); statement.setTimestamp(6,time); //将注册用户返回给前端的数据修改一下 input.setIconPath(""); input.setLastLogout(time); return statement.executeUpdate(); }catch (Exception e) {
throw new AppException("插入注册用户失败",e); }finally {
Util.close(connection,statement); } }

实现效果

哪一栏没写会提醒 :注册失败+原因

在这里插入图片描述
点击注册后,显示注册成功
在这里插入图片描述
接下来登录duoduo账号,可以登录
在这里插入图片描述

项目发布地址

欢迎小伙伴们点进来看看呀🤗

转载地址:http://vixvi.baihongyu.com/

你可能感兴趣的文章
python循环语句与C语言的区别
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
还不会正则表达式?看这篇!
查看>>
100道+ JavaScript 面试题,助你查漏补缺
查看>>
JavaScript深入理解之闭包
查看>>