Servlets fresher interview question/Servlets Interview Questions and Answers for Freshers & Experienced

What do you know about the ServletConfig object?

You use the ServletConfig object to give configuration information to a specific servlet. Each servlet has a unique ServletConfig object and the servlet container instantiates it. You can give servlet init parameters through the WebInitParam annotation. To get the ServletConfig object of a servlet, you’d have to use the getServletConfig() method.

What advantages does a servlet offer over CGI?

The CGI technology had many shortcomings. Servlets were introduced to overcome the same.
Servlets offer better performance than CGI in terms of utilising memory and processing time. They use the benefits of multithreading, where they create a new thread for every request, enhancing their speed greatly. In contrast, CGI creates a new Object for every request, which is relatively slower than the servlets’ process.
Servlets are system and platform-independent. You can run a servlet-based web application on any standard web container (Glassfish, Tomcat, and JBoss) and operating systems (Unix, Windows, Mac, etc.).
The learning curve for servlets is pretty small as you only need to handle the business logic for the application. Moreover, their container handles the servlet’s life cycle, so there’s no risk of memory leaks, garbage collection, and security.

What do you mean by a web application?

A web application is a module that runs on the server to provide dynamic and static content to the client browser. The Apache web server supports PHP, and you can create a web application using the same.

What is the difference between the POST and GET methods?

The primary difference between the POST and GET methods is that the POST method carries the response parameters in the message body while the GET method carries the response parameters appended in the URL string.

What do you mean by MIME Type?

MIME stands for Multipurpose Internet Mail Extension. The MIME type is an HTTP header that gives information about what we’re sending to a browser. It helps the client in data rendering. Common MIME types are text (HTML), text (plain), images (jpeg), application (jar), etc.
To get the correct MIME type of a particular file, you can use the ServletContext getMimeType() method. It comes in handy while downloading a file through servlets from a server.

7. What do you mean by Session Tracking and also explain its techniques?

Session tracking is a technique that servlets use to maintain a record of a series of requests originating from the same user or the same browser across a period of time. These sessions are shared between the servlets that are accessed by a client.

There are 4 techniques to track sessions:
1. Cookies
2. Hidden Fields.
3. URL Rewriting.
4. Session Tracking API.

Who is liable for writing a constructor?

The container is accountable for writing constructor without arguments in Servlet.

Name the various types of session tracking?

The types of session tracking are:

1. URL rewriting
2. Cookies
3. Secure Socket layer
4. Hidden form fields

Why do we have servlet listeners?

We know that using ServletContext, we can create an attribute with application scope that all other servlets can access but we can initialize ServletContext init parameters as String only in the deployment descriptor (web.xml). What if our application is database-oriented and we want to set an attribute in ServletContext for Database Connection.

If your application has a single entry point (user login), then you can do it in the first servlet request but if we have multiple entry points then doing it everywhere will result in a lot of code redundancy. Also if the database is down or not configured properly, we won’t know until the first client request comes to the server. To handle these scenarios, servlet API provides Listener interfaces that we can implement and configure to listen to an event and do certain operations.

What is URL Encoding?

URL Encoding is the process of converting data into CGI form so that it can travel across the network without any issues. URL Encoding strips the white spaces and replaces special characters with escape characters. We can use str, String unicode) to encode a String. URL Decoding is the reverse process of encoding and we can use str, String unicode) to decode the encoded string. For example “Rahul’s Data” is encoded to “Rahul%27s+Data”.

Why HttpServlet class is declared abstract?

HttpServlet class provide HTTP protocol implementation of servlet but it’s left abstract because there is no implementation logic in service methods such as doGet() and doPost() and we should override at least one of the service methods. That’s why there is no point in having an instance of HttpServlet and is declared abstract class.

How can we invoke another servlet in a different application?

