This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a servlet that gets data from some html and tries to add it to a database. Obviously, the add either succeeds or fails. Once the database update is done, I need to call a JSP to tell the user what happened. Murach's says just use RequestDispatcher.forward(request, response). Makes sense except the request and response are unaware of the results of the database insert. How do I pass that along to the JSP also?
ms
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use HttpServletRequest.setAttribute() to add the result to the request, say as a Boolean; the JSP can fetch it with <jsp:useBean>.
 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the advantages and disadvantages of that approach over using the session? I thought of using the session after my original posting...
ms
 
Jay Dellinger
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It really just depends on what parts of your code need to know about the success or failure of your servlet actions. If you only need to include the results on the JSP, put use request.setAttribute(...). If subsequent calls will also need to know about the transaction result then put it in the session. In general, use the narrowest scope that you can. Most likely, the request object will work fine for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic