将发布数据从JSP发送到Java Servlet

时间:2014-11-18 12:37:56

标签: java jsp servlets

我正在尝试将数据从JSP文件发送到Java Servlet。我在这里看到了很多关于如何做的例子,我相信我是以正确的方式做的,但由于某种原因,doPost()方法没有在我的servlet中调用。

以下是我添加到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中实现,而我只允许使用spec v2.5(工作时需要)。 任何人都知道如何解决这个问题?

我的目录:

enter image description here

3 个答案:

答案 0 :(得分:2)

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

  1. 将动作属性替换为

    <form action = "./login" method="post">

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

    @WebServlet("/login")

  3. 第三,要访问servlet中的<form>元素,请使用request#getParameter

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

答案 1 :(得分:0)

您的课程在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或更高版本

从提交的<form>使用ServletRequest#getParameter中检索参数。 JSP中使用request#getAttribute来检索使用表达式语言时显示数据的属性。

答案 2 :(得分:0)

只需删除@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">
相关问题