式言語とリクエスト ディスパッチャを使用しないと、コンテンツを 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>
これが私のログイン サーブレットで、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();
}
}
}