Vasu
Vasu

Reputation: 365

can't we have alert() before the sendRedirect in jsp file

i want to know how to implement a alert before i redirect from one page to other page.

my code : ( sciptlet )

  <% 
       if(condition)
        {
            // alert here. before redirecting other page.
            // now redirect here as fallow.
             response.sendRedirect("sem-duration_old.jsp");

           }
   %>

i tried this its not working :

   <% 
       if(condition)
        {   
    %>
              <script>
              alert(" we are going to some other page" );
            </script> 
    <%               
            response.sendRedirect("semduration_old.jsp");

           }
   %>

Upvotes: 1

Views: 5053

Answers (3)

Amanuel Nega
Amanuel Nega

Reputation: 1977

You can also create another jsp page that messages and redirects.

<% 
 String msg = request.getParameter("msg");//means you have to send the message as a parameter
 String location = request.getParameter("location");
%>
<script>
      alert("<%= msg %>");//alert the message
      //you can also display the message using a mark up on the page and use setTimeOut()
      window.location = "<%= location %>";//then redirect
</script>

Upvotes: 2

Tap
Tap

Reputation: 6522

Remember the separation between client-side and server-side. The redirect signal (an HTTP 302 response) is sent by your jsp, then received by the browser. So regardless of what else you've written to the response buffer (html markup, script tag, and alert) your browser is going to immediately request the Location specified in the redirect. So your jsp's response will resemble the following:

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1    
Location: http://localhost/semduration_old.jsp
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 0

One way to accomplish what you want would be to allow the client to "redirect" themselves using javascript:

<% 
    if(condition)
    {   
%>
     <script>
          alert(" we are going to some other page" );
          window.location = 'semduration_old.jsp';
     </script> 
<%
       }
%>

Upvotes: 4

Vinoth Krishnan
Vinoth Krishnan

Reputation: 2949

May be this will help you.

<% 
   if(condition)
    { %>
    <script type="text/javascript">
         alert('redirecting');
    </script>
        // alert here. before redirecting other page.
        // now redirect here as fallow.
    <%     response.sendRedirect("sem-duration_old.jsp?current_session");

       }
 %>

I haven't tried. But this may not be optimal solution. Have you tried unload function?

Upvotes: 0

Related Questions