How many objects of a servlet is created?

Only one.

What is the life-cycle of a servlet?

  1. Servlet is loaded
  2. servlet is instantiated
  3. servlet is initialized
  4. service the request
  5. servlet is destroyed


What are the life-cycle methods for a servlet?

  1. public void init(ServletConfig config): It is invoked only once when first request comes for the servlet. It is used to initialize the servlet.
  2. public void service(ServletRequest request,ServletResponse)throws ServletException,IOException: It is invoked at each request.The service() method is used to service the request.
  3. public void destroy():It is invoked only once when servlet is unloaded.

Who is responsible to create the object of servlet?

The web container.


When servlet object is created?

At the time of first request.


What is difference between Get and Post method?


GETPOST
1) Limited amount of data can be sent because data is sent in header.Large amount of data can be sent because data is sent in body.
2) Not Secured because data is exposed in URL bar.Secured because data is not exposed in URL bar.
3) Can be bookmarkedCannot be bookmarked
4) IdempotentNon-Idempotent
5) It is more efficient and used than PostIt is less efficient and used
more details...

What is difference between PrintWriter and ServletOutputStream?

PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class.The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.

What is difference between GenericServlet and HttpServlet?

The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific.HttpServlet provides additional functionalities such as state management etc.

What is servlet collaboration?

When one servlet communicates to another servlet, it is known as servlet collaboration.There are many ways of servlet collaboration:
  • RequestDispacher interface
  • sendRedirect() method etc.

What is the purpose of RequestDispatcher Interface?

The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.This interceptor can also be used to include the content of antoher resource.


Can you call a jsp from the servlet?

Yes, one of the way is RequestDispatcher interface for example:
  1. RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");  
  2. rd.forward(request,response);  


What is difference between forward() method of RequestDispatcher and sendRedirect() method ?

forward() methodsendRedirect() method
1) forward() sends the same request to another resource.1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) forward() method works at server side.2) sendRedirect() method works at client side.
3) forward() method works within the server only.3) sendRedirect() method works within and outside the server.



What is difference between ServletConfig and ServletContext?

The container creates an object of ServletConfig for each servlet whereas an object of ServletContext is created for each web application.


What is Session Tracking?

Session simply means a particular interval of time.
Session Tracking is a way to maintain state of an user.Http protocol is a stateless protocol.Each time user requests to the server, server treats the request as the new request.So we need to maintain the state of an user to recognize to particular user.

What are Cookies?

A cookie is a small piece of information that is persisted between the multiple client requests.A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number

What is difference between Cookies and HttpSession?

Cookie works at client side whereas HttpSession works at server side.


What is filter?

A filter is an object that is invoked either at the preprocessing or postprocessing of a request.It is pluggable

How can we perform any action at the time of deploying the project?

With the help of ServletContextListener interface.


What is the disadvantage of using cookies?

It will not work if cookie is disabled from the browser

How can we upload the file to the server using servlet?

One of the way is by MultipartRequest class provided by third party


In web.xml file   <load-on-startup>1</load-on-startup> is defined between <servlet></servlet> tag what does it means.

Answhenever we request for any servlet the servlet container will initialize the servlet and load it which is defined in our config file called web.xml by default it will not initialize when our context is loaded .defining like this <load-on-startup>1</load-on-startup> is also known as pre initialization of servlet means now the servlet for which we have define this tag has been initialized in starting when context is loaded before getting any request.


How can we create deadlock condition on our servlet?

Ans: one simple way to call doPost() method inside doGet() and doGet()method inside doPost() it will create deadlock situation for a servlet.


For initializing a servlet can we use constructor in place of init ().

Ans: No, we can not use constructor for initializing a servlet because for initialization we need an object of servletConfig using this object we get all the parameter which are defined in deployment descriptor for initializing a servlet and in servlet class we have only default constructor according to older version of java so if we want to pass a
Config object we don’t have parametrized constructor and apart from this servlet is loaded and initialized by container so ots a job of container to call the method according to servlet specification they have lifecycle method so init() method is called firstly.

More important Java doesn't allow interfaces to declare constructors.




Why super.init (config) wiil be the first statement inside init(config) method.

Ans: This will be the first statement if we are overriding the init(config ) method by this way we will store the config object for future reference and we can use by getServletConfig ()  to get information about config object if will not do this config object will be lost and we have only one way to get config object because servlet pass config object only in init method . Without doing this if we call the servletConfig method will get NullPointerException.




Can we call destroy() method inside the init() method is yes what will happen?

Ans:Yes we can call like this but  if we have not override this method container will call the default method and nothing will happen.after calling this if any we have override the method then the code written inside is executed.



How can we refresh servlet on client and server side automatically?

Ans: On client side we can use Meta http refresh and server side we can use server push.



