Set Attribute from JSP radiobutton

Asked

Viewed 423 times

1

I am putting together a generic form for registration of legal and natural persons and the definition part of two RadioButton's that change the fields, after completed they are sent to a Servletand then to a class DAO, the form has different type fields CPF for PF and CNPJ for PJ, I have a condition in Servlet that you need to validate the selected type in order to play the fields in the class Usuario(), and I want to know how I can make this validation in JSP from the radio button?

Basic structure:

 <form>
    <!-- Tipo de Cadastro -->                                    
    <input id="radiopf" type="radio" name="Type" checked value="pessoaPF">
    <label for="radiopf">Pessoa Fisica</label>            
    <input id="radiopj" type="radio" name="Type" value="pessoaPJ"> 
    <label for="radiopj">Pessoa Juridica</label>
 </form>

 <!-- Formulário de Cadastro -->
 <form id="frm" name="FormCadastro" action="Controle" method="POST"> 
   <fieldset id = "PF">
       <!-- Campos Pessoa Física -->
   </fieldset>

   <fieldset id = "PJ">
       <!-- Campos Pessoa Jurídica-->
   </fieldset>
   <input type="submit" value="Gravar" name="Cadastrar" onclick="return Validate();" />
 </form>

OBS: Validate(); is a validation in JavaScript made before sending the data to the Servlet checks data type, size, formatting, null fields etc.
Controle is the Servlet receiving the data via request.getParameter() to send to the class DAO.

1 answer

1


I got...

//html dentro do mesmo <form id="frm"></form>
<input id="radiopf" type="radio" name="Type" checked value="pessoaPF">
<input id="radiopf" type="radio" name="Type" value="pessoaPJ">

//Servlet
if(request.getParameter("Type") != null){
    if(request.getParameter("Type").equals("pessoaPF")) {
         //Atribuição Pessoa Física dentro da classe Usuario();
    }
    if(request.getParameter("Type").equals("pessoaPJ")) {
         //Atribuição Pessoa Jurídica dentro da classe Usuario();
    }
 }

Browser other questions tagged

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