Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

RequestDispatcher in Servlet

RequestDispatcher in Servlet

RequestDispatcher in Servlet


I am very much new in Java World. I was going through the servlet books and was doing some hands-on. I was trying to RequestDispatcher method to send response data from Controller class to a JSP page. In the controller class I had to set values of two variables as request attributes before calling the request.getRequestDispatcher method. Find the code below:
public class BeerSelectStep3 extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
        String c = request.getParameter("color");
        String[] sizes = request.getParameterValues("sizes");
        String sizeSel = "";
        BeerExpert be = new BeerExpert();
        List result   = be.getBrands(c);
        for(int i = 0; i < sizes.length; i++){
            sizeSel +=  sizes[i] + ",";
        sizeSel = sizeSel.substring(0, sizeSel.length() - 1);
        request.setAttribute("styles", result);
        request.setAttribute("sizeSel", sizeSel);
        RequestDispatcher view = request.getRequestDispatcher("result.jsp");
        view.forward(request, response);

It is not very much clear to me why I have to set request attribute instead of Response attributes before redirecting the data to VIEW(JSP) page. If there is no use of response object in the class then why we are sending that as a second argument in the forward method?    

RE: RequestDispatcher in Servlet

Neither clear to me. Usually when you redirect something you redirect the input, i.e. the request, not the response.

In this case, I guess you want the servlet to populate some data of the response and then let the JSP do the rest of the job, rigth?


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close