【发布时间】:2014-11-18 12:37:55
【问题描述】:
我正在尝试将数据从 JSP 文件发送到 Java Servlet。我在这里看到了很多关于如何做到这一点的例子,我相信我的做法是正确的,但由于某种原因,我的 servlet 中没有调用 doPost() 方法。
这是我添加到 web.xml 文件中的内容:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>src.action.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
这是我的 JSP 文件
<html>
<head>
</head>
<body>
<form action = "localhost:8080/UserModule/src/action/login" method="post">
Username<input type = "text" name = "username"><br><br>
Password<input type = "password" name = "password"><br><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
这是我的 Java servlet:
package action;
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;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String username = (request.getAttribute("username")).toString();
System.out.println("asd");
String password = (String) request.getAttribute("password");
System.out.println(username);
}
}
在import javax.servlet.annotation.WebServlet; 和@WebServlet("/login") 上给我一个错误
这是因为它们只在规范 v3.0 中实现,而我只能使用规范 v2.5(工作时需要)。 有人知道如何解决这个问题吗?
我的目录:
【问题讨论】:
-
这是您的Ref,您可以从给定链接中获取所有重要信息
-
我看到了这个帖子 Parth,但它没有解决我的问题。