I do not enter the IF [duplicated]

0

Good I have a jsp that passes data to my servlet with ajax.

This is my Script

$(document).ready(function() {
  var dni = "00000r";

  $.post('actionServlet', {
    }, function(responseText) {
        num: "1"
        $('#datos').html(responseText);
    });

  $("#mostrar").click(function(){
      $.post('actionServlet', {
            num: "2",
            dni: dni
        }, function(responseText) {
            $('#datos').html(responseText);
        });

  });
});

and this my java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType( "text/html; charset=iso-8859-1" );
    PrintWriter out = response.getWriter();

    // Obtengo los datos de la peticion
    String numero = request.getParameter("num");
    String nombre = request.getParameter("nombre");

    System.out.println(numero+" "+numero.getClass());

    if(numero == "1"){
        out.println("Prueba numero 1");
    }

    if(numero == "2"){
        out.println("Ha funcionado");
    }
}

Passing the data does not make me if in the java. Any solution? Thank you very much in advance

    
asked by bsg 08.08.2018 в 12:56
source

1 answer

5

In Java you can not compare objects with == you must use the .equals method

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {     
 response.setContentType( "text/html; charset=iso-8859-1" );
 PrintWriter out = response.getWriter();

 // Obtengo los datos de la peticion
 String numero = request.getParameter("num");
 String nombre = request.getParameter("nombre");

 System.out.println(numero+" "+numero.getClass());

 if(numero.equals("1")){
     out.println("Prueba numero 1");
 }

 if(numero.equals("2")){
     out.println("Ha funcionado");
 } 
}
    
answered by 08.08.2018 / 13:15
source