Reputation: 13547
I have a form in a jsp file, which I have used to get the user details. On clicking the submit button, the form action has been set to another jsp file where the details are inserted into the database. But before that as soon as the user enters the userid, there is a check availability button. On clicking the the button I want the control to go to another jsp page along with the username as a parameter. There I am doing the check availability. I want to return the response to the previous jsp file. Can this be accomplished using the 'request dispatcher'. If yes can someone pls explain the include and forward methods of the request dispatcher. I have tried searching for it on the net. There is only code available. I want to know what this request dispatcher is and how it works.
Upvotes: 0
Views: 1077
Reputation: 1109875
The request dispatcher dispatches the request to the given target. The request dispatcher basically passes the control to the given target. In case of JSPs, the JSP will then work with the given request and send its output to the given response.
The include()
method allows you to continue using the response after the control comes back, so that you can if necessary add some data. The included target is not allowed to manipulate the response headers. The target of an include()
should be a partial of the final response. The forward()
method allows you to pass the control fully to the given target. The forwarded target is allowed to manipulate the response headers. The target of a forward()
should be the entire JSP file itself which you want to present in its full glory (which in turn can however include other JSPs).
In this case, you need to send a forward. Oh, this kind of job doesn't belong in a JSP, but in a Servlet. You may otherwise face IllegalStateException
s when the JSP wherein you're trying to forward the request has already sent some data to the response.
Upvotes: 1