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);
}
}
标签:web,代码,req,javax,关于,import,servlet,resp,user From: https://www.cnblogs.com/K-wc2024/p/18631631