【问题标题】:Sending post data from JSP to a Java Servlet将发布数据从 JSP 发送到 Java Servlet
【发布时间】: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,但它没有解决我的问题。

标签: java jsp servlets


【解决方案1】:

我可以在您的帖子中看到多个错误,

  1. 将action属性替换为,

    &lt;form action = "./login" method="post"&gt;

  2. 如果您使用的是 2.5 版本,请删除注释的导入以及此行,

    @WebServlet("/login")

  3. 第三,要访问 servlet 中的 &lt;form&gt; 元素,请使用 request#getParameter

    String username = request.getParameter("username"));

【讨论】:

  • 当我执行
    时,它会重定向到不存在的 localhost:8080/UserModule/WebContent/jsp/login。我的 Servlet 在同一个项目中,但是在不同的目录和包下。
  • @显然在动作属性中使用./login
  • 我确实在 action 属性中设置了 ./login,如下所示:&lt;form action = "./login" method="post"&gt;
  • 是您的 servlet 的有效路径 &lt;servlet-class&gt;src.action.LoginServlet&lt;/servlet-class&gt;。在您的问题中发布您的目录结构
  • 我解决了这个问题。它仍然无法正常工作。无论如何,我在我的问题中发布了我的目录结构。谢谢。
【解决方案2】:

只需删除@WebServlet("/login")

这个注解和 servlet 映射是可以互换的。因此,当您使用其中之一时,不需要另一个定义。

为了使用@WebServlet webapp 的 web.xml 必须声明为 3.0 版

<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_3_0.xsd"
    version="3.0">

【讨论】:

  • 我知道,但我必须使用 2.5 版
  • 那么@WebServlet 将不适合您。坚持 web.xml 中的 servlet 映射,正如@luiggi-mendoza 所说,考虑用 ${request.contextPath} 替换硬编码的 localhost:8080
  • 正如我在@luiggi-Mendoza 的帖子中提到的那样,
    只重定向到 localhost:8080/login 而不会存在
【解决方案3】:

您的课程在包action 中,所以它的全名是action.LoginServlet。您已将 src.action.LoginServlet 配置为 servlet 类。从那里删除src

<servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>action.LoginServlet</servlet-class>
</servlet>

这部分:

<form action = "localhost:8080/UserModule/src/action/login" method="post">

那里有很多样板代码。相反,使用Expression Language 添加localhost:8080/webappName,如下所示:

<form action = "${request.contextPath}/login" method="post">

由于您使用的是 Servlet 规范 2.5,因此您不能使用 @WebServlet 注释。所以从你的 Serlvet 类中删除它。否则,通过更改 web.xml 中的 servlet 规范来开始使用 Servlet 3.0 或更高版本

要从提交的&lt;form&gt; 中检索参数,请使用ServletRequest#getParameterrequest#getAttribute 在 JSP 中用于检索属性以在使用表达式语言时显示数据。

【讨论】:

  • 这只是重定向到不存在的 localhost:8080/login
  • @显然那么您的应用程序有问题。我已经使用了好几次,并且按预期工作。
  • @显然,请提供有关您如何部署 Web 应用程序以及文件分布情况的信息。
  • 我猜。但即使它是硬编码的,我使用ServletRequest#getParameter 并修复了你提到的所有其他问题,不幸的是它仍然无法正常工作。
  • @显然您是否完成了我在此处发布的所有更改,或者只是根据所有答案进行了混合更改?
猜你喜欢
  • 2013-05-27
  • 2014-07-21
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多