0

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

user3393577
  • 21
  • 1
  • 3
  • When do you want the message to appear to appear? Immediately after the page renders, or after the user has taken some action on the page? – DaveH Mar 26 '14 at 15:49
  • when user submit the form(using form submission not using ajax) values goes to the servlet and after the operation it send the success or faliure message to the client – user3393577 Mar 26 '14 at 16:07
  • Then I'd do what Siger says below – DaveH Mar 26 '14 at 16:22

2 Answers2

0

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

NickJ
  • 9,380
  • 9
  • 51
  • 74
0

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

<script language="javascript">
   function displayObject(){
     var javascriptVar="${servletObject}";
     alert(javascriptVar);
   }
</script>
</script> 
</head> 
<body onload="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 :

public class YourClassName extends HttpServlet {

  public static final String VUE = "/WEB-INF/yourPageName.jsp";

  public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

     request.setAttribute( "ExampleValue", servletObject );

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

}
Siger
  • 9
  • 2