Servlet 页面跳转 并弹出提示对话框

来源:互联网 发布:太乙神数排盘软件 编辑:程序博客网 时间:2024/04/26 22:00
<pre name="code" class="html">简单的登录验证模块点击登录进入相应的Servlet,Servlet调用bean验证用户名是否存在如果存在则登录成功,跳转如果不存在则弹出错误对话框,并回到登录页面开始用的下面两句话out.print("<script language='javascript'>alert('the name doesnot exit')</script>");response.sendRedirect("Login.jsp");但不会显示对话框,而是直接回到Login.jsp后来改用JOptionPane.showMessageDialog(null, "name doesnot exits");response.sendRedirect("Login.jsp");则可以实现。


@Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");     String username=request.getParameter("username");    String password=request.getParameter("password");    String code=request.getParameter("code");    //状态码(0:默认值(用户未登入成功),1:用户登入成功,2:验证码通过)    String message="0";    boolean target=false;    if(username.equals("")){    username="0";}    if(password.equals("")){    password="0";    }        HttpSession sessions = request.getSession();        if(sessions.getAttribute("safecode")!=null ){    target=true;    message="2";    }        if(target){     String sql="select * from userinfo info where info.`username`='"+username.trim()+"' and info.`password`='"+password.trim()+"'";    try {ResultSet rusultset=DataBase.select(sql);while(rusultset.next()){message="1";HttpSession session = request.getSession();session.setAttribute("username",username);}DataBase.closeAll(rusultset);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}        }       <span style="color:#ff0000;">    response.setContentType("text/;html");        PrintWriter out=response.getWriter();    //登入信息返回    if(message.equals("0")){//默认    JOptionPane.showMessageDialog(null, "用户名或密码不正确");  response.sendRedirect("./login.html");     }else if(message.equals("1")){//成功  response.sendRedirect("./message.html");    }else if(message.equals("2")){//用户名已经存在  JOptionPane.showMessageDialog(null, "注册码不正确");  response.sendRedirect("./login.html");    }else{//特殊情况  JOptionPane.showMessageDialog(null, "用户名或密码不正确");  response.sendRedirect("./login.html");     }        out.flush();        out.close();</span>        }@Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubthis.doGet(request, response);}

0 0
原创粉丝点击