• Post Reply Bookmark Topic Watch Topic
  • New Topic

response.sendRedirect() vs response.getWriter()

 
Buk Lau
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the "normal" way of doing things?:
I have a jsp page that submits to a servlet where the results are tabulated. I then use response.sendRedirect() to display the results in another JSP page using either request.setAttribute() or session.setAttribute().
--
The reason I'm asking is because I've come across posts where people use response.getWriter() to out.println() the results.
Which way is "correct"? I didn't use the response.getWriter() approach because I the display of the results depended upon the results itself and I didn't want to recompile the servlet if i wanted to change the display of results. I'm also unaware of pro's and con's of either approach.
So again, is the approach I took the "normal" way of doing this type of application?
Thanks in advance,
Buk
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65519
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My general rule of thumb: servlets are for processing, JSPs are for formatting views. I avoid emitting HTML in servlets just as much as I try to avoid Java in the JSPs.
 
Perryn Fowler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither
Generally I would avoid writing out directly from the servlet since I would
like to seperate obtaining the information from displaying - that is what JSPs
are for.
On the other hand I wouldn't use a sendRedirect() to redirect to the JSP since
what that actually does is send a response to the client that tells it to redirect its request to another place. This is useful if you want to send them to a different site or if you have some funky refresh issues, but generally you just want to forward the request to the JSP on your server without going back to the client....
in other words I would use RequestDispatcher.forward().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!