response.sendRedirect() from Servlet to JSP does not seem to work
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?
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.
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);
Since you already have sent some data,
System.out.println("going to demo.jsp");
you won't be able to send a redirect.
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
精彩评论