简体   繁体   中英

getParameter in JSP sets null to href

I have the following situation, there's my index.jsp page:

<%@ 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>

So, I want to create the link with dynamical values which I get from inputs. But my result link is http://localhost:8080/Lab2_war_exploded/calculatePaymentForSeveralSemesters?value=null&faculty=null and I cannot understand why values from inputs are not added to this href. Can you help me to solve this problem? I will appreciate any help. Thanks in advance!

Until the page is submitted, you won't get the value of period and faculty available in the request object.

For the purpose of demo, I have added a form with submit button. Enter some values for period and faculty and hit the submit button. Now check the link and you will find it to be populated with the desired values eg when you hit the submit button after entering 10 in the period and test in the semester field, you find the value of link as 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM