gpt4 book ai didi

java - 我得到 "HTTP Status 404 -/MVC_demo/LoginController"。请检查我的程序是否有错误

转载 作者:行者123 更新时间:2023-12-01 10:56:33 24 4
gpt4 key购买 nike

我使用模型、 View 、 Controller 制作了一个登录验证项目,但它没有显示预期的结果。我猜 Controller 部分可能有一些错误。请看一下其中是否有任何错误。

Controller -LoginController.java(servlet)

package mvcdemo.controllers;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mcvdemo.model.Authenticator;
import mcvdemo.model.User;



public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;

public LoginController() {
super();
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String username = request.getParameter("username");
String password = request.getParameter("password");


RequestDispatcher rd = null;


Authenticator authenticator = new Authenticator();
String result = authenticator.authenticate(username,password);
if(result.equals("success")){
rd= request.getRequestDispatcher("/success.jsp");
User user = new User(username,password);
request.setAttribute("user", user);

}
else{
rd= request.getRequestDispatcher("/error.jsp");
}

rd.forward(request, response);
}
}

查看-1)登录.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "LoginController" method = "post">
Enter user : <input type="text" name="username" id="username"/>
<br>
Enter password : <input type="text" name="password" id="password"/>
<input type="submit" />
</form>
</body>
</html>

2) Success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
welcome!!!
</body>
</html>

3)error.jsp

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Login failed!!!
</body>
</html>

型号-

1)Authenticator.java

package mcvdemo.model;

public class Authenticator {
public String authenticate(String username, String password){
if("gaurav".equalsIgnoreCase(username)&&("password".equals(password)))
{
return "success";
}
else{
return "failure";
}
}

}

2)使用.java

package mcvdemo.model;

public class User {

private String username;
private String password;

public User(String username, String password)
{
this.username = username;
this.password = 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;
}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>MVC-demo</display-name>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>mvcdemo.controllers.LoginController</servlet-class>

</servlet>

<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>LoginController</url-pattern>
</servlet-mapping>

</web-app>

最佳答案

我在您的代码中看到的唯一错误是 web.xml 中的以下行:

<url-pattern>LoginController</url-pattern>

应该是

<url-pattern>/LoginController</url-pattern>

即需要在LoginController之前添加一个/

关于java - 我得到 "HTTP Status 404 -/MVC_demo/LoginController"。请检查我的程序是否有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com