Servlet interview questions and answers for experienced

Categories: Servlet Interview questions and answers Experienced Freshers

Servlet interview questions and answers for experienced by our expert team.

Questions :- What do you know about the ServletConfig object?

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

Questions :- What advantages does a servlet offer over CGI?

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

Questions :- What do you mean by a web application?

Answer :- 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.e.g. Tomact 

Questions :- What is the difference between the POST and GET methods?

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

Questions :- What do you mean by MIME Type?

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

Questions :- What do you mean by Session Tracking and also explain its techniques?

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

Questions :- Who is liable for writing a constructor?

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

Questions :- Name the various types of session tracking?

Answer :-The types of session tracking are:

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

Questions :- What is URL Encoding?

Answer :- 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 java.net.URLEncoder.encode(String str, String unicode) to encode a String. URL Decoding is the reverse process of encoding and we can use java.net.URLDecoder.decode(String str, String unicode) to decode the encoded string. For example “Rahul’s Data” is encoded to “Rahul%27s+Data”.

Questions :- Why HttpServlet class is declared abstract?

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

Questions :- How can we invoke another servlet in a different application?

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

Questions :- How do we call one servlet from another servlet?

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

Questions :- What is servlet attributes and their scope?

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

Questions :- What is the inter-servlet communication?

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

Questions :- Is it good idea to create servlet constructor?

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

Questions :- Do we need to override service() method?

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

Questions :- What is the use of servlet wrapper classes?

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

Questions :- How can we create deadlock situation in servlet?

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

Questions :- What is difference between PrintWriter and ServletOutputStream?

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

Questions :- How to get country name in Servlets?

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

String getDisplayCountry()

Questions :- What is locale?

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

Questions :- What is localization?

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

 

Questions :- How to read form data in servlet?

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

Questions :- When init() method of servlet gets called?

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

Questions :- What are the major tasks of servlets?

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

Questions :- What do you mean by Default initialization in Java Servlet?

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

Questions :- What is the difference between Servlets and applets?

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

Questions :- What is Pure Servlet?

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

Questions :- Can we refresh servlet in client and server side automatically?

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

Questions :- What is the difference between Server and Container?

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

Questions :- What is called Scriptlet?

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

Questions :- What is servlet lazy loading?

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

Questions :- What is URL rewriting?

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

Questions :- Why session tracking is needed?

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

Questions :- What are the supporting protocol by HttpServlet ?
Answer :- HttpServlet supports only HTTP and HTTPS protocol. 

Questions :- What are the features added in Servlet 2.5?
Answer :- 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 

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

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

Questions :- What is a filter?

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

Questions :- What is a web container and what is its responsibility?

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

Questions :- What are the life cycle methods of the Servlet?

Answer :- There are basically three lifecycle methods of a servlet.These are:

  1. init (),
  2. service ()
  3. destroy ()

 Questions :- What is the difference between the Http Servlet and Generic Servlet?

Answer :- 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 HttpServlet extends the generic servlet and supports the HTTP methods such as doGet (), doPost (), doHead (), doTrace (), etc.

Questions :- How is a Servlet implemented in code?

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

 

Questions :- What are the advantages of Servlet over CGI?

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

Questions :- What do you mean by Servlet context?

Answer :- 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

Questions :- Name the important functions of filters?

Answer :- The important functions of filters are:

  1. Logging and auditing
  2. Security check
  3. Response compression
  4. Data compression
  5. Modifying the response

 Questions :- Define Servlet mapping?

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

Questions :- What do you mean by Servlet?

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

 

Top articles
Servlet interview questions and answers for experienced Published at:- Servlet Lifecycle method Published at:- Servlet Architecture Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.