J2EE - Interview Questions and Answers on Servlets / JSP

Q1.  What is servlet Chaining ?

Ans. Multiple servlets serving the request in chain.

Q2.  Can we have multiple servlets in a web application and How can we do that ?

Ans. Yes by making entries in web.xml

Q3.  What is a config Object? 

Ans. The config object is an instantiation of javax.servlet.ServletConfig and is a direct wrapper around the ServletConfig object for the generated servlet. This object allows the JSP programmer access to the Servlet or JSP engine initialization parameters such as the paths or file location.

Q4.  What is a pageContext Object? 

Ans. The pageContext object is an instance of a javax.servlet.jsp.PageContext object. The pageContext object is used to represent the entire JSP page. This object stores references to the request and response objects for each request. The application, config, session, and out objects are derived by accessing attributes of this object.The pageContext object also contains information about the directives issued to the JSP page, including the buffering information, the errorPageURL, and page scope.

Q5.  Difference between socket and servlet ?

Ans. servlet is a small, server-resident program that typically runs automatically in response to user input. 
A network socket is an endpoint of an inter-process communication flow across a computer network. 

We can think of it as a difference between door and gate. They are similar as they both are entry points but they are different as they are put up at different areas.

Sockets are for low-level network communication whereas Servlets are for implementing websites and web services

Q6.  What are the phases of the JSP life cycle ?


Translation of JSP Page

Compilation of JSP Page

Classloading (class file is loaded by the classloader)

Instantiation (Object of the Generated Servlet is created).

Initialization ( jspInit() method is invoked by the container).

Reqeust processing ( _jspService() method is invoked by the container).

Destroy ( jspDestroy() method is invoked by the container).

Q7.  What are JSP directives ? What are different types of directives ?

Ans. The jsp directives are messages that tells the web container how to translate a JSP page into the corresponding servlet.

There are three types of directives -

• page directive
• include directive
• taglib directive

Q8.  What is session tracking and how do you track a user session in servlets?

Ans. Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:

User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password

Hidden form fields - fields are added to an HTML form that are not displayed in the client's browser. When the form containing the fields is submitted, the fields are sent back to the server

URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.

Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.

HttpSession- places a limit on the number of sessions that can exist in memory. 

Q9.  What are advantages of using Servlets over CGI ?

Ans. Better Performance as Servlets doesn't require a separate process for a single request.

Servlets are platform independent as they are written in Java.

Q10.  What is the use of HTTPSession in relation to http protocol ?

Ans. http protocol on its own is stateless. So it helps in identifying the relationship between multiple stateless request as they come from a single source.

Q11.  Why using cookie to store session info is a better idea than just using session info in the request ?

Ans. Session info in the request can be intercepted and hence a vulnerability. Cookie can be read and write  by respective domain only and make sure that right session information is being passed by the client.

Q12.  What are different types of cookies ?

Ans. Session cookies , which are deleted once the session is over.

Permanent cookies , which stays at client PC even if the session is disconnected.

Q13.  http protocol is by default ... ?

Ans. stateless