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