Can anyone tell me a way to display a message in javascript alert box,that message is comming from a servlet.Please help.
Asked
Active
Viewed 4,044 times
0
-
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 Answers
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
-
how to get the value to servletObject from the servlet and how to send it to the jsp page which i want to show the alert – user3393577 Mar 26 '14 at 16:12
-
please tell me how to put server as an attribute in doGet or doPost function – user3393577 Mar 26 '14 at 23:43