Servlets Interview Questions And Answers for freshers and experienced

Servlets Interview Questions And Answers for freshers as well 1 to 3 year experienced on advance and basic Servlets with examples that cover the essentials of Servlets means from life cycle to deploy questions covered under this page and also a form is given at the end of this page for those user who want more Interview questions and answer on Servlets just need to fill the form and send us we will send all the answers to them and it for both freshers and experienced condidate and also chat option is given in right hand side for directly chat with for answers


Top servlets interview questions and answers are below

Questions : 1 What is Servlet?
Answers : 1

Servlet is small program which execute on the server. The environment exploiting the functionalities of the web server.More

   
Questions : 2 What are advantages of servlets over CGI?
Answers : 2

In CGI for every request there is a new process started which is quiet an overhead. In servlets JVM stays running and handles each request using a light weight thread. In CGI if there are 5000 request then 5000 CGI program is loaded in memory while in servlets there are 5000 thread and only one copy of the servlet class.

   
Questions : 3 What is Servlet life cycle?
Answers : 3

There are three methods which are very important in servlet life cycle those one init(), service() and destroy(). Server invokes "init ()" method when servlet is first loaded in to the web server memory. Servlet reads HTTP data provided in HTTP request in the "service ()" method. Once initialized servlet remains in memory to process subsequent request. So for every HTTP request "service ()" method of the servlet is called. Finally when server unloads the "servlet ()" from the memory it calls the "destroy" method which can be used to clean up any resource the servlet is consuming.

   
Questions : 4 What are the two important API’s in for Servlets?
Answers : 4

Two important packages are required to build servlet "javax.servlet" and javax.servlet.http". They form the core of Servlet API. Servlets are not part of core Java but are standard extensions provided by Tomcat.

   
Questions : 5 Can you explain in detail “javax.servlet” package?
Answers : 5

javax.servlet package has interfaces and classes which define a framework in which servlets can operate. Let’s first make a walk through of all the interfaces and methods and its description.

Interfaces in javax.servlet :-
Servlet Interface This interface has the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. Following are the method in Servlet interface :- void destroy( ):- Executed when servlet is unloaded from the web server memory. ServletConfig getServletConfig() :- Returns back a ServletConfig object that contains initialization data. String getServletInfo( ):- Returns a string describing the servlet. init method :- Called for first time when the servlet is initialized by the web server. void service() method :- Called to process a request from a client. ServletConfig Interface This interface is implemented by the servlet container. Servlet can access any configuration data when its loaded. The methods declared by this interface are summarized here: Following are the methods in ServletConfig interface:- ServletContext getServletContext():- Gives the servlet context. String getInitParameter(String param):- Returns the value of the initialization parameter named param. Enumeration getInitParameterNames() :- Returns an enumeration of all initialization parameter names.

String getServletName( ) :- Returns the name of the invoking servlet. .

   
Questions : 6 What’s the use of ServletContext?
Answers : 6

ServletContext Interface
It gives information about the environment. It represents a Servlet's view of the Web Application.Using this interface servlet can access raw input streams to Web Application resources, virtual directory translation, a common mechanism for logging information, and an application scope for binding objects.

   
Questions : 7 What's the difference between GenericServlet and HttpServlet?
Answers : 7

HttpServlet class extends GenericServlet class which is an abstract class to provide HTTP protocol-specific functionalities. Most of the java application developers extend HttpServlet class as it provides more HTTP protocol-specific functionalities. You can see in HttpServlet class doGet (), doPOst () methods which are more targeted towards HTTP protocol specific functionalities. For instance we can inherit from GenericServlet class to make something like MobileServlet. So GenericServlet class should be used when we want to write protocol specific implementation which is not available. But when we know we are making an internet application where HTTP is the major protocol its better to use HttpServlet.

   
Questions : 8 What’s the architecture of a Servlet package?
Answers : 8

At the top of all is the main servlet interface which is implemented by the generic servlet. But generic servlet does not provide implementation specific to any protocol. HTTP servlet further inherits from the generic servlet and provides HTTP implementation like “Get” and “Post”. Finally comes our custom servlet which inherits from HTTP Servlet.

   
Questions : 9 Why is HTTP protocol called as a stateless protocol?
Answers : 9