How can you get the information about one servlet context in another servlet?

Ans: In context object we can set the attribute which we want on another servlet and we can get that attribute using their name on another servlet.
Context.setAttribute (“name”,” value”)
Context.getAttribute (“name”)


Why we need to implement Single Thread model in case of Servlet.

Ans: In J2EE we can implement our servlet on two different ways either by using:
1. Single Thread Model
2. Multithread Model
Depending upon our scenario, if we have implemented single thread means only one instance is going handle one request at a time no two thread will concurrently execute service method of servlet.
Example in banking account where sensitive data is handle mostly this scenario was used this interface is deprecated in Servlet API version 2.4.

As the name signifies multi thread means a servlet is capable to handle multiple requests at same time. This servlet interview question was quite popular few years back on entry level but now its loosing its shine.

what is servlet collaboration?
Ans communication between two servlet is called servlet collaboration which is achieved by 3 ways.
1. RequestDispatchers include () and forward() method .
2. Using sendRedirect()method of Response object.
3. Using servlet Context methods



What is the difference between ServletConfig and ServletContext?

Ans: ServletConfig as the name implies provide the information about configuration of a servlet which is defined inside the web.xml file or we can say deployment descriptor.its a specific object for each servlet.

ServletContext is application specific object which is shared by all the servlet belongs to one application in one JVM .this is single object which represent our application and all the servlet access application specific data using this object.servlet also use their method to communicate with container.


When should you prefer to use doGet() over doPost()?

GET is preferred over POST in most of the situations except for the following:

- When the data is sensitive.
- When the data is greater than 1024 characters

What is lazy loading?

The servlets are not initialized by the container from the start. It happens when the servlet is requested for the first time. This is called lazy loading.

What are the types of Session Tracking ?

Following are the popular ways of session tracking:

a.) URL rewriting: In this method of session tracking, some extra data is appended at the end of the URL, which identifies the session. This method
is used for those browsers which do not support cookies or when the cookies are disabled by the user.

b.) Hidden Form Fields: This method is similar to URL rewriting. New hidden fields are embedded by the server in every dynamically generated form
page for the client. When the form is submitted to the server the hidden fields identify the client.

c.) Cookies: Cookie refers to the small amount of information sent by a servlet to a Web browser. Browser saves this information and sends it back to the server when requested next. Its value helps in uniquely identifying a client.

d.) Secure Socket Layer (SSL) Sessions

What are the disadvantages of storing session state in cookies?

- Using a cookie, all the session data is stored with the client. If the cookies at client side get corrupt, purged or expired, the information received won't be complete.

- Some user may disable the cookies or their browser might not support them. Some users might have a firewall filtering out the cookies. So, you may either not receive the information or trying to switch to an alternate means may cause complexity.

- Cookie based solutions work only for HTTP clients.

- A low-level API controls the cookies. It is quite difficult to implement them.

Define HTTP Tunneling?

AnswerIn some organizations, the intranet is blocked by a firewall to the internet. It is exposed to the outer networks only by means of webserver port that accept only Http requests. In such situations, if protocols other than http are used, then they get rejected. The solution is to have them encapsulated in http or https and sent as an HttpRequest. Thus, masking other protocols as http requests is called HTTP Tunneling.


What is the difference between using getSession(true) and getSession(false) methods?

AnswergetSession(true) will check whether a session already exists for the user. If yes, it will return that session object else it will create a new session object and return it.
getSession(false) will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.


Explain session tracking in Java Servlet. 

The state of requests for the same user is being maintained by the session of a servlet. Session tracking is a mechanism that is tracked and maintained by such requests by the user. These sessions are being shared by the servlets and accessed by a client. This provides a convenience to the web applications that uses multiple servlets. For example a shopping cart application uses the session tracking, which tracks the requests of the user for various mobile devices. In java servlets this task is being maintained by:
Cookies, URL rewriting, session objects(HttpSession) and hidden fields.

Explain session tracking in servlets.

The states of a series of requests from a single user, for a stipulated period of time, are performed by the mechanism called session tracking. Sessions can be shared among the servlets which is a convenient for a web application that uses multiple servlets.

HTTP is a stateless protocol. The server cannot remember the previous transactions. To identify all the requests from one user, the web server is to remember every user. To overcome the problem user’s information is being managed by implemented by Cookies, URL rewriting, hidden fields. A session i.e., a user’s requests are tracked by – obtaining a session object, storing and retrieving data from the HttpSession object and invalidate the session after a user completes flushing the requests. 


Explains the differences between context.getRequestDispatcher() and request.getRequestDispatcher() ? [Very Important]

• To create the relative path for the resource we use request.getRequestDispatcher(path) .

• To create the absolute path of the resource we use resourcecontext.getRequestDispatcher(path).





Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -