Java Login - Error: HTTP Status 404 – Not Found

Em xin chào các bác :slight_smile:
Em là người mới, hiện đang học cách làm form đăng nhập cơ bản bằng java ạ.
Em hiện tại đang bị vướng 1 vấn đề về URL không được tìm thấy sau khi bấm nút đăng nhập.
Em mong có thể nhận được sự trợ giúp của các bác ở đây ạ.
Em xin cảm ơn các bác trước nhé :slight_smile:

Đây là cấu trúc thư mục ạ

Đây là code của file index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Vi du MVC trong JSP</title>
</head>
<body>
	<form action="login" method="post">
		Username: <input type="text" name="username">
		<br><br>
		Password: <input type="password" name="password">
		<br>
		<input type="submit" value="login">
	</form>
</body>
</html>

code login_success.jsp

<%@ page import="bean.LoginBean" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<p>
		Login success :)
	</p>
	
	<jsp:useBean id="bean" class="bean.LoginBean" scope="request"></jsp:useBean>
	Welcome &emsp; <jsp:getProperty property="username" name="bean" />
	Password &emsp; <jsp:getProperty property="password" name="bean" />
</body>
</html>

code login_error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<p> Login Fail :( </p>
	
	<%@ include file="index.jsp" %>
</body>
</html>

code LoginBean.java

package bean;

public class LoginBean {
	private String username;
	private String password;
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

code LoginController.java

package controller;

/*
 * goi ra de dung class HttpServlet
 * phai tao them serialVersionUID
 */
import javax.servlet.http.HttpServlet;

/*
 * goi ra de dung HttpServletRequest
 */
import javax.servlet.http.HttpServletRequest;

/*
 * goi ra de dung HttpServletResponse
 */
import javax.servlet.http.HttpServletResponse;

/*
 * goi ra de dung ServletException
 */
import javax.servlet.ServletException;

/*
 * goi ra de dung IOException
 */
import java.io.IOException;

/*
 * goi ra de dung PrintWriter
 */
import java.io.PrintWriter;

/*
 * goi ra de tao doi tuong moi tu lop LoginBean
 */
import bean.LoginBean;

/*
 * goi ra de tao doi tuong moi tu lop LoginModel
 */
import model.LoginModel;

/*
 * goi ra de dung RequestDispatcher
 */
import javax.servlet.RequestDispatcher;

public class LoginController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//Dung de lay gia tri username va password ma user nhap vao
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		LoginBean bean = new LoginBean();
		bean.setUsername(username);
		bean.setPassword(password);
		request.setAttribute("bean", bean);
		
		LoginModel loginModel = new LoginModel();
		boolean status = loginModel.checkAccount(bean);
		
		if (status) {
			RequestDispatcher rd = request.getRequestDispatcher("login_success.jsp");
		} else {
			RequestDispatcher rd = request.getRequestDispatcher("login_error.jsp");
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}
}

code LoginModel.java

package model;

import bean.LoginBean;

public class LoginModel {
	public LoginModel() {
		
	}
	
	public boolean checkAccount(LoginBean bean) {
		if ("admin".equals(bean.getPassword())) {
			return true;
		} else {
			return false;
		}
	}

}

file cấu hình web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>mvc_demo</display-name>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>LoginController</servlet-name>
  	<servlet-class>controller.LoginController</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>LoginController</servlet-name>
  	<url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

Khi nhấn nút submit thì bạn xem trên thanh Url của trình hiện lên gì?

3 Likes

À nó có hiện ra đây ạ:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?