We can’t use RequestDispatcher to invoke servlet from another application because it’s specific for the application. If we have to forward the request to a resource in another application, we can use the ServletResponse sendRedirect() method and provide the complete URL of another servlet. This sends the response to the client with the response code as 302 to forward the request to another URL. If we have to send some data also, we can use cookies that will be part of the servlet response and sent in the request to another servlet.

How do we call one servlet from another servlet?

We can use RequestDispatcher forward() method to forward the processing of a request to another servlet. If we want to include the another servlet output to the response, we can use RequestDispatcher include() method.

What is servlet attributes and their scope?

Servlet attributes are used for inter-servlet communication, we can set, get and remove attributes in web application. There are three scopes for servlet attributes – request scope, session scope and application scope.
ServletRequest, HttpSession, and ServletContext interfaces provide methods to get/set/remove attributes from request, session anServlet attributes are different from init parameters defined in web.xml for ServletConfig or ServletContext.d application scope respectively.

What is the inter-servlet communication?

When we want to invoke another servlet from a servlet service methods, we use inter-servlet communication mechanisms. We can invoke another servlet using RequestDispatcher forward() and include() methods and provide additional attributes in request for other servlet use.

Is it good idea to create servlet constructor?

We can define a constructor for servlet but I don’t think it’s of any use because we won’t be having access to the ServletConfig object until unless servlet is initialized by the container. Ideally, if we have to initialize any resource for the servlet, we should override init() method where we can access servlet init parameters using ServletConfig object.

Do we need to override service() method?

When servlet container receives client request, it invokes the service() method which in turn invokes the doGet(), doPost() methods based on the HTTP method of request. I don’t see any use case where we would like to override the service() method. The whole purpose of service() method is to forward to request to corresponding HTTP method implementations. If we have to do some pre-processing of request, we can always use servlet filters and listeners.

What is the use of servlet wrapper classes?

Servlet HTTP API provides two wrapper classes – HttpServletRequestWrapper and HttpServletResponseWrapper. These wrapper classes are provided to help developers with custom implementation of servlet request and response types. We can extend these classes and override only specific methods we need to implement for custom request and response objects. These classes are not used in normal servlet programming.

How can we create deadlock situation in servlet?

We can create deadlock in servlet by making a loop of method invocation, just call doPost() method from doGet() method and doGet() method to doPost() method to create deadlock situation in servlet.

What is difference between PrintWriter and ServletOutputStream?

PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class. We can use PrintWriter to write character based information such as character array and String to the response whereas we can use ServletOutputStream to write byte array data to the response.

We can use ServletResponse getWriter() to get the PrintWriter instance whereas we can use ServletResponse getOutputStream() method to get the ServletOutputStream object reference.

How to get country name in Servlets?

Following method returns a name for the locale's country that is appropriate for display to the user.

String getDisplayCountry()

What is locale?

This is a particular cultural or geographical region. It is usually referred to as a language symbol followed by a country symbol which is separated by an underscore. For example "en_US" represents english locale for US.

What is localization?

This means adding resources to a web site to adapt it to a particular geographical or cultural region for example Hindi translation to a web site.

How to create a cookie using servlet?

Setting cookies with servlet involves three steps:

(1) Creating a Cookie object: You call the Cookie constructor with a cookie name and a cookie value, both of which are strings.

How to read form data in servlet?

Servlets handles form data parsing automatically using the following methods depending on the situation:

1. getParameter(): You call request.getParameter() method to get the value of a form parameter.
2. getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
3. getParameterNames(): Call this method if you want a complete list of all parameters in the current request.

When init() method of servlet gets called?

The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets.

What are the major tasks of servlets?

Servlets perform the following major tasks:
1. Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
2. Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
3. Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
4. Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
5. Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.

What do you mean by Default initialization in Java Servlet?

This is one of the servlet initialization and it is initialized when it is called for the first time.

What is the difference between Servlets and applets?

Servlets are used for server side config and it keeps on server. But, Applets are used for client side coding and it runs on client browsers.

What is Pure Servlet?

Pure servlet is servlet which is used to create java objects that can be implemented from javax.servlet.Servlet interface.

