How to print the response of a servlet in a jsp by means of an alert

1

Hello, I am going to do a few exercises from the university for the java programming class.

What I have to do in this is a login, then I enter the user and a password, which go in 2 text boxes that are in a jsp, by clicking the button sends the data to a servlet that validates and sends the response again to the eye (jsp), which has to come out by means of an alert on the screen to the user.

My code is like this:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>LOGIN</title>
</head>
<body>
    <div align="center">
        <h1>LOGIN</h1>
        <h4>Ingrese los datos que se solicitan: </h4>

        <!-- este es el form que se encarga de encapsular los textbox y los botones, este manda la informacion de estos al servlet -->
        <form name="idLogin" action="srvLogin" method="POST">
            <input type="text" name="txtUsuario" value="" placeholder="NOMBRE DE USUARIO"/><br><br>
            <input type="text" name="txtPass" value="" placeholder="CONTRASEÑA"/><br><br>
            <input type="submit" value="Calcular" name="btnlog" /> <!-- este es el boton que desencadena el evento-->
        </form>
        <% //si ls respuesta de mi servlet es diferente a vacio
            if(request.getAttribute("mensaje")!=null) 
            {  //tendrìa que mandar a imprimir por medio del alert de abajo el mensaje de acceso correcto o incorrecto, pero no me lo muestra
        %>          
                <script>alert( "Hola " + <%=request.getAttribute("mensaje")%>) </script>
                <br>
                <!-- este es una caja de texto que puse para ver si estaba conectando correctamente al servlet y si conecta pero no muestra el alert-->
                <input type="text" name="txtResultado" value="<%=request.getAttribute("mensaje")%>" disabled/>
        <%
            }
                %>
    </div>
</body>

In other words, if it shows everything right, it makes the connection with the servlet and validates and returns the response, however I have a problem with the alert when executing it does not appear, it only shows me the input below the alert that I put it to verify If I made the connection or not, and I see that the problem is in the alert, I have bad syntax of the alert? Thanks again!

    
asked by Kenny99A 21.10.2018 в 21:07
source

1 answer

0
<script>alert( "Hola " + <%=request.getAttribute("mensaje")%>)</script>

If the message is mundo , the HTML is left:

<script>alert( "Hola " + mundo)</script>

But here mundo is not a literal but interprets it as a variable that is not defined, giving an error 1

You can do directly

<script>alert( "Hola <%=request.getAttribute("mensaje")%>")</script>

the tag is processed on the server so the browser reaches it

<script>alert( "Hola mundo")</script>

In any case, this executes the alert when the browser is still processing the HTML and the screen is half drawn; it is much more elegant:

<script>
    window.onload= function() {
       alert( "Hola <%=request.getAttribute("mensaje")%>");
    };
 </script>

which tells you that when you have finished loading the entire page, execute the indicated code.

1 Browsers have a JS console among the navigation tools, it is very useful. And in any case you can always look at the source code of the generated web page to see where the shots go.     
answered by 21.10.2018 в 21:17