0

I could not figure out a way of sending content to a JSP element without using expression language and request dispatcher. The problem is that I can't change the browser URL using the forward method, which makes my application seems weird to the user. So how can I add a message to a <div> tag in a JSP page, without using EL and/or request dispatcher?

Here is my login.jsp page:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>SiCF - Login</title>
    <link rel="stylesheet" href="css/stylesheetlogin.css">
</head>
<body>
    <form METHOD=POST ACTION="principal.jsp">
        <ul class="ul_login">
            <li> 
                <span class="login"> Login: </span> 
                <input type="text" size="20" name="usuario" minlength="3" maxlength="10"> 
            </li>
            <li> 
                <span class="login"> Senha: </span> 
                <input type="password" size="20" name="senha" minlength="3" maxlength="10"> 
            </li>
            <li>
                </span> <input type="submit"  name="login" value="" class="botao">
            </li>
        </ul>
    </form>
    <div id="ïnvalido"> ${erro} </div>
</body>

And here is my login servlet, which is mapped to a page called principal.jsp in the web.xml file:

public class LoginServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {

        UsuarioBean usuario = new UsuarioBean(request.getParameter("usuario"), 
                                              request.getParameter("senha"));

        try {
            UsuarioDAO userDao = new UsuarioDAO();
            Integer permissao = userDao.login(usuario);
            if (permissao == 0) {
                HttpSession sessao = request.getSession(true);
                sessao.setAttribute("usuarioCorrente", usuario.getNome());
                String destino = "/WEB-INF/jsp/paginaPrincipal.jsp";
                RequestDispatcher dispatcher = request.getRequestDispatcher(destino);
                dispatcher.forward(request, response);
            } else {
                request.setAttribute("erro", "User is invalid");
                RequestDispatcher dispatcher = request.getRequestDispatcher("login.jsp"); 
                dispatcher.forward(request, response);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
xeko
  • 67
  • 1
  • 9
  • I'm not sure what you mean--why can't you use EL? Why can't you redirect if you need to change the URL? – Dave Newton Nov 23 '11 at 03:34
  • The problem is that if i redirect, i cant set the attribute 'erro' to display the message. Or am i wrong? – xeko Nov 23 '11 at 03:42
  • Not as a request attribute, but you can use the session scope and remove it after the first request ("flash" scope). But the login servlet shouldn't need to redirect on login error--it should redirect on login *success*, implementing the post-redirect-get pattern and avoiding weirdness if the user hits `refresh` after logging in. – Dave Newton Nov 23 '11 at 03:44
  • You are almost answering my question. I just need to know how i can check if it is the first request or not, so i can remove the attribute. And how can i alter the div tag if i dont redirect? – xeko Nov 23 '11 at 06:01
  • @DᴀᴠᴇNᴇᴡᴛᴏɴ I followed your suggestions concerning the post-redirect-get pattern on log in and it worked, but i still have the same problem in another servlet. I want to display a message when i persist some information based on form submission, but i can neither avoid the url weirdness nor remove the attribute properly. – xeko Dec 02 '11 at 18:48

0 Answers0