JAVA servlets - open message popup

前端 未结 4 478
名媛妹妹
名媛妹妹 2021-01-13 15:13

I want to user HttpServletResponse object to compose a response that will tell the browser client to open a popup with some message - how can i do that?

相关标签:
4条回答
  • 2021-01-13 15:42

    Basically, you cannot do that directly. You must send in response some code (probably HTML and JS) which will instruct client browser to show message window, eg

    String someMessage = "Error !";
    PrintWriter out = response.getWriter();
    out.print("<html><head>");
    out.print("<script type=\"text/javascript\">alert(" + someMessage + ");</script>");
    out.print("</head><body></body></html>");
    
    0 讨论(0)
  • 2021-01-13 15:50

    Every Servlet response is basically an Http doc/snippet. So you could return a call to a javascript function that will be executed on the client side. The function can be passed in that Servlet response or it can be pre-included in the .js file.

    just an example:

    //servlet code
    PrintWriter out = response.getWriter();  
    response.setContentType("text/html");  
    out.println("<script type=\"text/javascript\">");  
    out.println("alert('deadbeef');");  
    out.println("</script>");
    
    0 讨论(0)
  • 2021-01-13 16:04

    Add to HttpServletResponse some Javascript code that will open a popup, something like

    <script type="text/javascript">
    function popupWindow() {
        window.open( "someLinkToBePoppedUp" )
    }
    </script>
    
    0 讨论(0)
  • 2021-01-13 16:04

    Generally speaking, you can't.

    Thanks to their popularity for annoying adverts, most browsers reject attempts to open popups that aren't a response to something the user does within a page.

    If you just want to display messaging, you could just include it in a page, or output a script element with an alert statement in it.

    0 讨论(0)
提交回复
热议问题