Servlet interview questions

If you are Java developer, then you must have the good knowledge of servlet. As this is the utmost topic of Java EE. Coupling with this, all the frameworks of web applications such as spring and Strut are built on top of it. There are chances that interviewer asked questions from Servlet. So, equip yourself with these questions.

  • Q1.

    What do you mean by Servlet?

    It is designed by the Java software company. It is used to extend the ability of the server. Servlet is created to handle all types of requests but usually, it works to hosts web application on web servers. So, it is used as server-side servlet web API.

  • Q2.

    Differentiate between the web server and application server?

    Web Server is the connection between the HTTP and HTML. Web server takes the response from the client browser and gives output in the form of HTML. It is understandable of HTTP and works on HTTP protocol.

    Apache Web server is a good example of the web server. There are several containers that are used to accomplish servlets. JSPs is also referred to as servlet container, for e.g. Tomcat.

    While there is some list of supplementary features that are provided by the Application Servers. This list includes

    Enterprise JavaBeans support

    JMS Messaging support

    Transaction Management

    So, Applications server are basically the modified version of web servers which includes some additional features and functionalities that make the developers more comfort with business applications.

  • Q3.

    Which HTTP method is said to be non-idempotent and idempotent?

    If any HTTP brings the same result every time then it means it is idempotent. We implement application when need the same result at every request. Some of the HTTP methods are PUT, DELETE, HEAD, GET, OPTIONS. While that HTTP which brings the different result at every request is said to be non- idempotent. The POST method is the example of non- idempotent. As opposed to the above, POST method is implanted when there is need of something that changes at every request. Let's take an example to simplify it.

    We use the method GET at the time of accessing in an HTML page because it will ever pay the same result. Post method is used when there is need to save customer's information to the database. idempotent methods are referred to as a safe method because it does not give the repetitive request from the user.

  • Q4.

    What are the benefits of using Servlet over CGI?

    Servlet technology was introduced to overcome the limitations of CGI technology. These drawbacks are:

    • Servlet technology is better than the CGI in performance with the respect of the timing process. It is possible because Servlet uses the multithreading technology. By this, a new thread has been created at every request which is faster than the loading generating fresh request Object for every request with CGI
    • Servlet technology is better than CGI in memory utilization.
    • It is efficient to use Servlets as the web application that develops on it can work on a standard web container, for example, JBoss, Tomcat, Glassfish servers.
    • It can also run on several operating systems like Linux, Windows, Solaris, Mac, Unix etc.
    • The very important point of choosing Servlet is that the users don't have to worry about the security, memory leaks, garbage collection etc. container maintain servlet
    • Servlets is easy to use as there is nothing to learn except business logic
    • Servlets are manageable and the knowledge trajectory is little.
  • Q5.

    Describe some assignments that are executed by Servlet Container?

    Applications that are executed by Servlet container are: -

      • Communication

    These containers are beneficially in communication as it provides the best and easy method to communicate between servlet and web client or browser and JSPs. In the absence of a container, there is a need of server rocket which listens to the request from the browser and then analyzes it. When it is done, it generates the response to the request. All this process is very time-consuming. But with the container the whole process is fast. The container itself perform all these difficult tasks, users only have to concentrate on the business logic for the applications.

      • Life cycle and sources handling

    servlet handles many tasks like carrying of servlets into memory, starting the servlets, summoning servlets tasks, gives services like JNDI and much more.

      • Multithreading

    As discussed earlier, the container provides a different thread to the servlet for every request. With this, times and memory both are saved.

      • JSP

    JSP is originally looked different from the ordinary java classes. JSP is first selected by the container and then changed it to the Servlet. After that, it is handled by the container same as the other servlets

    • Diverse applications

    Scarlet provides a lot of applications that help the clients to work on it easily and make them comfortable. Some of these applications are

    • It handles the resource pool
    • It makes memory optimizations
    • Servlets act as the garbage collector too
    • It addresses security arrangements
    • It is very supportive of various tasks
    • It is also efficient in hot deployment tasks
    • It handles so many tasks in the background that is very useful for a developer.
  • Q6.

    What is the use of Request Dispatcher Interface?

    Request Dispatcher interface is used as a dispatcher in Servlet. It dispatches the requests to different sources. The source can be of any type it may be a servlet, JSP or HTML. Request Dispatcher interface is also practiced to add the material of resources.

  • Q7.

    What do you understand by MIME Type?

    MIME Type is referred to the 'Content-Type' response header. It provides knowledge to the clients about the type of data that is sending to them. It is very efficient for the client as they are able to render the data for the users. Some examples of the MIME Types are text/XML, text/HTML, application/XML and many more.

    Now it is very easy to arrange the correct MIME type. For this, just use a Servlet Context, get Mime Type () technique and fix it to the response type. This method is highly useful at the time of downloading any file from the server using a servlet.

  • Q8.

    What do you mean by web applications? Explain web application directory arrangement?

    Web applications provide the client browser both the unvarying and changing content. PHP is understood by Apache web browser and thus clients are able to create web applications using PHP. In a servlet container, the web application is supported by the Servlets and JSPs. And these web applications are provided by the Java.

  • Q9.

    What is the purpose of inter-servlet communication?

    Inter-servlet is used to request different servlet from servlet services techniques. It can be possible with Request Dispatcher forward () and include () programs. It also gives you the more features in a invoke for using a different servlet.

  • Q10.

    Elucidate servlet attributes and its scope?

    Users are able to get, set and delete the attributes from a session, application, request in the web system. It is done by Http Session, Servlet context and Servlet request. This method is very valuable in inter-servlet communication.

    Session scope, application scope, and request scope are the qualities/attributes of the servlet.

  • Q11.

    What is the procedure of invoking different servlet in a different application?

    Request Dispatcher is used to invoking a specific application. As it is used for a specific application, we can't use this method for every application. In the case of sending a request to the resource in a different application, it is better to use Servlet Response send Redirect () and provides the full URL of another servlet. When it is done, it forwards the clients a response which has a response code to send the invoke to another URL. Cookies are also available to send the data to the different servlet.

  • Q12.

    What are the various ways of session supervision in servlets?

    A session is a colloquial link between the server and the client. Session handles a lot of requests and supplies the answer within the client and server. As both the HTTP and web server are unsettled, so for the maintenance of a session it is necessary to have some bizarre request that has to be passed between the server and the client.

    The various method that is used to manage session in servlets are

    • Cookies
    • HTML Hidden Field
    • URL Rewriting
    • User Authentication
    • Session Management API
  • Q13.

    What do you understand by URL Rewriting?

    It is not good to use Http Session in servlets as it does not work by alone. It uses cookies for session management and we can disable the cookie in the client browser. So most of the IT worker believes that it is better to use Servlet API because it can easily perform the function of URL rewriting.

    Servlet API is very easy to use as it involves only one step that is encoding the URL. So, it is very beneficially in respect of coding. Another benefit of using Servlet API over Http session is that it works only when the cookies are disabled.

    Http Servlet Response encodes URL () can also be used for redirecting the request to the different resources and encode Redirect URL () method is used in the case of client wants to give some session knowledge.

  • Q14.

    Differentiate between the GET and POST method

    • GET is a safe method (idempotent) where POST is the non-idempotent method.
    • GET is an idempotent method and it is safe while the POST method is non-idempotent.
    • GET is the part of the header while POST is the content of the body.
    • A limited data can be sent with GET while POST method allowed a large data to send
    • GET method can easy to bookmark while POST method can't.
    • It is not secure to use the Get method as it is possible to send the same request over and over while the POST method is secure.
    • GET method is the default method while the POST method needs to be specified.
  • Q15.

    Differentiate between the Print Writer and Servlet Output Stream?

    • Print Writer is referred to a character-stream state while Servlet Output Stream is considered as a byte-stream state.
    • Print Writer is used to writing the information that is based on the character for example character design and string to the response. While to write the byte design, Servlet Output Stream is used.
    • To arrange the Print Writer the clients have to use the Servlet Response get Writer () while to arrange Servlet Output Stream the client have to use Servlet Response get Output Stream () method.
  • Q16.

    Why do we have servlet wrapper classes?

    To design implement of servlet request and response, these wrapper classes are being used. The users are able to increase these classes to revoke the particular techniques that need to be implemented. There are two wrapper classes that are provided by Servlet HTTP API. These classes are

    1. Http Servlet Response Wrapper
    2. Http Servlet Request Wrapper
  • Q17.

    Explain URL Encoding?

    URL Encoding means the changing of data into CGI. It is done to circumvent any type of issue while it is going over the network. URL coding is schemed by removing the white spaces and put any escape symbols. Thus, the special characters are removed. To encode any string java.net.URLEncoder.encode (String str, String Unicode) is used and to decode the URL java.net.URLDecoder.decode (String str, String Unicode) is used. Let's take an example to simplify it. To encode 'Ravi's data' it is changed into 'Ravi/24d+data'.

  • Q18.

    Why the concept of Single Thread Model interface is used?

    Safety and security are always the priority for any company. Thus, the single Thread Model interface introduces for this reason. The main purpose of this concept is to check whether two or more threads are executing at the same time. Like many threads may be penetrated by many requests, so to execute them in a manner, it is used. But in servlet 2.3, this concept is disapproved as it can stop the multithreading method.

  • Q19.

    Write all the phases defined in Servlet Life Cycle?

    There are main four phases required in Servet Life Cycle. These are:

    1. Servlet class Loading: On a client request of a servlet, the container first loads the class in the memory and after that calls its default no argument constructor.
    2. Servlet Class Initialization: After the first phase of loading class in memory, container initiates the ServletContext object and then invokes the Init method by passing servlet Config for the servlet. This phase transforms the normal class to a servlet.
    3. Request Handling: It is the phase where a servlet is ready to handle the requests. For each client request, it invokes the method service () by passing request and response object reference.
    4. Removal from service: when the process stops, the container calls the destroy () method and destroy the servlet.
  • Q20.

    List some life cycle methods of a servlet.

    It consists of three methods:

    1. Public void Init: Container initialize the servlet using this method. It is invoked once during the whole life cycle.
    2. Public void service: For every request, this method is called once at a time.
    3. Public void destroys: This is called when a servlet is destroyed from memory.
  • Q21.

    What is the need of Servlet filters?

    Servlet filters are pluggable java components that are used to process requests before the requests are sent to the servlets and before a container sends the response back to the client.

    Some tasks performed with filters are:

    • Logging request parameters to log files.
    • Authorization of request for services
    • The arrangement of request body and header before sending it to a servlet.
    • Condense the response data sent to client
    • Add cookies, header information etc. In response.
  • Q22.

    Write a command to get actual path of a servlet to the server?

    Use the written code to get the actual path:

    GetServletContext().getRealPath(request.getServletPath())

  • Q23.

    Write the code to get the server information in servlet.

    Use the below snippet code to get the information:

    getServletContext().getServerInfo()

  • Q24.

    What are different ways for authentication of servlet?

    There are many ways of login-based authentication. These are:

    • HTTP Digest Authentication
    • HTTP Basic Authentication
    • HTTPS Authentication
    • Form-based Authentication
  • Q25.

    Why the container loads server at the application startup and how?

    In general, on the very first request of the client, the server container loads a servlet. But in case, the servlet is too heavy that it takes much time to load, at that time we might want it to load during the application startup. To do this, the user can use WebServlet notation or loadOnStartup variable to stimuli container. Or, we can use a load-on-startup element with a servlet configuration in web.xml file to load the servlet on startup.

    Syntax:

    <servlet>
    <servlet-name>coo </servlet-name>
    <servlet-class>com.coo.servlets.Coo </servlet-class>
    <load-on-startup>6 </load-on-startup>
    </servlet> 
    

    If you want that the container loads the servlet on client request, then the load-on-startup value should be negative otherwise if it’s positive then the loading will start on application startup.

    If there are many servlets with different load-on-startup values, then that servlet will be loaded first which have the lowest value.