我搞不清楚到底错哪了,请指教

package login.servlet;

import login.dao.UserDao;
import login.domain.User;

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;

/**

  • @description 登录请求处理类
  • @author WANGZIC
    */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    //接收表单信息
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String verifyc = request.getParameter("verifycode");
    //设置回显
    request.setAttribute("username", username);
    request.setAttribute("password", password);
    request.setAttribute("verifycode", verifyc);
    //获取验证码
    String svc =(String) request.getSession().getAttribute("sessionverify");
    //根据用户名查询用户
    User user =new UserDao().selectByUsername(username);
    if(!svc.equalsIgnoreCase(verifyc)){
    request.setAttribute("loginError", "* 验证码错误");
    request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request, response);
    return;
    }
    if(user!=null){
    if(user.getPassword().equals(password)){
    request.getSession().setAttribute("user", user);
    response.sendRedirect("index.jsp");
    }else {
    request.setAttribute("loginError", "* 密码错误");
    request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request, response);
    }
    }else {
    request.setAttribute("loginError", "* 用户不存在");
    request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request, response);
    }

    }

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

}

package login.servlet;

import login.dao.UserDao;
import login.domain.User;

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;

/**
 * @description 注册请求处理类
 * @author WANGZIC
 */
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rpsw = request.getParameter("rpsw");
        if(username==null||username.trim().isEmpty()){
            request.setAttribute("registError", "用户名不能为空");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp").forward(request, response);
            return;
        }
        if(password==null||password.trim().isEmpty()){
            request.setAttribute("registError", "密码不能为空");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp").forward(request, response);
            return;
        }
        if(!password.equals(rpsw)){
            request.setAttribute("registError", "密码不一致");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp").forward(request, response);
            return;
        }
        UserDao dao = new UserDao();
        User user =new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean res = dao.insert(user);
        if(res){
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else {
            request.setAttribute("registError", "注册失败,该用户名已存在");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp").forward(request, response);
        }
    }

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


1个回答

这种又有表单,又有数据库的程序,建议你调试,或者在关键的地方输出变量,来判断问题在哪里
比如在dopost里输出username
如果什么都没有输出,说明根本没有执行,检查网页
输出的如果不正确,检查参数有没有传入
如果正确,那么显然就是数据库的问题。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问