Can we refresh servlet in client and server side automatically?

On the client side, Meta http is used for refresh and server push is used for server side refresh.

What is the difference between Server and Container?

A server can provide service to the client and it contains one or more containers such as EJBs, Servlet, JSP containers. Containers hold set of objects.

What is called Scriptlet?

A scriptlet contains any language statements, variables, expressions that can be valid in the page scripting language. Scriptlet is a part of generated servlet service method.

What is servlet lazy loading?

A servlet container which does not initialize at the start up, this is known as servlet lazy loading.

What is URL rewriting?

URL rewriting is one of the methods of session tracking in which additional data is appended at the end of each URL. This additional data identifies the session.

Why session tracking is needed?

Every HTTP request needs to be captured by HTTP protocol and for that, state is captured. Tracking of state is called session tracking.

What are the supporting protocol by HttpServlet ?

HttpServlet supports only HTTP and HTTPS protocol.

What are the features added in Servlet 2.5?

Following are the features added in Servlet 2.5:

1. Dependency on J2SE 5.0
2. Support for annotations
3. Loading the class
4. Several web.xml
5. Removed restrictions
6. Edge case clarifications

How can we refresh automatically when new data has entered the database?

Refresh in Client side and Server Push can be performed to refresh automatically when new data is entered into the database.

What is a filter?

A filter is nothing but a piece of code which can be reusable that will be transforming the content of HTTP requests, response and header information.

What is a web container and what is its responsibility?

A web container is also called Servlet container and is used to interact with the Servlet and contains all the Servlet, JSP, XML files in it. Web container manages the life cycle of a servlet and helps to map the URL to a specific servlet. Web container creates the object of a servlet.

What are the life cycle methods of the Servlet?

There are basically three lifecycle methods of a servlet.
These are:
Init (),
Service (),
Destroy ()

What is the difference between the Http Servlet and Generic Servlet?

The difference between the Http Servlet and Generic Servlet is
the Generic Servlet can handle all types of requests. As it has a service () method, it is independent, whereas Http Servlet extends the generic servlet and supports the HTTP methods such as doGet (), doPost (), doHead (), doTrace (), etc.

How is a Servlet implemented in code?

Servlet can be implemented in code by simply extending the Httpservlet or generic servlet class.

What are the advantages of Servlet over CGI?

The advantages of the servlet are as follows:

1. Servlet creates a thread for each incoming request and not process, thus it is faster.
2 Servlet is platform-independent as it is based on Java Programming Language.
3. As it is based on Java, it is also robust and secure.

When do you think Servlet is unloaded?

A Servlet is unloaded when:
Administrator manually unloads

Server shuts down

What do you mean by Servlet context?

Servlet context holds Servlet view of web application in which Servlet will be in a row. By using the context,

1. Set and store attributes
2. Log events
3. Obtain URL references to resources

Name the important functions of filters?

The important functions of filters are:
1. Logging and auditing
2. Security check
3. Response compression
4. Data compression
5. Modifying the response

Define Servlet mapping?

Servlet Mapping is a connection mapping between Servlet and a URL pattern. It is used to map Servlet with the needs.

What do you mean by Servlet?

Servlet is a Java program that is managed by a container called servlet engine. It generates active content and interacts with the client through demand and Response.

R4R Team
R4R provides Servlets Freshers questions and answers (Servlets Interview Questions and Answers) .The questions on website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,Servlets fresher interview question,Servlets Freshers & Experienced Interview Questions and Answers,Servlets Objetive choice questions and answers,Servlets Multiple choice questions and answers,Servlets objective, Servlets questions , Servlets answers,Servlets MCQs questions and answers Java, C ,C++, ASP, C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for Servlets fresher interview questions ,Servlets Experienced interview questions,Servlets fresher interview questions and answers ,Servlets Experienced interview questions and answers,tricky Servlets queries for interview pdf,complex Servlets for practice with answers,Servlets for practice with answers You can search job and get offer latters by studing .learn in easy ways .