很久不写 Servlet 有大佬知道这是啥问题嘛

查看 62|回复 3
作者:hemingyang   
这是过滤器方法
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)resp;
HttpSession session = request.getSession();
String path = request.getRequestURI();
if(path.contains("login.jsp") || path.contains("/login") ||path.contains("register.jsp") ||path.contains("register") || path.contains("/DrawValidateCode") || path.contains("/layui") || path.contains("/image")){
chain.doFilter(req, resp); //登录页面,登录请求,验证码请求,前端资源和图片一律放行
}else{
String username = (String)session.getAttribute("username");
if (username == null){ //用户不存在一律重定向到登录页
response.sendRedirect(request.getContextPath() + "/login.jsp");
}else {
chain.doFilter(req, resp);
}
}
}
这个注册
@WebServlet(urlPatterns = "/addUser")
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.printf("1111111111111");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    String jsonRegistration = RequestUtil.getRequestBody(req);
    Gson gson = new Gson();
    JsonObject jsonObject = new JsonParser().parse(jsonRegistration).getAsJsonObject();
    String username = jsonObject.get("username").getAsString();
    String password = jsonObject.get("password").getAsString();
    resp.setContentType("application/json");
    BaseResponse[I] baseResponse = new BaseResponse[I]();
    boolean registrationSuccess = AdminDao.insertAdministrator(username, password);
    if (registrationSuccess) {
        baseResponse.setCode(200);
        baseResponse.setMsg("注册成功");
        resp.sendRedirect(req.getContextPath() + "/login.jsp");
    } else {
        baseResponse.setCode(500);
        baseResponse.setMsg("注册失败,请重试");
    }
    PrintWriter out = resp.getWriter();
    out.print(gson.toJson(baseResponse));
    out.flush();
    out.close();
}
}
前端就 layui'
就是进不了这个放 addUser 打断也调试了看不出问题

req, username, resp, string

mgzu   
github 整个 demo 先,这没法看
hemingyang
OP
  
@mgzu https://github.com/hemingyang/studentManagement/tree/main/src/main
hemingyang
OP
  
@mgzu 我上传了,大佬看看
您需要登录后才可以回帖 登录 | 立即注册

返回顶部