-1

I can pass a parameter from .html to .jsp, but can't pass from .jsp to .html. What can be done to receive the parameter from .jsp? Would it be possible?

This is what we tried so far:

Javascript code on html:

    <script>
        function mensagemErro() {
            document.getElementById("mensagem").innerHTML = ${mensagem};
        }
    </script>

JSP code:

<html>
<head>
    <title>Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<% 
    ControleUsuario controleUsuario = new ControleUsuario();
    Usuario usuario = new Usuario();

    usuario.atualizarLogin(request.getParameter("login"));
    usuario.atualizarSenha(request.getParameter("senha"));

    if(controleUsuario.obterUsuarioLogin(usuario.obterLogin()) != null){
        if(controleUsuario.obterUsuarioLogin(usuario.obterLogin()).obterSenha().equals(usuario.obterSenha())) {
             request.setAttribute("mensagem", "Senha correta!"); 
        }else{
            request.setAttribute("mensagem", "Senha incorreta!");
        } 
    }else{
        request.setAttribute("mensagem", "Login não encontrado!");
    }
%>
<body>

</body>

halierier
  • 27
  • 5
  • it is unclear what you want to achieve. Pass something from your client to your server? Javascript would be inappropriate then. Maybe just explain your business requirements without your own tech guesses. – Mick Feb 07 '18 at 14:13
  • We've made a index.html page with 2 input texts, a input button and an empty label to receive a message through parameter passed from index.jsp. I could take texts from text fields to index.jsp page but i would like to return an error message to index.html page if it coudn't find login on database. – halierier Feb 07 '18 at 14:50
  • Possible duplicate of [How conditionally show the content of a div rather than another div in a JSP page?](https://stackoverflow.com/questions/27273220/how-conditionally-show-the-content-of-a-div-rather-than-another-div-in-a-jsp-pag) – DJDaveMark Feb 07 '18 at 15:24

1 Answers1

1

You could use scriptlets, but you're better off using JSTL and the Expression Language (EL) in the JSP:

<label class="message">
    <c:if test="${not empty requestScope.mensagem}">
        ${requestScope.mensagem}
    </c:if>
</label>

If you still want to stick to scriptlets then you could do something like:

<label class="message">
    <% if(request.getAttribute("mensagem") != null) { %>
        <%= request.getAttribute("mensagem") %>
    <% } %>
</label>
DJDaveMark
  • 2,669
  • 23
  • 35