A protocol is stateless if it can remember difference between one client request and the other. HTTP is a stateless protocol because each request is executed independently without any knowledge of the requests that came before it.

   
Questions : 10 What are the different ways we can maintain state between requests?
Answers : 10

Following are the different ways of maintaining state’s between stateless requests:-
>>URL rewriting
>> Cookies
>>Hidden fields
>> Sessions

   
Questions : 11 What is URL rewriting?
Answers : 11

It’s based on the concept of attaching a unique ID (which is generated by the server) in the URL of response from the server. So the server attaches this unique ID in each URL.

   
Questions : 12 ) What are cookies?
Answers : 12

Cookies are piece of data which are stored at the client’s browser to track information regarding the user usage and habits. Servlets sends cookies to the browser client using the HTTP response headers. When client gets the cookie information it’s stored by the browser in the client’s hard disk. Similarly client returns the cookie information using the HTTP request headers.

   
Questions : 13 What are sessions in Servlets?
Answers : 13

Session's can be considered as a series of related interactions between client and the server that take place over a period of time. Because HTTP is a stateless protocol these series of interactions are difficult to track. That’s where we can use HttpSession object to save in between of these interactions so that server can co-relate between interactions between clients.

   
Questions : 14 Which are the different ways you can communicate between servlets?
Answers : 14

Below are the different ways of communicating between servlets:-
>> Using RequestDispatcher object.
>> Sharing resource using “ServletContext ()” object.
>> Include response of the resource in the response of the servlet.
>> Calling public methods of the resource.
>> Servlet chaining.

   
Questions : 15 How do we share data using “getServletContext ()?
Answers : 15

Using the “getServletContext ()” we can make data available at application level. So servlets which lie in the same application can get the data. Below are important snippets which you will need to add, get and remove data which can be shared across servlets. Figure 4.10 : - Sharing data using “getServletContext ()” You can see in Step1 how “getServletContext().setAttribute()” method can be used add new objects to the “getServletContext” collection. Step2 shows how we can get the object back using “getAttribute()” and manipulate the same. Step3 shows how to remove an already shared application object from the collection. “getServletContext()” object is shared across servlets with in a application and thus can be used to share data between servlets.

   
Questions : 16 Explain the concept of SSI ?
Answers : 16

Server Side Includes (SSI) are commands and directives placed in Web pages that are evaluated by the Web server when the Web page is being served. SSI are not supported by all web servers. So before using SSI read the web server documentation for the support. SSI is useful when you want a small part of the page to be dynamically generated rather than loading the whole page again.

   
Questions : 17 what’s the difference between Authentication and authorization?
Answers : 17

Authentication is the process the application identifies that you are who. For example when a user logs into an application with a username and password, application checks that the entered credentials against its user data store and responds with success or failure. Authorization, on the other hand, is when the application checks to see if you're allowed to do something. For instance are you allowed to do delete or modify a resource.

   
Questions : 18 Explain in brief the directory structure of a web application?
Answers : 18

Below is the directory structure of a web application:-
webapp/
WEB-INF/web.xml
WEB-INF/classes
WEB-INF/lib

The webapp directory contains the JSP files, images, and HTML files. The webapp directory can also contain subdirectories such as images or html or can be organized by function, such as public or private.
The WEB-INF/web.xml file is called the deployment descriptor for the Web application. This file contains configuration information for the Web application, including the mappings of URLs to servlets and filters. The web.xml file also contains configuration information for security, MIME type mapping, error pages, and locale settings
The WEB-INF/classes directory contains the class files for the servlets, JSP files, tag libraries, and any other utility classes that are used in the Web application.
The WEB-INF/lib directory contains JAR files for libraries that are used by the Web application. These are generally third-party libraries or classes for any tag libraries used by the Web application.

   

Next Page 1 | Next Page 2 | Next Page 3

Ask your interview questions on Servlate

Write Your comment or Questions if you want the answers on Servlate from Servlate Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---