Tuesday 27 March 2007

Servlet Interview Questions 26-50

Posting the second set of 25 questions on servlets. I would update the page with answers in day or 2 keep reading. Have fun.....

Q26: What are the type of protocols supported by HttpServlet?
Ans :
HTTP and HTTPS.

Q27: What is Session ID?
Ans :
A session ID is an unique identification alpha-numeric string, that is shared between the client and the server to maintain the session.

Q28: Why do u use Session Tracking in HttpServlet?
Ans :
To track the user state on server side.

Q29: What are the advantage of Cookies over URL rewriting?
Ans :
Sessions tracking using Cookies are more secure and fast.Url rewriting requires more development effort since we have rewrite each URL to contain the sessionId.

Q30: What is session hijacking?
Ans :
If any client program is able to generate a valid sessionId for a server it can take control of user session. this is a hijacking of some existing session, which belongs to some other client.

Q31: What is Session Migration?
Ans :
Session Migration is a mechanism of moving the session from one server to another in case of server failure or clustering.
This can be done in 2 ways :
a) Saving the session state on database
b) In-momeory Session objects replication to different servers.

Q32: How do we track a user session in Servlets?
Ans :
HttpSession session = request.getSession() returns the current request's session.

Q33: How you can destroy the session in Servlet?
Ans :
session.invalidate(); destroy the current session.

Q34: Difference between forward and redirect?
Ans :
The forward method would dispatch the request to the path specified in getRequestDispatcher(String path). The request is dispatched from one servlet to another on the server side itself, The response will not be sent back to the client, so the client will not know that request has been dispatched to another servlet. All the request parameters are maintained and available in another servlet as well. if the request was in doGet in first servlet, the second servlet's doGet() would be executed, if it was doPost() then doPost() would be executed.
Since the client does not know about this forward on the server side, no history will be stored on the client/browser, so using the back will not take you to first servlet.

An example using forward:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("pathToResource");
rd.forward(request, response);
}

The HttpServletResponse.sendRedirect(String path) method of tell the client to send a new request to the specified path. So the client will send a new request to the server, all previous parameters stored in the request will be unavailable, unless we specify them manually in path. The client's history will be updated so the forward and back buttons will work. This method is useful for redirecting to pages on other servers and domains.

An example using sendRedirect:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("pathToResource");
}

Forward() method is faster than using sendRedirect, becuase no network round trip to the server and back is required.

Q35. What is <load-on-startup> parameter in servlets?

Ans : We provide an integer value in this element. this value tells in what order the servlet's init() method will be executed. If there are multiple servlets the order in which the init is called the the ascending order of this value.

Q36. What if I write <load-on-startup></load-on-startup> for a servlets, I mean no value is provided? Would this give exception? Or it won't intialize the servlets?

Ans:
This is not an error. The servlet would still intialize with the container startup but this servlets would be last to intialize in the intialization sequence.

Q37. What would happen if 2 servlets have same value for <load-on-startup> ?Is this an error? Which one would intialize first?

Ans :
This is not an error. both the servlets would be intialized along with the containner.
The servlet defined first in order in web.xml would intialized first.

Q38. What if I give the same name to 2 servlets <servlet-name> would this be error?
Ans : Yes this is a container start-up time error. The servlet container wont start the webapplication if web.xml has duplication name for 2 or more servlets.

Q39. What is the difference between HttpServlet and GenericServlet?
Ans: Generic servlet represents the servlets are generic level independent of any protocol.
HttpServlet add the additional features of HTTP to the servlets.

Q40. If servlet raised some exception in init() what do think going to happen when it receives request?
Ans: All the request to the servlet are going to get runtime exception.

Q41. I have not defined any doGet() method in servlet, still I call the servlet with GET request what is going to happen? Would I get 404 file not found or I would get exception?
Ans : you get an exception at runtime.

Q42. I defined the servlet only with init () method only, no doGet (), no doPost (), would the servlet compile?
Ans: Yes.

Q43. HttpServletRequest, HttpServletResponse are Interfaces or classes?
Ans:
Interfaces. Only GenericServlet and HttpServlet are classes (they are abstract ones).

Q44. What is Servlet Config?
Ans: This object models/represents the servlet level configurations.

Q45. What is Servlet Context?
Ans:
This object models/represents your application context. The containment in this object are at application level or global level.

Q47. What are Context-Params? Why you would require them?
Ans: The context parameters are also the servlets initialization parameters. The context parameters are available in every servlet. So if some init parametrs is required in multiple servlets we define them at context level as context params.

Q47. Where do I define Context-Params and how I would fetch them in servlets?
Ans:
The Context param are defined within the <web-app> element as follows

<context-param>

<param-name>counter</param-name>
<param-value>100</param-value>
</context-param>


Q48. Can I define a param with multiple values? If not, how can I do that?
Ans: No one init paramtere can have only one string value, if we want multiple values, we can specify say( "," ) comma separted list of values and split them into multiple values in my servlets. If you define the same init parameter multple times the last in the order would be picked by the servlet.

Q49. How would I know the IP Address of the current request?
Ans:
request.getRemoteAddr().

Q50. Can I find out which browser the request is coming from?
Ans: request.getRemoteUser() or request.getRemoteAddr()

0 comments: