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" %>
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(leixing2){
req.getSession().setAttribute("userBean",messageModel.getObject());
resp.sendRedirect("Student.jsp");
}
if(leixing==3){
req.getSession().setAttribute("userBean",messageModel.getObject());
resp.sendRedirect("Teacher.jsp");
}/