0

如果不使用表达式语言和请求调度程序,我想不出一种将内容发送到 JSP 元素的方法。问题是我无法使用 forward 方法更改浏览器 URL,这使得我的应用程序在用户看来很奇怪。那么如何<div>在不使用 EL 和/或请求调度程序的情况下将消息添加到 JSP 页面中的标记?

这是我的login.jsp页面:

<%@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>

这是我的登录 servlet,它映射到 web.xml 文件中名为 principal.jsp 的页面:

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();
        }
    }
}
4

0 回答 0