Tomcat登录表单返回找不到资源

时间:2018-12-27 14:06:45

标签: java html jsp tomcat login

我在基本登录方面遇到了困难。

我正在使用:
-tomcat 8
登陆页面是index.jsp
-linux mint 18

我不是jsp的新手,所以我只是想使基本表单起作用。 Tomcat服务器已启动并正在运行,并且重定向到端口80
我可以访问主登录页面,但是当我输入有效或无效的凭据时,服务器返回“ 404资源未找到错误”
我尝试了重定向,转发到实际的jsp页面(从web-inf出入),甚至重定向到www.google.es,都遇到了同样的错误...

任何人都可以向这个领域透露些信息吗??我很困惑,看不到我缺少什么... 谢谢!

我的代码(在重定向/转发中加入注释行是因为我尝试了很多事情)

@WebServlet(description = "A controller for handling user logins",         
urlPatterns = { "/Login" })
public class LoginController extends HttpServlet {

private static final long serialVersionUID = 1L;
private HttpSession session; 
private String url;
private int loginAttempts;
private boolean us,ps;

/**
 * Servlet constructor
 */
public LoginController() {
    super();
}

/**
 * Process GET requests/responses (logout)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //User has clicked the logout link
    session = request.getSession();

    //check to make sure we've clicked link
    if(request.getParameter("logout") !=null){

        //logout and redirect to frontpage
        logout();
        url="index.jsp";
    }

    //forward our request along
    RequestDispatcher dispatcher = request.getRequestDispatcher(url);
    dispatcher.forward(request, response);
}

/**
 * Process POST requests/responses (login)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //get our current session
    session = request.getSession();

    //get the number of logins
    if(session.getAttribute("loginAttempts") == null){
        loginAttempts = 0;
    }

    //exceeded logins
    if(loginAttempts > 2){
        String errorMessage = "Error: Number of Login Attempts Exceeded";
        request.setAttribute("errorMessage", errorMessage);
        url = "index.jsp";
    }else{  //proceed
        //pull the fields from the form
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        try {
            us = ProtectUserPassword.comprobar(username, true);
            ps = ProtectUserPassword.comprobar(password, false);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        if(us && ps){
            //invalidate current session, then get new session for our user (combats: session hijacking)
            session.invalidate();
            session=request.getSession(true);
            session.setAttribute("user", username);
//              url="/WEB-INF/UserAccount.jsp";
            response.sendRedirect("www.google.es");
        }       
        else{
            String errorMessage = "Error: Unrecognized Username or Password<br>Login attempts remaining: "+(3-(loginAttempts));
            request.setAttribute("errorMessage", errorMessage);

                //track login attempts (combats: brute force attacks)
                session.setAttribute("loginAttempts", loginAttempts++);
//              url="/index.jsp";
            response.sendRedirect("www.google.es");
        }
    }
//      //forward our request along
//      RequestDispatcher dispatcher = 
request.getRequestDispatcher(url);
//      dispatcher.forward(request, response);
    }


    public void logout() {
    session.invalidate();
    }
}

0 个答案:

没有答案