【问题标题】:How to pass parameter to Java using HTML如何使用 HTML 将参数传递给 Java
【发布时间】:2015-03-12 17:37:01
【问题描述】:

我是 Java 和一般编程的新手。作为背景,我正在研究我的简单调查系统。我拥有从 MySQL 中插入和拉取所需的所有类/方法,并在我的 JSP 页面上显示调查。

这是我的 Index.jsp

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Survey System</title>
</head>
<body>                
    <h1>Survey List</h1>
    <%            
        ResultSet resultset = new controller.SelectSurvey().SelectSurvey();    

        if (request.getParameter("btnControlSurvey") != null){
            response.sendRedirect("controlsurvey.jsp");
        }

        if (request.getParameter("btnTakeSurvey") != null){
            response.sendRedirect("takesurvey.jsp");                   
        }
    %>
    <form name = 'surveylist' action="index.jsp" method="POST">             
        <table border="0">                
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <tr>                        
                    <td>
                        <select id="listsurvey">
                            <% while (resultset.next()) {                                     
                            %>
                            <option><%= resultset.getString("survey_ID")%> - <%= resultset.getString("title")%></option>
                            <% } %>
                        </select>
                    </td>                        
                    <td>
                        <input type="submit" value="TAKE SURVEY" name = "btnTakeSurvey" />                            
                    </td>
                </tr>                                                                                                     
            </tbody>
            <td>
                <input type="submit" value="CONTROL SURVEY" name = "btnControlSurvey" />                                         
            </td>
        </table>
    </form>                            
</body>

单击“提交”按钮后,如何将 ID 为 listsurvey 的下拉列表中选定的surveyID 值传递给 Takesurvey.jsp(见下文)中硬编码的surveyID1 变量?

<%            
    int surveyID1 = 1;
    ResultSet rsSurvey = new controller.SelectSurvey().SelectSurveyByID(surveyID1);
    ResultSet rsSurveyQuestion = new controller.SelectSurvey().SelectQuestionByID(surveyID1);
%>

我发现至少有以下三种方法可以做到这一点。什么是最简单的方法,你能给我举个例子吗?

将值放入会话对象中。 将值放入应用程序对象 将值放在重定向 URL 的末尾。

非常感谢您的意见。

谢谢你,祝你有美好的一天。

【问题讨论】:

    标签: html jsp jakarta-ee parameters request


    【解决方案1】:

    不确定您在第二个代码段中在做什么,但您可以使用获取表单参数

    String surveyId = request.getParameter(listsurvey);
    

    看起来您正在寻找另一边的 int,但它以 String 的形式出现,因此只需使用 Integer.parseInt() 从请求中拉取它之后。

    注意: request 变量在您的 JSP 中已经隐式可用。您不必声明它。

    http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter%28java.lang.String%29

    【讨论】:

    • 你不能将 String 转换为 int... 你需要使用 Integer.parseInt
    • 这是真的!修正了我的术语
    • 我的第二个代码段在另一个 jsp 页面中,因此您的代码将无法工作。感谢您的意见。
    • 我能够弄清楚并使其与您的代码示例一起使用。但是我需要将表单操作更改为 takesurvey.jsp,这使我的控制调查按钮也加载了 takesurvey.jsp 页面,这是一个问题。因此,我不能使用 request.getParameter.
    • 请您给我最好的答案好吗?
    【解决方案2】:

    您需要使用 GET 或 POST 在 HTTP 页面请求中传递调查 ID。然后在 servlet/JSP 中可以使用 request.getParameter("surveyID")

    【讨论】:

    • 你能举个例子吗?
    • 请看我上面的评论。
    【解决方案3】:

    我通过使用 request.getParameters 解决了这个问题,并为每个按钮使用不同的形式,它可以按需要工作。

    再次感谢您的所有输入。

    【讨论】:

      猜你喜欢
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多