java - jsp中的request.getParameter返回null或空字符串

标签 java sql jsp servlets

我制作了一个用于输入的 html 文本框,并希望将其连接到数据库。 但是每次我在文本框中输入内容并转到结果页面时,结果只显示属性的名称,而不显示任何元组。 我认为 request.getParameter() 返回 null 或空字符串。我尝试了几次,但找不到任何解决方案。

这是我的代码。 这是 selectTestForm.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.sql.*" %>

<!DOCTYPE html>
<html>
<head>
    <title>Select the game</title>
</head>
<body>
    <p>Input opponent team</p>
    <form name="form1" method="get" action="result.jsp">
        <p>Opponent team : <input type="text" name="oppon"></p>
        <p><input type="submit" name="Submit" value="send"></p>
    </form>
</body>
</html>

这就是结果.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.sql.*" %>

<!DOCTYPE html>
<html>
<head>
    <title>Find the game</title>
</head>
<body>
    <table width="500" border="1">
        <tr>
            <td width="100">Game ID</td>
            <td width="100">Opponent Team</td>
            <td width="100">Start Date</td>
        </tr>
<%
    String opponent = (String) request.getParameter("oppon");

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try{
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }catch(ClassNotFoundException cnfe){
        cnfe.printStackTrace();
        System.out.println("Driver loading error");
    }
    try{
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
        String userId = "sports_booking";
        String userPass = "jade";
        con = DriverManager.getConnection(jdbcUrl, userId, userPass);

        String sql = "select * from game where opponent=?";

        pstmt = con.prepareStatement(sql);
        pstmt.setString(1, "opponent");
        rs = pstmt.executeQuery();

        while( rs.next() ) {
            String game_id = rs.getString("game_id");
            String start_date = rs.getString("start_date");

%>
            <tr>
                <td width="100"><%= game_id %></td>
                <td width="100"><%= opponent %></td>
                <td width="100"><%= start_date %></td>
            </tr>

<%
        }

    }catch(SQLException e){
        e.printStackTrace();

        if(rs != null) {
            try {
                rs.close();
            }catch(SQLException sqle) {} 
        }
        if(pstmt != null) {
            try {
                pstmt.close();
            }catch(SQLException sqle) {}
        }
        if(con != null) {
            try {
                con.close();
            }catch(SQLException sqle) {}
        }
    }
%>

    </table>

</body>
</html>

如果您能帮助我解决问题,我将不胜感激!

最佳答案

问题出在 pstmt.setString(1, "opponent"); 行,因为您设置了 const "opponent" 字符串而不是变量。

pstmt.setString(1, "opponent"); -> pstmt.setString(1, opportunity); 现在应该可以工作了。

关于java - jsp中的request.getParameter返回null或空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62288065/

相关文章:

java - 计算泊松分布随机变量之和的概率

java - 无法获取 'https://google.bintray.com/.../maven-metadata.xml' 。从服务器 : Forbidden 收到状态码 403

sql - 在 Postgres 查询中保留数组成员顺序

java - struts2中如何使用jsp将值传递到新窗口

java - 取消选中框

java - JSP 页面是/否对话框

php - 检查两个日期之间是否包含句点

mysql - 从mysql查询中选择第二长的时间

JSP 编程 - response.getWriter().flush();不起作用

java - 没有使用 mockmvc 的请求映射