This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Servlets and the fly likes RequestDispatcher issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "RequestDispatcher issue " Watch "RequestDispatcher issue " New topic

RequestDispatcher issue

Hazel Sisson

Joined: Feb 12, 2002
Posts: 17
Hi there!
I'm trying to write a servlet that takes form data, validates it then sends some of that data to a JSP.
The problem is, the servlet uses a printwriter to inform the user of any mistakes they have made filling in the form, therefore I can't use the RequestDispatcher to send data to a JSP due to the output stream having already been used.
Does anyone have any idea how I could get round this?
Thanks very much,
Mayer Salzer

Joined: Apr 01, 2002
Posts: 20
I'm assuming that you're trying to use both the PrintWriter (to report errors) and the RequestDispatcher (to send the rest of the data to the jsp) at the same time. What you can do instead is have some sort of error-page jsp, this way your error message can be sent through the RequestDispatcher as well, and you won't need the PrintWriter at all.
If you're worried that you won't be able to use the RequestDispatcher for more than one jsp, just call the RequestDispatcher's 'include' method. This method is used to call as many jsps (or other servlets) as you need. Each call to this method will keep appending to the bottom of the same output (page your user sees).

Sun Certified Programmer for Java 2 Platform
Hazel Sisson

Joined: Feb 12, 2002
Posts: 17
Great idea, why didn't I think of that?! :roll:
Thanks very much!
I agree. Here's the link:
subject: RequestDispatcher issue
It's not a secret anymore!