Tuesday 27 March 2007

Servlet Interview Questions 51-75

Q51. What is the difference between ServletContext and ServletConfig?
Ans: The servlet context is a single object for the entire application withing the container where as servlet config represents the single servlet configurations i.e. each servlet instance has its own servlet config.
If we want some configuration parameter at application level, they need to be set in Servlet context , however if we want a configuration parameter for a particular servlet, that should go to servlet config.
We should keep always in mind that servlet context objects are shared across the application and all the requests have the access to them. so these objects should be readonly as far as possible to avoid any potential synchronization problems. If we need to modify these objects they must be ensured be modified under sync blocks. though doing such things are never my favourites.

Q52. What are the common mechanisms used for session tracking?

Ans: There are 3 fundamental ways for session trackings. The primary requirement for session tracking is the server should be able to uniqly identify different browsers and the series of requests from them. The mechanism server relies upon in this case is the sessionID. In lay man's language. On recieving the first request from a client(browser) The server gives the client a unique Id (we call it sessionID) . Server expects the client to circulate this ID with all the subsequent requests and How does client ciculates this Id? There are 3 different ways :)
1) Cookies : client stores The session Id in a cookie called jsessionID and every request takes the valid cookies automatically. So sessionId is automatically sent to the server with each request and session is maintained.

2) URL Rewriting : what if cookies are disabled? so client won't be able be store the sessionId in a cookie. So first opion does not seems feasible and still we need to make sure "every request should carry the sessionId if client has the one". We know every request has URL agree? what if we append the sessionId somehow in each url from Client? this is what this technique is, but its not as straight forward as the first one where we need not do anything. In url rewriting the view writer has to make sure each and every link on the page carries the sessionId to the server.

3) Hidden form fields: What if I dont want to rely on the default session management behavoiur of the server and browser. what I can do is, I write my own piece of server side programs to create/manage the sessions and for all my applications I need a sessionId request parameter mechanism to share the sessionId with the browser and server. Incase of links, I would send the sessionId in query strings and in case of forms I would define a hidden field which would carry the session Id on submit. This mechanism is called Hidden form Fields.


Q53 What is the difference between the RequestDispatcher from Request and ServletContext?
Ans: The answer lies in the url-patterns. Simplest answer is the request dispatcher obtained from servlet context can dispatch the request anywhere in the application i.e. to any servlet/jsp/page. howerver the rd from request is not capable of doing so. its can only forward the request to resources which have the same base urls.
i.e. lets us say we have following servlets /bill/s1, /bill/s2, /bill/reciept/s3, /shop/a1, /shop/a2 now if we have request dispatcher from request then i can dispatch the request from s1 to s2, s3. However you can not dispatch the request from s3 to s1 or s2
you can not dispatch the request from s1 to a1 or a2 the reason being the base urls are different.

Q54. What is the difference between sendRedirect and Forward?
Ans: The forward is a request dispatch on server side itself and in sendRedirect from s1 to s2 takes place something like this. request processing for s1 halts where we say sendRedirect(s2) and server responds with the response code 302 (saying please send a new request to s2) and browser sends the new request to s2. i.e. It involves a new roundtrip.
The request in case of forward does not loose its identity. i.e. all the request parameters/objects avaliable in s1 are also available in s2. In case of sendRedirect the request to s2 is etirely a fresh request so earlier identity is lost.

Q55. If we use sendRedirect does the session expires?
Ans: This question is to confuse you. It has nothing to do with the session expiry :)
No, The session does not expire in this case. The session expires only in the following cases.

1) Some servlet explicitly expires the session for a request through session.invalidate()

2) The client does not send the request to server for long enough time. which is greater than the session timeout interval.

Q56. How can I pass on an object from servlet S1 to servlet S2 if I am using sendRedirect?
Ans: Use Session object, but make sure guys you clean up the session in S2 so that the object is not just sitting there in luxury and doing nothing after the request for S2 is finished.

Q57. How can I pass on a request parameter from servlet S1 to servlet S2 if I am using sendRedirect?
Ans: we append the request parameter in the Redirect URL as a part of query string.

Q58. Which one is faster sendRedirect or forward?
Ans: Let us not answer it straight away :D why dont you guys judge it, I give you hint.
Forward is a request dispatch on server side i.e. if I want to Forward from s1 to s2. Server takes the request/response from s1 and give to s2.
Redirect is something like when you want to redirect from s1 to s2. request processing for s1 is halted and then server asks the browser to please please send the request to s2 and browser sends altogether a fresh request to s2. i.e. request is routed through the browser?

Now answer please? :)

Q59. If forward is faster then why you would like to use sendRedirect?
Ans: Its a trap :). The one thing forward can not do is it can not forward the request to a different application context i.e. urls outside of your applications.

you also do redirects within your applications too. when you would require to redirect within your applications? you want answer? here we go.... when you don't want to forward :D.

Let us imagine a situation. Let us say you are wrote a servlet (s1) to send an email. you have to send a email when client submits the form and show him the status page (s2). Let us say when you send an email in s1 and then you forward the request to s2 and s2 shows the status page. everything working fine right?

Now let us consider this. I opened the browser and fill the email form and submit it and say becuase my network is slow, I could not see the status page. so i tried refresh 2-3 times. now what your implementation would do is, it would send the emails 3-4 times. do you think I really want to send the email 3-4 times? whats wrong then?

So you need to think such usability issues arising out of implementations as well.

Q60. If I redirect from servlet S1 to servlet S2, which method of S2 get called doGet() or doPost(), why?s

Ans: we were discussing in 58 that browser sends the fresh request to the S2 and you know guys the default method for any cleint to request a URL is GET. So if a redirect request is a GET request which method of S2 would be called? Not able to judge it still then you are in a wrong matrix :)

Q61. If I forward from doPost() method of servlet S1 to servlet S2, which method of S2 get called doGet() or doPost(), why?
Ans: We have discussed in 54 that in forward the request retains its identity. so if s1 recieved POST the s2 would also recieve the POST. so doPost() would be executed.

Q62. What is a query string?
Ans: In case of GET request the request parameters are passed as name value pairs in querystring. e.g. http://localhost:8080/shopping/login?user=neo&password=neo
here the query string is user=neo&password=neo
and we have 2 request parameters here user and password

Q63. Is the request parameter name case sensitive? What I mean is, Let us say request contains parameter "username=bob" if I fetch it like this request.getParameter("UserName"); would I get the value "bob"?
Ans: no its not. yes we would get the value bob.
But I think being a java technologists, for consistency purposes Its better to stick to a case sensitive perceptions. i.e. if we define the form field UserName , we should fetch it as UserName.

Q64. If I fetch a request parameter with a name which is not there in the request…do I get a null value or I get an exception?
Ans: We get the null

Q65. How you would fetch the request parameter which has more than one value?
Ans: String[] request.getParameterValues(String paramName)

Q66. The servlet element must occur before the servlet-mapping element in a web.xml file ?

Q67. The servlet-class element must specify the full package name of the servlet class?

Q68. The servlet configuration can change from request to request True/False?
Ans: Requests for same servlet False
Requests for different servlets True \

Q69. The servlet context can change from request to request True/False?
Ans: False (within the same webapplication :))

Q70. What are the methods defined in Servlet Interface?
Q71. Can you access the client side files from a servlet?
Ans: No servlets always executes on the server side and the underlying protocol for client/server communication is HTTP. which does not allow the access to client filesystems for security reasons. This is only possible through javascript and it works only to a very limited extent and its possible the security setting on browser does not allow the javascript to access any file on client side.

0 comments: