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.

Posted Date:- 2021-08-19 07:04:09

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.

Posted Date:- 2021-08-19 07:03:25

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.

Posted Date:- 2021-08-19 07:01:41

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.

Posted Date:- 2021-08-19 07:00:52

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.

Posted Date:- 2021-08-19 07:00:12

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.

Posted Date:- 2021-08-19 06:59:09

Who is liable for writing a constructor?

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

Posted Date:- 2021-08-19 06:29:29

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

Posted Date:- 2021-08-19 06:28:29

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.

Posted Date:- 2021-08-19 06:26:57

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”.

Posted Date:- 2021-08-19 06:21:33

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.

Posted Date:- 2021-08-19 06:17:12

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.

Posted Date:- 2021-08-19 06:16:08

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.

Posted Date:- 2021-08-19 06:15:28

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.

Posted Date:- 2021-08-19 06:14:54

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.

Posted Date:- 2021-08-19 06:13:24

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.

Posted Date:- 2021-08-19 06:12:47

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.

Posted Date:- 2021-08-19 06:11:08

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.

Posted Date:- 2021-08-19 06:09:07

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.

Posted Date:- 2021-08-19 06:07:52

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.

Posted Date:- 2021-08-19 06:07:19

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()

Posted Date:- 2021-08-19 06:03:21

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.

Posted Date:- 2021-08-19 06:02:27

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.

Posted Date:- 2021-08-19 06:01:55

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.

Posted Date:- 2021-08-19 06:00:48

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.

Posted Date:- 2021-08-19 06:00:11

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.

Posted Date:- 2021-08-19 05:56:03

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.

Posted Date:- 2021-08-19 05:53:19

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.

Posted Date:- 2021-08-19 05:49:03

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.

Posted Date:- 2021-08-19 05:48:25

What is Pure Servlet?

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

Posted Date:- 2021-08-19 05:45:45

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.

Posted Date:- 2021-08-19 05:45:01

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.

Posted Date:- 2021-08-19 05:44:34

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.

Posted Date:- 2021-08-19 05:43:35

What is servlet lazy loading?

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

Posted Date:- 2021-08-19 05:42:33

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.

Posted Date:- 2021-08-19 05:41:42

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.

Posted Date:- 2021-08-19 05:41:16

What are the supporting protocol by HttpServlet ?

HttpServlet supports only HTTP and HTTPS protocol.

Posted Date:- 2021-08-19 05:40:25

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

Posted Date:- 2021-08-19 05:39:49

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.

Posted Date:- 2021-08-19 05:38:24

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.

Posted Date:- 2021-08-19 05:37:59

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.

Posted Date:- 2021-08-19 05:36:34

What are the life cycle methods of the Servlet?

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

Posted Date:- 2021-08-19 05:36:05

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.

Posted Date:- 2021-08-19 05:34:51

How is a Servlet implemented in code?

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

Posted Date:- 2021-08-19 05:33:42

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.

Posted Date:- 2021-08-19 05:33:10

When do you think Servlet is unloaded?

A Servlet is unloaded when:
Administrator manually unloads

Server shuts down

Posted Date:- 2021-08-19 05:31:30

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

Posted Date:- 2021-08-19 05:30:44

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

Posted Date:- 2021-08-19 05:29:40

Define Servlet mapping?

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

Posted Date:- 2021-08-19 05:26:57

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.

Posted Date:- 2021-08-19 05:25:07

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 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 .