Good morning buddy, before I help you, let’s consider some changes.
Are you wearing a link to make a request, in case:
<a href="http://localhost:8080/EccomerceJSP2/Carrinho?descricao=${registro.descricao}&valor=${registro.valor}&parcela=">COMPRAR</a>
When using an anchor with href
you automatically send the request with method GET.
According to the standards HTTP, the method GET should be used to recover the data and not to send or edit anything, just recover. What your code is doing is sending data on URL for some business logic on backend.
That’s not only not following standards, it’s not safe. Fine, your request has no sensitive data, but imagine that you want to add the credit card field, for example. You will send the user’s credit card number on URL? Obviously not.
Generally, the secure partner of GET, the method POST.
This method does not cache or keep the user’s data in the browser history, as well as send it safely to the backend. Does not appear in the URL, and is much harder to intercept than the simple method GET.
To use, you open a tag form
and starts the attribute method to "POST".
It would look something like:
<form method="POST"></form>
To tag form
has another important attribute, which is the action.
This attribute specifies the URL to which you will send the POST request.
It would look something like:
<form method="POST" action="http://localhost:8080/EccomerceJSP2/Carrinho"></form>
Note that it was taken from link the attributes in URL, you will now send them via POST.
Just create the form as follows:
<form method="POST" action="http://localhost:8080/EccomerceJSP2/Carrinho">
<input name="descricao" value="${registro.descricao}" readonly="readonly"/>
<input name="codigo" value="${registro.codigo}" readonly="readonly"/>
<input name="valor" value="${registro.valor}" readonly="readonly"/>
<select id="parcelas" name="parcelas">
<option value="1"> ${registro.prod_parc1}</option>
<option value="2">${registro.prod_parc2}</option>
<option value="3">${registro.prod_parc3}</option>
<option value="4">${registro.prod_parc4}</option>
<option value="5">${registro.prod_parc5}</option>
<option value="6">${registro.prod_parc6}</option>
<option value="7">${registro.prod_parc7}</option>
<option value="8">${registro.prod_parc8}</option>
<option value="9">${registro.prod_parc9}</option>
<option value="10">${registro.prod_parc10}</option>
</select>
<button type="submit" class="botao_comprar">COMPRAR</button>
</form>
Note that each input has an attribute name
which is precisely the parameters you were passing on URL formerly.
In his Servlet, now, instead of implementing the method doGet
, you will implement the method doPost
and will recover the data in the same way:
request.getParameter("descricao"); //descrição vindo do formulário
I hope I’ve helped you. Any questions just comment.
Hugs.
thank you for replying Andrew Ribeiro. but now as I will take the option selected and send the value to Servlet ?
– User1999
and in case I’m not using the form because I’m showing the data using a foreach
– User1999
No need. The structure I explained there already does it for you. Servlet you will already have access to
value
of option throughrequest.getParameter("parcelas");
.– Andrew Ribeiro
Example, when the user selects parcel 5, you will not need to mount the URL with the
value
of option which contains plot 5. Just give a Submit in the form that parcel 5 will already be associated with the "parcels" parameter automatically.– Andrew Ribeiro
Another question Andrew is showing the data according to a <c:Choose><c:when test="${! Empty param.Description}"> is a <c:foreach items="${requestScope.products}" var="record"> so the lib tag should go before or after the <form tag>?
– User1999
And I do not want to present the data of the product description, code and value in the input but present them as titles
– User1999
@User1999, for each loop of the foreach, you will ride a form. Then the libs open before the form and close after the form.
– Andrew Ribeiro
do not want to present the data of the product description in the input and rather present them as titles so if I put so <H3 name="Description" value="${record.Description}"></H3> would send the value of <H3> to Servlet ?
– User1999
@user1999 No, only inputs are sent in the post. You can just leave the <H3 value="${record.Description}" /> and use a <input type="Hidden" name="Description" value="${record.Description}" /> to send the data.
– mari
@Andrew Ribeiro POST absolutely does not encrypt the data.
– mari
@But, perfect. I was wrong about the encryption, I had https in my head. Reply has already been edited.
– Andrew Ribeiro