Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RequestDispatcher in Servlet

Status
Not open for further replies.

SomnathG

Programmer
Sep 1, 2010
1
IN
Hi,

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 + ",";
}

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?
 
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?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top