Sergei Mikhailovskii
Sergei Mikhailovskii

Reputation: 2465

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!

Upvotes: 1

Views: 186

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79005

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 e.g. 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>

Upvotes: 1

Related Questions