问题描述
我正在尝试通过表单从最终用户那里获取输入.我使用jsp
制成了表格.
I'm trying to get input from the end user through a form. I made the form by using jsp
.
welcome.jsp
welcome.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome</title>
</head>
<body>
<form action="welcome" method="post">
<input type="text" value="username" />
<input type="text" value="password" />
<input type="submit" value="login" />
</form>
</body>
</html>
然后,用户将输入的信息将进入servlet,并在其中将其输出到控制台.
The information that the user will enter will then go to a servlet where it will be printed out to the console.
MyApp.java
MyApp.java
import java.io.IOException;
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("/welcome")
public class MyApp extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/welcome.jsp").forward(req, resp);
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println("Name: " + name);
System.out.println("Password: " + password);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1"
>
<servlet>
<servlet-name>MyApp</servlet-name>
<servlet-class>main.com.myfirstapp.MyApp</servlet-class>
</servlet>
</web-app>
在服务器上执行程序时遇到问题.这是我得到的错误
I encounter my problem when I execute my program on the server. This is the error I am getting
HTTP Status 405 - HTTP method GET is not supported by this URL
type: Status report
message: HTTP method GET is not supported by this URL
description: The specified HTTP method is not allowed for the requested resource.
推荐答案
我已经遇到了相同的问题,那么我建议您从操作中删除该斜杠,而只使用<form action="welcome" method="post">
,因为您使用的是@注解,因此不需要web.xml即可将其删除.另一件事是您不会在servlet中得到等待的结果,因为表单中没有名称,这是一个示例您在表单中键入String name = req.getParameter("name");
,您必须设置名称插入值,例如<input type="text" name="name" />
,同样的事情,最终您的表单将看起来像
i have already suffering from the same problem, well i suggest you to remove that slash from the action and let it just <form action="welcome" method="post">
, since you are using @annotation so there is no need for web.xml you can delete it .another thing you will not get the waited result in your servlet cause there is no name in the form , here is an exampleyou type String name = req.getParameter("name");
in the form you have to set name insted of value like this <input type="text" name="name" />
and the same thing for the finally your form will look like
<form action="welcome" method="post">
<input type="text" name="name" />
<input type="text" name="passcode" />
<input type="submit" value="submit" />
</form>
这篇关于HTTP状态405-使用jsp时,此URL不支持HTTP方法GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!