By using forward() method of _________ object, the called resource will be able to access the original request object.
AHttpServletRequest
BHttpServletResponse
CRequestDispatcher
DServletConfig
RequestDispatcher.forward() passes the same request object to the called resource.
2.
★ Final term 2012
Why we use RequestDispatcher to forward a request to another resource(servlet, html, jsp), instead of using a sendRedirect()?
AsendRedirect() is not supported in servlet API.
BRequestDispatcher is not supported in servlet API.
CRequestDispatcher is efficient and allows the server to maintain request status.
DNone of these
Forwarding needs no round trip to the client and keeps the same request object.
3.
★ Final term past paper
By using sendRedirect() method of ________ object, a new request is generated which redirects the user to called resource.
AServletConfig
BServletContext
CHttpServletRequest
DHttpServletResponse
sendRedirect() is a method of HttpServletResponse.
4.
★ Final term past paper
By ________, the original request can be forwarded to the next servlet.
Aboth request dispatching and response redirection
BNone of the given options
Crequest dispatching
Dresponse redirection
RequestDispatcher.forward() passes the same request on; sendRedirect() makes the browser send a new one.
5.
★ Final term past paper
Which one of the following is not a request dispatching method?
Aforward( )
Binclude( )
CBoth forward( ) and include( )
DNone of the given options
forward() and include() are both RequestDispatcher methods, so none of the options is excluded.
6.
★ Final term past paper
In which of the following format does the key in request Dispatching is stored?
AInteger
BDouble
CString
DChar
request.setAttribute(String name, Object value) stores the attribute under a String key.
No MCQs match this filter.Is filter mein koi MCQ nahi.
1.
★ Final term July 2012Lecture 30
What is the difference between Request Dispatcher forward , and Request dispatcher Include?
Show answerJawab dekhein
forward(): passes the request completely to another resource (servlet, JSP or HTML) in the same context, and that resource creates the whole response. It is transparent to the client (URL does not change), the request object stays available, headers can be set before forwarding but no output may be written before it.
include(): inserts the output of another resource into the current response and then control returns to the first servlet. Data can be written before and after the include, and the first servlet finishes the response.
2.
★ Final term July 2012Lecture 302 marks
What are two ways of request dispatching? Explain.
Show answerJawab dekhein
Using a RequestDispatcher (from request.getRequestDispatcher() or the ServletContext):
1. forward(request, response): sends the request to another resource, which generates the whole response; transparent to the client.
2. include(request, response): includes the output of another resource in the current response, then control returns to the calling servlet.
3.
★ Final term 2012Lecture 303 marks
What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?
Show answerJawab dekhein
ServletRequest.getRequestDispatcher() accepts a relative path (relative to the current request’s path) as well as a path starting with "/". ServletContext.getRequestDispatcher() accepts only a path that begins with "/", interpreted relative to the context root of the web application.
4.
★ Final term 2012Lecture 30
Difference of forward and send redirect.
Show answerJawab dekhein
forward() (RequestDispatcher) passes the same request and response to another resource on the server. It happens inside the web container, the client is not informed, the URL in the browser does not change and request attributes remain available.
sendRedirect() (HttpServletResponse) sends a response telling the browser to request a new URL. The browser makes a completely new request, the URL changes, request attributes are lost, it can point to another server, and it is slower because of the extra round trip.
5.
★ Final term Spring 2011Lecture 302 marks
Request Dispatching methods.
Show answerJawab dekhein
Request dispatching lets a servlet forward a request to another resource or include another resource's output. A RequestDispatcher is obtained with getRequestDispatcher(path), and it has two methods: forward(request, response) and include(request, response).
6.
★ Final term Spring 2011Lecture 302 marks
What is the difference between getRequestDispatcher(String path) method using javax.servlet.ServletRequest or using ServletContext?
Show answerJawab dekhein
The getRequestDispatcher(String path) method of ServletRequest accepts a relative path, interpreted relative to the current request; a path starting with "/" is relative to the context root. The method of ServletContext does not accept relative paths: the path must start with "/" and is interpreted relative to the context root.
7.
★ Final term past paperLecture 303 marks
Write a statement to call an error page along with the "source not found" message using request dispatching technique.
Show answerJawab dekhein
request.setAttribute("msg", "source not found");
RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
rd.forward(request, response);
The error page reads the message with request.getAttribute("msg").
8.
★ Final term past paperLecture 302 marks
Request of one page can be dispatched to other page using two methods. Write the code syntax of both methods.
Show answerJawab dekhein
Both use a RequestDispatcher:
RequestDispatcher rd = request.getRequestDispatcher("next.jsp");
rd.forward(request, response); // forward: control passes fully to next.jsp
RequestDispatcher rd = request.getRequestDispatcher("header.jsp");
rd.include(request, response); // include: output of header.jsp is added, then control returns
1.
★ Final term 2014Lecture 305 marks
Explain the "include" method of "Request Dispatcher" with the help of diagram.
Show answerJawab dekhein
RequestDispatcher.include(request, response) lets a servlet include the output of another resource (servlet, JSP or HTML) in its own response. Flow: Client -> Servlet1 (writes part of the response) -> include -> Servlet2/JSP (adds its output to the same response) -> control returns to Servlet1 -> Servlet1 writes the rest and finishes -> response to Client. Data can be written before and after the include, the included resource cannot change status codes or headers, and the first servlet finishes the response. Code:
RequestDispatcher rd = request.getRequestDispatcher("second");
rd.include(request, response);
2.
★ Final term 2012Lecture 305 marks
Request dispatcher’s include method?
Show answerJawab dekhein
include(request, response) of RequestDispatcher lets a servlet include the output of another resource (servlet, JSP or HTML) in its own response. Flow: the user sends a request to Servlet1; Servlet1 gets a RequestDispatcher for Servlet2 and calls include(request, response); Servlet2 writes its output, and control returns to Servlet1, which can add more content and finally sends the complete response to the user.
Differences from forward(): data can be written to the response before and after an include, and the first servlet that received the request is the one that finishes the response.
Example:
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.include(request, response);
3.
★ Final term Spring 2011Lecture 305 marks
Include method of dispatcher
Show answerJawab dekhein
include(request, response) of RequestDispatcher lets a servlet include the output of another resource (servlet, JSP or HTML) in its own response. Flow: the user sends a request to Servlet1; Servlet1 gets a RequestDispatcher for Servlet2 and calls include(request, response); Servlet2 writes its output, and control returns to Servlet1, which can add more content and finally sends the complete response to the user.
Differences from forward(): data can be written to the response before and after an include, and the first servlet that received the request is the one that finishes the response.
Example:
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.include(request, response);