【问题标题】:Linking HTML to JSP page将 HTML 链接到 JSP 页面
【发布时间】:2013-05-21 07:51:41
【问题描述】:

我在链接 HTML 和 JSP 时遇到问题。只是尝试制作一个简单的 html 页面来获取“电子邮件”和“密码”,然后让 jsp 显示在下一页上。但是当我点击提交时,参数没有被传递,我只是得到“null”,任何想法我哪里出错了?

HTML 页面

<html>

<head>
        <title>Enter your email and password</title>
</head>

<body>

        <form action="form.jsp" method="get">
        <table cellspacing="5" border="0">
                <tr>
                     <td align="right">Email:</td>
                     <td><input type="text" email=email></td>
                </tr>

                <tr>
                     <td align="right">Password:</td>
                     <td><input type="text" password=password></td>
                </tr>

                <tr>
                     <td></td>
                     <td><br><input type="submit" value="Submit"></td>
                </tr>
        </table>
        </form>
</body>
</html>

JSP 页面

<html>
<body>

        <%
        // get info from request
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        %>

        <p>Here is the info you provided:</p>

        <table cellspacing="5" cellpadding="5" border="1">
            <tr>
                <td align="right">Email:</td>
                <td><%= email %></td>
            </tr>

            <tr>
                <td align="right">Password:</td>
                <td><%= password %></td>
            </tr>

        </table>


        <form action= "Next" method="post">
                <input type="submit" value="Return">
        </form>

        </body>
        </html>

【问题讨论】:

    标签: java html jsp servlets


    【解决方案1】:

    您需要在表单字段中使用name 属性。

    <input type="text" name="email"></td>
    

    “email”的值可以在 JSP 中检索为:

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

    ${param.email}
    

    <c:out value="${param.email}"/> <%--(Using JSTL)--%>
    

    这里是属性的complete list

    name = string #

    与此元素关联的名称/值对的名称部分,用于提交表单。

    【讨论】:

    • +1。但作为旁注:为了避免误解,在您的 JSTL 示例中使用 ${param.value} 会更好,例如,${param.paramName} 或类似的东西(恕我直言)。我的第一印象是${param.value} 是什么?因为不是点.后面的参数的,所以是参数名(即真正的参数名)。
    【解决方案2】:

    您应该指定输入name 属性。试试这个,

    <form action="form.jsp" method="get">
            <table cellspacing="5" border="0">
                    <tr>
                         <td align="right">Email:</td>
                         <td><input type="text" name="email"></td>
                    </tr>
    
                    <tr>
                         <td align="right">Password:</td>
                         <td><input type="text" name="password"></td>
                    </tr>
    
                    <tr>
                         <td></td>
                         <td><br><input type="submit" value="Submit"></td>
                    </tr>
            </table>
            </form>
    

    有关更多信息,您可以试试这个link

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多