jave_Web实战03

jave_Web实战03

本次来处理对于登录的数据的后端的处理,包括以下几个步骤,首先获得前端的数据,然后,在数据库里查询,查询成功添加到session(由于账号和对应的工号是十分重要的信息,经常会使用到),然后跳转到本人的主页面

package com.home.servlet;

import com.home.mapper.ShopperMapper;
import com.home.pojo.Shopper;
import com.home.pojo.User;
import com.home.service.ShopperService;
import com.home.service.UserService;

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.swing.*;
import java.io.IOException;

@WebServlet("/Login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String username = req.getParameter("username");
         String password = req.getParameter("password");
         User user = new User(username, password);
        UserService userService = new UserService();
       User user1= userService.login(user);
       req.getSession().setAttribute("user", user1);
        if(user1==null||user1.getType().equals("4")){
            resp.sendRedirect("index.jsp");
        }
        else {
             String type =user1.getType();
             req.getSession().setAttribute("type", type);
             if(type.equals("0")){
                 ShopperService shopperService = new ShopperService();
                 Shopper shopper=shopperService.selectShopperByUserId(username);
                 req.getSession().setAttribute("shopper", shopper);
             }
             if(type.equals("1")){


             }
             if(type.equals("2")){


             }

             resp.sendRedirect("worker.jsp");

        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

posted @ 2024-12-19 22:36  元始天尊123  阅读(4)  评论(0编辑  收藏  举报