JSP 中的 getParameter 将 null 设置为 href

getParameter in JSP sets null to href

我有以下情况,这是我的 index.jsp 页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Lab2</title>
</head>
<body>
<p>
    Period: <input type="number" name="period" size="50">
    <br>
    Faculty: <input type="text" name="faculty" size="50">
    <br>
    <br>
    <a href="${pageContext.request.contextPath}/calculatePaymentForSeveralSemesters?value=<%=request.getParameter("period")%>&faculty=<%=request.getParameter("faculty")%>">Calculate
        payment for several semesters</a>
    <a href="${pageContext.request.contextPath}/showTwoSmallestFaculties">Show two smallest faculties</a>
</p>
</body>
</html>

所以,我想用从输入中获得的动态值创建 link。但是我的结果 link 是 http://localhost:8080/Lab2_war_exploded/calculatePaymentForSeveralSemesters?value=null&faculty=null,我不明白为什么输入的值没有添加到这个 href。你能帮我解决这个问题吗?我将不胜感激任何帮助。提前致谢!

在提交页面之前,您不会获得请求对象中可用的 periodfaculty 的值。

为了演示的目的,我添加了一个 formsubmit 按钮。为 periodfaculty 输入一些值,然后点击 submit 按钮。现在检查 link ,您会发现它填充了所需的值,例如当您在 period 中输入 10 并在 semester 字段中输入 test 后点击 submit 按钮时,您会发现 link 的值为http://localhost:8080/TestDynamicProject/calculatePaymentForSeveralSemesters?value=10&faculty=test

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<html>
<head>
<title>Lab2</title>
</head>
<body>
    <p>
    <form>
        Period: <input type="number" name="period" size="50"> <br>
        Faculty: <input type="text" name="faculty" size="50"> <br>
        <a href="${pageContext.request.contextPath}/calculatePaymentForSeveralSemesters?value=<%=request.getParameter("period")%>&faculty=<%=request.getParameter("faculty")%>">Calculate payment for several semesters</a> 
        <a href="${pageContext.request.contextPath}/showTwoSmallestFaculties">Show two smallest faculties</a> 
        <input type="submit">
    </form>
    </p>
</body>
</html>