java后台向前台输出弹出框

实现方法分为两种:


一、直接在后台打印js对话框,原理如下:


    response.setContentType("text/html; charset=UTF-8"); //转码
    PrintWriter out = response.getWriter();
    out.flush();
    out.println("<script>");
    out.println("alert('此用户名已存在,请重新输入!');");
    out.println("history.back();");
    out.println("</script>");
    return mapping.findForward("");


二、在后台存放一个变量message,其值就是弹出的对话框中的内容,在前台接收这个变量,首先要在前台页面放一个隐藏域,第一次访问该页面时隐藏域的值为空,这里就需要用onload调用js弹出对话框,将后台的内容以对话框的形式弹出,如下:


后台:


request.setAttribute("message", "此用户名不存在,请确认后再输入!");


前台:


<script type="text/javascript">
    function checkForm(){
     var flag=true;
     var message=form1.message.value;
     if(message!='null'){
      alert(message);
      flag=false;
     }
     return flag;
    }


</script>


 <body class="backgdcolor" onload="checkForm()">


<input type="hidden" name="message" value="<%=request.getAttribute("message") %>">


</body>


出处:http://blog.163.com/yurong_1987@126/blog/static/4751786320092711596354/

猜你喜欢

转载自blog.csdn.net/m0_37879526/article/details/79880167