Search code examples
javajspjstl

How to add parameters to request in JSP page?


I have to add to GET method two dates selected by users.
How can I do this, with the opportunity to extract it at servlet later?

Code snippet from jsp page:

<p class="text-left">
  <strong><fmt:message key="apartments.arrival"/>:</strong> <input type="text" id="arrivaldate" name="arrivaldate"/> <br/>
  <strong><fmt:message key="apartments.depart"/>:</strong> <input type="text" id="departdate" name="departdate"/> <br/>
  <strong><fmt:message key="apartments.price"/>:</strong> ${apartment.price} <br/>
</p>
<a class="btn btn-primary btn-large btn-block"
                       href="<c:url value="/purchase?apartment=${apartment.id}"/>"><fmt:message key="button.book"/>
</a>

I have to take arrivaldate + departdate.

The date is chosen with the JQuery date picker.

I want to do the following:

  • take two data values inputted by the user at JSP page
  • set as a parameter to URL
  • at servlet side into doGet() extract these two values. Better to avoid using the form, I want to put this request into doGet().

UPDATE:

Here is the content of a full page - apartments.jsp

How to solve this?


Solution

  • Probably what you want is to overwrite the method doGet of your Servlet (if you have not done that already) and retrieve the GETparameters as

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Stirng arrivaldate = request.getParameter("arrivaldate");
        Stirng departdate = request.getParameter("departdate");
        Stirng id = request.getParameter("apartment");
    }
    

    UPDATE: Looking at your apartments.jsp, I figured out the problem. You are not using any form tag and your button button.book has the GET parameters hardcoded. That is why parameters other than apartment are not submitted.

    To fix it, you are going to have to wrap the content of lines 74 to 81 with a form tag, like this:

    <form action="/purchase" method="GET">
        <p class="text-left">
            <strong><fmt:message key="apartments.arrival"/>:</strong>
            <input type="text" id="arrivaldate" name="arrivaldate"/> <br/>
            <strong><fmt:message key="apartments.depart"/>:</strong>
            <input type="text" id="departdate" name="departdate"/> <br/>
            <strong><fmt:message key="apartments.price"/>:</strong> ${apartment.price} <br/>
        </p>
        <input type="hidden" id="apartment" name="apartment" value="${apartment.id}"/>
        <input class="btn btn-primary btn-large btn-block" type="submit" value="Submit" />
    </form>