关于web层代码

BaseServlet
package com.cwk.web.listener.servlet;

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.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**

  • 替换HttpServlet 根据请求最后一段路径 进行分发
    */
    public class BaseServlet extends HttpServlet {
    //根据路径进行方法分发
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //1.获取请求路径
    String requestURI = req.getRequestURI();//brand-case/brand/selectAll
    //2.获取最后一段路径 方法名
    String methodName = requestURI.substring(requestURI.lastIndexOf('/') + 1);

     //2.执行方法
     //2.1获取BrandServlet/UserServlet的字节码对象 字节码对象Class
     Class<? extends BaseServlet> aClass = this.getClass();
    
     try {
         Method method = aClass.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
         //执行方法
         method.invoke(this,req,resp);
     } catch (NoSuchMethodException e) {
         throw new RuntimeException(e);
     } catch (InvocationTargetException e) {
         throw new RuntimeException(e);
     } catch (IllegalAccessException e) {
         throw new RuntimeException(e);
     }
    

    }
    }

登录servlet

package com.cwk.web.listener.servlet;

import com.cwk.pojo.User;
import com.cwk.service.UserService;
import com.cwk.service.impl.UserServiceImpl;

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;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
UserService userService = new UserServiceImpl();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();

    //接收数据
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    User user = userService.login(username, password);
    String contextPath = req.getContextPath();
    //存Session

    session.setAttribute("user", user);
    session.setAttribute("username", username);
    //到时候解封即可
   /* if (user != null) {
        if (user.判断职位之类的.equals("1")) {
            resp.sendRedirect(contextPath + "/generalStaff.jsp");
        } else if (user.判断职位之类的().equals("2")) {
            resp.sendRedirect(contextPath + "/DepartmentManager.jsp");
        } else if (user.判断职位之类的().equals("3")) {
            resp.sendRedirect(contextPath + "/GeneralManager.jsp");
        } else if (user.判断职位之类的().equals("4")) {
            resp.sendRedirect(contextPath + "/FinanceStaff.jsp");
        } else {
            System.out.println("数据非法!");
        }


    }
    else {
        System.out.println("nihao");
        //登陆失败
        //存储错误信息
        req.setAttribute("login_msg", "用户名或密码错误");
        //跳转
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

*/

}

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

}

posted @   好像是Cwk  阅读(2)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示