1

I faced with the following problem:

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

Now I have to enter data to the inputs, then click on Submit and only then I can click on the link. But is there any way to navigate to another pages by clicking on the button and passing parameters to other pages? I will appreciate any help, thanks in advance!

Sergei Mikhailovskii
  • 2,100
  • 2
  • 21
  • 43

1 Answers1

0

But is there any way to navigate to another pages by clicking on the button and passing parameters to other pages?

Yeah. You can just pass the values as inputs within a form:

   <form method="get" action="${pageContext.request.contextPath}/calculatePaymentForSeveralSemesters">
        Period: <input type="number" name="period" value="${period}" size="50"> <br>
        Faculty: <input type="text" name="faculty" value="${faculty} size="50"> <br>
        <button type="submit">Calculate payment for several semesters</button> 
    </form>


   <form method="get"  action="${pageContext.request.contextPath}/showTwoSmallestFaculties">
        Period: <input type="number" name="period" value="${period}" size="50"> <br>
        Faculty: <input type="text" name="faculty" value="${faculty} size="50"> <br>
        <button type="submit">Show two smallest faculties</button> 
    </form>

You could also just do it with javascript...

    Period: <input id="period" type="number" name="period" value="${period}" size="50"> <br>
    Faculty: <input id="faculty" type="text" name="faculty" value="${faculty} size="50"> <br>
   <button type="submit" onclick="showFaculties">Show two smallest faculties</button> 
   <button type="submit" onclick="calculatepayments">Calculate payment for several semesters</button> 

<script>

var period = document.getElementById("period").value;
var faculty = document.getElementById("faculty").value;

function showFaculties(){
     window.location = "/calculatePaymentForSeveralSemesters?value="+period+"&faculty="+faculty;
}
function calculatepayments(){
     window.location = "/showTwoSmallestFaculties"
}

<script>

Also, scriptlets are highly discouraged against... you don't need to do this:

<%=request.getParameter("period")%>

just this is fine:

${period}

Why scriptlets are discouraged:

How to avoid Java code in JSP files?

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44