如何从servlet显示错误消息?

时间:2015-04-24 21:03:11

标签: html5 java-ee

我想在登录和密码错误时显示错误消息,我尝试了这个尝试,但它没有用,谢谢。

的index.html

<body>
<form action="Serv">
<input type="text" name="log">
<input type="password" name="pwd">
<input type="submit" value="send">
</form>
</body>

Serv.java

 response.setContentType("text/html;charset=UTF-8");
 PrintWriter out = response.getWriter(); 
 String u =request.getParameter("log");
 String p =request.getParameter("pwd");
 if(u != "admin"  && p!= "admin"){
 String someMessage = "Error !";
 getServletContext().getRequestDispatcher("/index.html").forward(request, response); 
 out.println("<html><head>");

out.println("<script type=\"text/javascript");
   out.println("alert("+ someMessage +");</script>");
out.println("</head><body></body></html>");
}

2 个答案:

答案 0 :(得分:1)

尽可能在里面使用单引号,试试这个:

String someMessage = "Error !";
out.println("<script type='text/javascript'>");
out.println("alert(" + "'" + someMessage + "'" + ");</script>");
out.println("</head><body></body></html>");

答案 1 :(得分:0)

看起来您没有关闭引号或脚本标记,添加结束引号/尖括号:

out.println("<script type=\"text/javascript\">");