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);
my answer was none of the above.. but the answer is response.sendRedirect("some url"); how could it be.. if the url specify the out of web app.. will it be right?
could anyone justify the answer..
Another question
By Implementing SingleThreadModel interface doesnot ensure Your servlet is threadsafe.. ? How could you justify the above..
For the first question... i have a question in reply!
Consider you are running a real web application. and your context root has been modified say in the same server.
How you tackle the change. I mean How you are gonna inform old users ??? Hope this will answer your first question.
[ March 08, 2007: Message edited by: Srinivasan thoyyeti ]
Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
madhav changala
Ranch Hand
Joined: Dec 20, 2005
Posts: 57
posted
0
which may or may not be a part of the same webapplication
Hi srinivas thanks for your answer, I know response.sendRedirect is one possible answer. (consider url is part of the same webapplication) ..
If it is not part of the same webapplication then,,,, response.sendRedirect will work..?
Regards
Srinivasan thoyyeti
Ranch Hand
Joined: Feb 15, 2007
Posts: 557
posted
0
Hi Madhav,
Within the Same WebServer (say Tomcat, if you have two web applications.)
1) webAppl1 //currently used welcome.jsp
2) webAppl2 welcomeNew.jsp
you want to make used welcomeNew.jsp(webappl2) instead of welcome.jsp(webappl1). it can be done with this: welcome.jsp: <% response.sendRedirect("htp://localhost:8080/webappl2/welcomeNew.jsp"); %>
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