Your servlet may receive a request which it cannot handle. In such cases, you want to redirect the request to another resource which may or may not be a part of the same webapplication. Which of the following options can be used to achieve this objective? 1. RequestDispatcher rd = this.getServletContext().getRequestDispatcher("some url"); rd.forward(request, response);
1. response.sendRedirect() returns a redirect message to the browser, with a "location" header that specifies url to go to. The url in the location header can be any other url (e.g. "http://www.yahoo.com") or it can be just the change in the protocol (using https instead of http). So, response.sendRedirect() can ask the client browser to any other url also.
2. if your servlet implements single thread model, container ensures that only one thread of a servlet instance will be running the service() method. But this does not ensure safety, as the following examples state:
(A) What if two different servlets access the same session or application level variable? Even if the two different servlets are running in seperate threads, BOTH THE THREADS HAVE ACCESS TO THE ServletContext OBJECT!!!
(B) Declare a servlet class MyServlet, and let it have a static member myStatic. Now, declare two servlet elements in the DD, such that both servlet elements use the MyServlet class. In this case, container will create 2 instances of MyServlet class, one for each servlet element in the DD. Even though these two servlets are running in seperate threads, still they can simultaneously access the common static member myStatic, as it is at the class level