24

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
zoozo
  • 253
  • 1
  • 2
  • 10

4 Answers4

46

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.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
15

Instead of using

response.sendRedirect("/demo.jsp");

Which does a permanent redirect to an absolute URL path,

Rather use RequestDispatcher. Example:

RequestDispatcher dispatcher = request.getRequestDispatcher("demo.jsp");
dispatcher.forward(request, response);
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • Thanks for answering. I tried that too but no use. I doesnt work !! – zoozo May 30 '11 at 12:02
  • @zoozo, are you receiving requests from the Android app? If so, can you do a dispatch to `"/demo.jsp"` instead? – Buhake Sindi May 30 '11 at 12:06
  • I am sending an arraylist from the android app. I can get the list in my servlet and can also fetch the values from the list but only this redirection part isn't working. I will try dispatching to `"/demo.jsp"` and will let you know. – zoozo May 30 '11 at 12:12
  • where is demo.jsp placed in your web application? – Buhake Sindi May 31 '11 at 06:48
  • @Buhake : You seem to be a good professional(looking at your score), so I assume you didn't read his question clearly. In his case, he has already sent some responses (response "body" too), and hence it wont be possible to have a permanent redirect with **sendRedirect**. – M-D Jan 20 '13 at 05:48
  • @M-D, if you see clearly, the OP has not posted anything substantial for us to tell exactly why the issue persists. We don't have any stacktrace or HTTP response code whatsoever. The only way is to ask "redundant" questions to see **how** the OP is doing Servlet redirection. – Buhake Sindi Jan 20 '13 at 13:15
-3

Since you already have sent some data,

System.out.println("going to demo.jsp");

you won't be able to send a redirect.

M-D
  • 10,247
  • 9
  • 32
  • 35
-3

You can use this:

response.sendRedirect(String.format("%s%s", request.getContextPath(), "/views/equipment/createEquipment.jsp"));

The last part is your path in your web-app

D.Kanaev
  • 79
  • 1
  • 5