我无法找到一种在不使用表达式语言和请求调度程序的情况下将内容发送到JSP元素的方法。问题是我无法使用forward方法更改浏览器URL,这使得我的应用程序对用户来说似乎很奇怪。那么如何在不使用EL和/或请求调度程序的情况下向JSP页面中的<div>
标记添加消息?
这是我的 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();
}
}
}