请在给我投任何反对票之前......等等......我是个新手......我无法解决这个错误......。我是非常新的......我无法解决错误......。
我得到以下错误在我的java servlet第一个程序;名称的servlet类是Anjali和servlet网址和名称是vashisth。
错误是这样的。
HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 5.1.0
xml代码是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>vashisth</servlet-name>
<servlet-class>Anjali</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>vashisth</servlet-name>
<url-pattern>/vashisth</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
html代码是:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<form name="loginForm" method="get" action="Anjali">
Username: <input type="text" name="username"/> <br/>
Password: <input type="password" name="password"/> <br/>
<input type="submit" value="Login" />
</form>
</div>
</body>
</html>
而我的java程序是这样的。
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Anjali")
public class Anjali extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// read form fields
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("username: " + username);
System.out.println("password: " + password);
// do some processing here...
// get response writer
PrintWriter writer = response.getWriter();
// build HTML code
String htmlRespone = "<html>";
htmlRespone += "<h2>Your username is: " + username + "<br/>";
htmlRespone += "Your password is: " + password + "</h2>";
htmlRespone += "</html>";
// return response
writer.println(htmlRespone);
}
}
请帮我找到错误的地方......明天是提交类似作业的最后一天......我整个人都去纠正了很多这种类型的错误......。
拜托拜托拜托
在你的html代码中,你已经提到 method="get" 并在你的java类中 Anjali.java你写了只接受post请求的代码,所以你才会收到404。因为你的请求类型不匹配。
解决方案:请更改 method="get" ---> method="post"
希望能解决你的问题。