我请求时,getParameter方法丢失了

时间:2016-01-21 20:26:27

标签: java jsp

我正在尝试从表单发布数据并使用getParameter将其存储为String并插入到数据库中,但似乎getParameter甚至不存在。请求后。我不懂那种方法。尝试重建,没有区别。我应该添加外部依赖项吗?我也没有看到任何进口提示。

我的index.jsp文件

<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <img src="img/logo.png">

    <form method="post">
        <div class="form-group">
            <input type="text" class="form-control" id="usernameField" placeholder="Username">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="passwordField" placeholder="Password">
        </div>
        <button type="submit">Login</button>
    </form>

    <%  
        // .getParameter highlighted in red. Not valid
        String username = request.getParameter("usernameField");
        String password = request.getParameter("passwordField");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/tablename", "root", "");
        Statement statement = con.createStatement();
        statement.executeUpdate("INSERT INTO stat(user, pass) VALUES (username, password)");

    %>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我相信你的输入标签应该有一个“name”属性,这将是获取值的请求参数的名称。 E.g。

<input type="text" class="form-control" id="usernameField" name="username" placeholder="Username">

并检索它的值:

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

如果以上操作不起作用,则会显示一个代码段,用于读取所有请求参数及其值:

<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
  String paramName = (String)paramNames.nextElement();
  out.print("<b>" + paramName + ":</b>\n");
  String paramValue = request.getHeader(paramName);
  out.println(paramValue);
}
%>

编辑确定一个更接近你的完整示例:

<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.io.*,java.util.*" %>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

    <img src="img/logo.png">

    <form method="post">
        <div class="form-group">
            <input type="text" class="form-control" id="usernameField" name="username" placeholder="Username">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="passwordField" name="password" placeholder="Password">
        </div>
        <button type="submit">Login</button>
    </form>

    <%  
        // .getParameter highlighted in red. Not valid
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Enumeration paramNames = request.getParameterNames();
        while(paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            out.print("<b>" + paramName + ":</b>\n");
            String paramValue = request.getHeader(paramName);
            out.println(paramValue);
        }

    %>
</body>
</html>

答案 1 :(得分:0)

我遇到了同样的问题,我的解决方案是添加一个javax.servlet依赖项。

有了Maven,就只有以下内容:

<project>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>