JSP 页面中如何弹出一个对话框提示用户输入错误?

我在JSP后台的servlet中写了这样一段代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
//执行dao中的查询方法,该方法将数据保存到了dto的Users类中并返回该类的一个对象,所以要使用
//该类的对象来接收数据。
UserDao ud = new UserDao();
Users us = new Users();
us = ud.Inquire(username);
if(us!=null){
if(us.getPassword().equals(password)){
request.getRequestDispatcher("admin/admin.jsp").forward(request, response);
}else{
response.sendRedirect("index.jsp");
}
}else{
JOptionPane.showMessageDialog(this, "用户不存在");
这个位置一直报错,请问JSP 中不能用这个方法弹出对话狂吗?
报错信息为(The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments
(LoginServlet, String))
}
}
我可以直接通过response.sendRedirect("index.jsp");直接跳转到首页,但是我想给用户一个提示.

第1个回答  2013-11-11
用request跳转 并带上一个参数 然后再首页获取这个参数 判断是否为空 不为空则使用js弹出对话框告诉用户出错 为空则正常显示
明白??追问

照你这样做 我还不如直接做一个错误页面,输入错误我就直接response到错误页面去

追答

那你不是要跳转到index.jsp首页 然后在首页提示用户吗?这是你的要求没错吧!

追问

不会在前台弹出对话框,并且for传递request不知道用什么方法带上参数。我有一个新的问题,你去看一下。这个问题就不纠结了

追答

哪儿

本回答被提问者采纳
第2个回答  2013-11-11
JOptionPane.showMessageDialog(this, "用户不存在");

不能在客户端弹出,客户端要使用javascript的alert弹出
相似回答