| | | | Question Answer Tutorial - JAVA->SERVLET | | | | Question: 11.What is difference between ServletConfig and ServletContext?
| | Answer: Some of the differences between ServletConfig and ServletContext& nbsp;are:
/>1.) ServletConfig is a unique object per servlet whereas&n bsp;ServletContext is a unique object for complete application /> />2.) ServletConfig is used to provide init parameters to&nb sp;the servlet whereas ServletContext is used />to provide application level init parameters that all othe r servlets can use
/>3.) We can’t set attributes in ServletConfig object wherea s we can set attributes in ServletContext />that other servlets can use in their implementation
| | | | | | | | Question: 12.What is Request Dispatcher?
| | Answer: RequestDispatcher interface is used to forward the request t o another resource that can be HTML, JSP />or another servlet in same application. We can also u se this to include the content of another />resource to the response. This interface is used for inter-servlet communication in the same context
/>There are two methods defined in this interface:
/>1.) void forward(ServletRequest request, ServletResponse response) – forwards the request from a />servlet to another resource (servlet, JSP file, or HTML&nb sp;file) on the server
/>2.) void include(ServletRequest request, ServletResponse response) – includes the content of a />resource (servlet, JSP page, HTML file) in the response /> />We can get RequestDispatcher in a servlet using ServletCon text getRequestDispatcher(String path) />method. The path must begin with a / and is inte rpreted as relative to the current context root
| | | | | | | | Question: What is difference between PrintWriter and ServletOutputStream?
| | Answer: PrintWriter is a character-stream class whereas ServletOutputStream&nb sp;is a byte-stream class. We can />use PrintWriter to write character based information such as character array and String to the />response whereas we can use ServletOutputStream to write b yte array data to the response.
/>We can use ServletResponse getWriter() to get the PrintWri ter instance whereas we can use />ServletResponse getOutputStream() method to get the ServletOutputStr eam object reference
| | | | | | | | Question: Can we get PrintWriter and ServletOutputStream both in a servlet? | | Answer: We can’t get instances of both PrintWriter and ServletOutput Stream in a single servlet method, if we />invoke both the methods; getWriter() and getOutputStream() on&n bsp;response; we will get />java.lang.IllegalStateException at runtime with message as other&nbs p;method has already been called for this response
| | | | | | | | Question: How can we create deadlock situation in servlet?
| | Answer: We can create deadlock in servlet by making a loop  ;of method invocation, just call doPost() method />from doGet() method and doGet() method to doPost() method& nbsp;to create deadlock situation in servlet.
| | | | | | | | | | | | |