Send selected option value by parameter

Asked

Viewed 734 times

0

I’m developing an application Java in which I take the database data and present it in a <select><option> but I don’t know how to catch the value selected and sent by parameter.

JSP

<c:choose>
<c:when test="${!empty param.descricao}">
<c:forEach items="${requestScope.produtos}" var="registro"> 
 <div class="col-sm-6 col-md-6"> 
  <h3>${registro.descricao}</h3>
  <p>${registro.codigo}</p>
  <div class="linha_compra"></div><br/><br/> 
  <div class="col-sm-6 col-md-6">
  <p>${registro.valor}</p> 

  <select id="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><br/><br/>  
  <div class="col-sm-6 col-md-6">
   <button type="button" onclick="" class="botao_comprar"><a href="http://localhost:8080/EccomerceJSP2/Carrinho?descricao=${registro.descricao}&valor=${registro.valor}&parcela=">COMPRAR</a></button> 
  </div>
 </div>
</c:foreach>

1 answer

0


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 ?

  • and in case I’m not using the form because I’m showing the data using a foreach

  • No need. The structure I explained there already does it for you. Servlet you will already have access to value of option through request.getParameter("parcelas");.

  • 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.

  • 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>?

  • 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, for each loop of the foreach, you will ride a form. Then the libs open before the form and close after the form.

  • 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 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.

  • @Andrew Ribeiro POST absolutely does not encrypt the data.

  • 1

    @But, perfect. I was wrong about the encryption, I had https in my head. Reply has already been edited.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.