Skip to content Skip to sidebar Skip to footer

Display A Message Coming From Servlet On Jsp Page In Javascript Alert Box

Can anyone tell me a way to display a message in javascript alert box,that message is comming from a servlet.Please help.

Solution 1:

You need AJAX. Javascript will send a request to the servlet, retrieve the message from the servlet response, and then display it in the alert box.

Various tutorials on the web, including this one

Solution 2:

You can store servlet variables in javascript variables, i.e. (JSP) :

<scriptlanguage="javascript">functiondisplayObject(){
     var javascriptVar="${servletObject}";
     alert(javascriptVar);
   }
</script></script></head><bodyonload="displayObject()"></body></html>

To be able to use servletObject, you first need to put it in the server as an attribute in your doGet or doPost function :

publicclassYourClassNameextendsHttpServlet {

  publicstaticfinalStringVUE="/WEB-INF/yourPageName.jsp";

  publicvoiddoGet( HttpServletRequest request, HttpServletResponse response )throws ServletException, IOException {

     request.setAttribute( "ExampleValue", servletObject );

     this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
   }

}

Post a Comment for "Display A Message Coming From Servlet On Jsp Page In Javascript Alert Box"