Help with a JSP!

0

I need to show a message I have from a (try) in a jsp. I already tried with <% = NombreVariableDeMenjeje% > But it's not working. My code is as follows:

<%
int PUERTO = 4000;

try{

    System.out.println("Iniciando Servidor...");
    //Creamos un objeto de la clase ServerSockets para esperar conexiones por el puerto designado
    ServerSocket socket_servidor = new ServerSocket(PUERTO);
    //Imprimimos que estamos escuchando al puerto
    System.out.println("Escucho al puerto: " + PUERTO);
    System.out.println("Esperando conexiones de los clientes...");

    //Mediante un ciclo, restringimos la atención do sólo 3 clientes
    for(int numCliente = 0; numCliente < 200 ; numCliente++){

    double sl = 5.7;    
    double sw = 2.8;    
    double pl = 4.1;    
    double pw = 1.3;    


    String parametros = "{'sl':" + sl + ", 'sw':" + sw + ", 'pl':" + pl + ", 'pw':" + pw +"}" ; 

    //Creamos un objeto de la clase socket para gestionar las conexiones de cada cliente
    //El método .accept() crea un socket para atender a un cliente  que se ha conectado
    Socket socket_cliente = socket_servidor.accept();
    System.out.println("--------------------------------------------------");
    //Imprimimos en pantalla que estamos atendiendo al cliente #tal :v
    System.out.println("Sirvo al cliente : "+numCliente+", en el puerto de comunicación: " +  socket_cliente.getPort());
    //Creamos un stream de salida para enviar los mensajes a los clientes
    OutputStream mensajeParaCliente = socket_cliente.getOutputStream();
    //La clase DataOutStream es útil para escribir los datos de tipo primitivo de una forma portable
    DataOutputStream flujoSecuencial = new DataOutputStream(mensajeParaCliente);
    //Escribimos en el flujo secuencial del socket mediante el .writeUTF  y lo enviamos al cliente
    flujoSecuencial.writeUTF(parametros/*"Este es un mensaje enviado desde el servidor :v""Bienvenido cliente: " + numCliente + ".\n El puerto de eschucha es: " + socket_cliente.getLocalPort() + " y el puerto de comunicaciones bidereccional es: " + socket_cliente.getPort()*/ );
    //Creamos un stream de entrada para recibir todos los mensajes del cliente 
    InputStream is = socket_cliente.getInputStream();
    BufferedReader buf =new BufferedReader(new InputStreamReader(is));
    String mensaje = buf.readLine();
    System.out.println("El cliente " + numCliente + " Dice --> " + mensaje);

    socket_cliente.close(); 

    }


    System.out.println("Demasiados Clientes...");
    System.out.println("Solo puedo atender 3 clientes...");


    }catch(Exception e){

        System.out.println(e.getMessage()); 

    } 
%>

The variable I want to show is:

String mensaje = buf.readLine();

The html code is:

<!DOCTYPE html>

             Iris Calculator                                                               

IRIS CALCULATOR

                    
                        <form action="#" name="formulario">
                        <label> Sepal Length:
                                <input type="text" name="sl" size="3" maxlength="1"  readonly="readonly" value="<%=sl%>"/>
                            </label><br><br>

                            <label> Petal Length:
                                <input type="text" name="pl" size="3" maxlength="1" readonly="readonly" value="<%=pl%>"/>
                            </label><br><br>

                             <label> Sepal Width:
                                <input type="text" name="sw"  size="3" maxlength="1" readonly="readonly" value="<%=sw%>"/>
                            </label><br><br>


                            <label> Petal Width:
                                <input type="text" name="pw" size="3" maxlength="1"  readonly="readonly" value="<%=pw%>"/>
                            </label><br><br>

                        </form>
                </div>

            <form action="#" name="resultado">

                <div class="w3ls-bottom">
                    <strong>
                        <p>Esta especie es: <a id="especie"><%=clase%></a></p>
                    </strong>
                </div>
            </form>     
        </div>  
    </section>
</body>

And I want to put the message in:

<p>Esta especie es: <a id="especie"><%=mensaje%></a></p>

But do not take it ...

Beforehand, thank you very much for reading and helping me

asked by Camilo Andrade 14.10.2018 в 20:47
source

1 answer

0

You can not call the message variable from the JSP, you have to send it from the controller to the JSP, there are several ways but one of them could be:

request.setAttribute("msj", mensaje);

where "msj" is the identifying name of the attribute and message the second parameter is the variable that you are going to pass.

then from the JSP in the HTML you put:

<p>Esta especie es: <a id="especie">${msj}</a></p>

Where msj would be the variable you sent from your servlet.

    
answered by 14.10.2018 в 23:07