您好我今天开始使用jsp,并且我尝试编写一个简单的登录jsp页面。
现在我正在尝试在没有成功登录时显示提醒。
这是我的jsp页面
<body contenteditable="false">
<p><%= request.getAttribute(Constants.ERROR)%></p>
<script>
if (!$(<%= request.getAttribute(Constants.ERROR)%>).isEmptyObject()) {
alert("Veuillez confirmer mot de passe et le username");
}
</script>
<!-- Static container -->
<div class="login-wrap">
<div class="login-html">
<input id="tab-1" type="radio" name="tab" class="sign-in" checked="" contenteditable="false"><label contenteditable="false" for="tab-1" class="tab">Se connecter </label>
<input id="tab-2" type="radio" name="tab" class="sign-up" contenteditable="false" ><label contenteditable="false" for="tab-2" class="tab">S'inscrire</label>
<div class="login-form">
<div class="sign-in-htm">
<form action="./UserConnect" name="login">
....
</form>
这是我的servlet UserLogin:
@WebServlet(name = "UserConnect", urlPatterns = {"/UserConnect"})
public class UserConnect extends HttpServlet {
@EJB
private UtilisateurFacadeLocal utilisateurFacade;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter(Constants.USERNAME);
String password = request.getParameter(Constants.PASSWORD);
String email ="";
boolean doLogin = utilisateurFacade.doLogin(username, password, email);
System.out.println("do login "+doLogin);
if (doLogin) {
response.sendRedirect("https://www.google.fr");
}else{
request.setAttribute(Constants.ERROR, "login ou mot de passe incorrecte");
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/Login.jsp");
requestDispatcher.forward(request, response);
}
}
`
我的<p>
很好地显示了该消息,但没有提醒我该怎么办?
答案 0 :(得分:1)
用$(document).ready
包装你的jquery函数<script>
$(document).ready(function(){
if (!$(<%= request.getAttribute(Constants.ERROR)%>).isEmptyObject()) {
alert("Veuillez confirmer mot de passe et le username");
}
});
</script>