首页 > 其他分享 >简单的登录系统

简单的登录系统

时间:2022-12-21 11:57:34浏览次数:46  
标签:xxxx 登录 req 系统 userBean messageModel 简单 import com

Login.jsp
<%--
Created by IntelliJ IDEA.
User: dell
Date: 2022/12/20
Time: 23:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

登录界面

用户登录界面

请输入账号:
请输入密码:
请选择类型:
${messageModel.msg}

mapper
package com.xxxx.mapper;

import com.xxxx.Bean.userBean;
import org.apache.ibatis.annotations.Select;

public interface userMapper {
//定义sql语句
@Select("select * from user where username=#{username}")
userBean Login(String username);
}
userBean
package com.xxxx.mapper;

import com.xxxx.Bean.userBean;
import org.apache.ibatis.annotations.Select;

public interface userMapper {
//定义sql语句
@Select("select * from user where username=#{username}")
userBean Login(String username);
}
MessageModel
package com.xxxx.Bean;

public class MessageModel {
private Integer code= 1;//状态码
private String msg= "成功!";//消息

public Integer getCode() {
    return code;
}

public void setCode(Integer code) {
    this.code = code;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public Object getObject() {
    return object;
}

public void setObject(Object object) {
    this.object = object;
}

private Object object;//回显对象

}
Service
package com.xxxx.Service;

import com.xxxx.Bean.MessageModel;
import com.xxxx.Bean.userBean;
import com.xxxx.mapper.userMapper;
import com.xxxx.Util.SqlSessionFactoryUtil;
import com.xxxx.Util.StringUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class Login {
static SqlSessionFactory service = SqlSessionFactoryUtil.getSqlSessionFactory();

public MessageModel userLogin(String username, String password) {
    MessageModel messageModel =new MessageModel();
        userBean u =new userBean();
        u.setUsername(username);
        u.setPassword(password);
        messageModel.setObject(u);
        if (StringUtil.isEmpty(username)||StringUtil.isEmpty(password)) {
            messageModel.setCode(0);
            messageModel.setMsg("用户名和密码不能为空!");
            return messageModel;
        }
        SqlSession sqlSession = service.openSession();
        userMapper us = sqlSession.getMapper(userMapper.class);
        userBean userBean= us.Login(username);
        if(userBean == null){
            messageModel.setCode(0);
            messageModel.setMsg("用户名不存在");
            return messageModel;
        }
        if(!password.equals(userBean.getPassword())){
            messageModel.setCode(0);
            messageModel.setMsg("用户密码不正确");
            return messageModel;
        }
        //登录成功
        messageModel.setObject(userBean);



    return messageModel;
}

}
Servlet
package com.xxxx.Servlet;

import com.xxxx.Bean.MessageModel;
import com.xxxx.Service.Login;

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 java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Login login =new Login();
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String leixing = req.getParameter("leixing");
MessageModel messageModel = login.userLogin(username,password);
if(messageModel.getCode()==1) {//成功
if (leixing.equals("1")) {
req.getSession().setAttribute("userBean", messageModel.getObject());
resp.sendRedirect("User.jsp");
}
if (leixing.equals("2")) {
req.getSession().setAttribute("userBean", messageModel.getObject());
resp.sendRedirect("Student.jsp");
}
if (leixing.equals("3")) {
req.getSession().setAttribute("userBean", messageModel.getObject());
resp.sendRedirect("Teacher.jsp");

        }
    }
    else {//失败
        req.setAttribute("messageModel",messageModel);
        req.getRequestDispatcher("Login.jsp").forward(req,resp);
    }


    }

}

/if(leixing 1){
req.getSession().setAttribute("userBean",messageModel.getObject());
resp.sendRedirect("User.jsp");
}
if(leixing
2){
req.getSession().setAttribute("userBean",messageModel.getObject());
resp.sendRedirect("Student.jsp");
}
if(leixing==3){
req.getSession().setAttribute("userBean",messageModel.getObject());
resp.sendRedirect("Teacher.jsp");
}
/

标签:xxxx,登录,req,系统,userBean,messageModel,简单,import,com
From: https://www.cnblogs.com/lin513/p/16995934.html

相关文章

  • 30秒在Centos7安装Nginx(步骤简单)
    Nginx安装1、安装好依赖gcc、gcc-c++、pcre-devel、zlib-devel、openssl、openssl-devel、wgetyum-yinstallgccpcre-develzlib-developensslopenssl-develgcc-c++w......
  • Linux系统 目录文件制作 ISO 镜像文件
    #下载工具#制作iso镜像文件[root@node01~]#yuminstallgenisoimage-y[root@node01~]#mkisofs-r-ozabbix-5.4.iso/zabbix-5.3/zabbix-5.3/[root@node01~]#llz......
  • 禁止windows系统自动更新
    第一步win+r,输入services.msc找到WindowsUpdate单击 常规->启动类型选择禁用。恢复->第一次失败选择无操作最后应用保存  第二步win+r,......
  • 基于Java springboot+mybatis+mysql+jsp网上书城管理系统
    @目录一、系统介绍二、功能展示1.主页(客户)2.登陆(客户)3.我的购物车(客户)4.我的订单(客户)5.我的图书(商家)6.新书上架(商家)7.订单管理(商家)7.统计分析(管理员)8.用户管理(用户管理......
  • 基于springboot+mybatis+vue软件缺陷管理系统
    @目录一、系统介绍二、功能展示1.主页2.个人中心3.缺陷管理4.项目管理5.系统管理6.统计分析三、代码展示四、获取源码一、系统介绍系统主要功能:开发人员:主页、个人中心(......
  • 基于Spring+SpringMVC+Mybatis+Mysql在线考试系统
    @目录一、系统介绍二、功能展示1.用户登陆2.学生页面3.考试信息(老师)4.试卷库(老师)5.试题库(老师)6.考生信息(老师)7.成绩分析(老师)8.成绩排名(老师)9.错题统计(老师)10.成绩导出(老......
  • 基于Springboot+Mybatis+mysql+vue考研规划与交流系统
    @目录一、系统介绍二、功能展示1.主页(普通用户)2.登陆、注册(普通用户)3.复习规划制定(普通用户)4.经验分享交流(普通用户)5.考研学校资讯(普通用户)6.我的文章(普通用户)7.个人信......
  • 从几个简单例子谈随机优化技术
    从几个简单例子谈随机优化技术1.关于随机优化(stochasticoptimization)随机优化技术常被用来处理协作类问题,它特别擅长处理:受多种变量的影响,存在许......
  • react页面登录重定向中遇到的困惑
    是这样的模拟写了一点后端json数据点击登录跳转到首页,目的是我开启,node服务之前,务必进到登录页首页数据是拿到了。请求是发起异步请求,数据也打印出来了。问题是,路由......
  • ssh免密登录
    转载自链接:https://blog.csdn.net/Defiler_Lee/article/details/116278442配置过程首先生成密钥对ssh-keygen-trsa#-t表示类型选项,这里采用rsa加密算法2.上传......