GeeCON Prague 2014*
The moose likes Servlets and the fly likes RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher" Watch "RequestDispatcher" New topic
Author

RequestDispatcher

Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
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


ms<br />SCJP, SCJD
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use HttpServletRequest.setAttribute() to add the result to the request, say as a Boolean; the JSP can fetch it with <jsp:useBean>.


[Jess in Action][AskingGoodQuestions]
Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
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

Joined: Sep 19, 2002
Posts: 12
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.
 
GeeCON Prague 2014
 
subject: RequestDispatcher