Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

response.sendRedirect() from Servlet to JSP does not seem to work

Tags:

I am writing a client server program. I am sending an arraylist from an android phone and I am able to receive the list also. After that I want the servlet to redirect to demo.jsp using response.sendRedirect(), but it just won't redirect. Tried with requestDispatcher.forward() too.

ObjectInputStream in = new ObjectInputStream((InputStream) request.getInputStream()); List<Double> al=(List<Double>)in.readObject(); in.close(); for(int x=0;x<al.size();x++) {     System.out.println("List");     System.out.println(al.get(x)); } System.out.println("going to demo.jsp"); response.sendRedirect("demo.jsp"); 

How is this caused and how can I solve it?

like image 540
zoozo Avatar asked May 30 '11 11:05

zoozo


People also ask

What is response sendRedirect in jsp?

sendRedirect() accepts the respective URL to which the request is to be redirected. Can redirect the request to another resource like Servlet, HTML page, or JSP page that are inside or outside the server. It works on the HTTP response object and always sends a new request for the object.

Is it possible to call sendRedirect Once the response is committed?

you can't call sendRedirect(), after you have already used forward().

What is the difference between sendRedirect () and RequestDispatcher?

The RequestDispatcher interface allows you to do a server side forward/include whereas sendRedirect() does a client side redirect. SendRedirect() will search the content between the servers. it is slow because it has to intimate the browser by sending the URL of the content.


1 Answers

I'm posting this answer because the one with the most votes led me astray. To redirect from a servlet, you simply do this:

response.sendRedirect("simpleList.do") 

In this particular question, I think @M-D is correctly explaining why the asker is having his problem, but since this is the first result on google when you search for "Redirect from Servlet" I think it's important to have an answer that helps most people, not just the original asker.

like image 97
Daniel Kaplan Avatar answered Sep 20 '22 18:09

Daniel Kaplan