-1

In this I'm performing the validation in servlet to check whether the user already exists or not, I'm entering the username in JSP then the value is passed to the servlets.

I have used the following code in servlet for redirecting to JSP.

"request.setAttribute("errorMessage",true);"

and in JSP I used the following code to retrieve the attribute

<%
if(request.getAttribute("errorMessage") != null ){
%>

<%
}
%>  

Inside the loop I have to call the JavaScript to show alert in the JSP.

Can anybody help me to process further. Thanks in advance

informatik01
  • 16,038
  • 10
  • 74
  • 104
Vivek T.K.
  • 23
  • 4

2 Answers2

0

Well you can do it like this

<%
 String err_msg=request.getParameter("errorMessage");

     if(err_msg!= null ){
 %>
  <script type="text/javascript" >
      alert("Error:"+<%=err_msg %>);

   </script>
<%
  }
%>

Happy Coding :)

dreamweiver
  • 6,002
  • 2
  • 24
  • 39
  • I Suppose you verify the spelling of script tag. as far as i know it should work as per this SO http://stackoverflow.com/questions/9617676/javascript-inside-java-jsp-code – dreamweiver Jun 01 '13 at 07:05
0

First thing is using scriptlet in jsp is not encouraged. Use EL or JSTL instead.

Instead of sending a request to the servlet with user data and checking if the user exists and sending back the response is not so dynamic approach, use Ajax for asynchronous calls it's more dynamic and you can display the error message with out redirecting to other page.

MaheshVarma
  • 2,081
  • 7
  • 35
  • 58