Servlet and JSP Interview Questions | Page 3


Question 21:

What do you mean by idempotent HTTP methods?

An idempotent HTTP method is a HTTP method that can be called many times without different outcomes. It would not matter if the method is called only once, or ten times over. The result should be the same. Again, this only applies to the result, not the resource itself. From the given options GET, HEAD, DELETE & PUT are idempotent methods.

Question 22:

When the servlet is unloaded?

Any servlet is unloaded by the container if the container is shut down, or if the container reloads the whole web application at runtime.

Question 23:

Why <load-on-startup> is used in web.xml?

Any servlet is loaded/initialized when the first request comes to container. Hence, the first request may appear significantly slow. To reduce the latency, you can define <load-on-startup> for any servlet so that container loads it at the time when server is started. To read full article visit Why load-on-startup is used in web.xml 

Question 24:

Let us assume two servlets defined in web.xml and corresponding <load-on-startup> attribute. Which one of the servlet will be loaded in memory first at the time of server startup?

<servlet>
  <servlet-name>AuthenticationServlet</servlet-name>
  <display-name>AuthenticationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthenticationServlet</servlet-class>         
  <load-on-startup>2</load-on-startup>
</servlet>

<servlet>
  <servlet-name>AuthorizationServlet</servlet-name>
  <display-name>AuthorizationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthorizationServlet</servlet-class>
  <load-on-startup>3</load-on-startup>
</servlet>

Servlet will lower value of load-on-startup attribute/tag is loaded first. Hence, AuthenticationServlet will be loaded first and then AuthorizationServlet will be loaded.

Question 25:

Which all protocols are supported by HTTPServlet?

HTTPServlet supports following protocols

  • HTTP
  • HTTPS

Question 26:

Let us assume two servlets defined in web.xml and have equal <load-on-startup> attribute. Which one of the servlet will be loaded in memory first at the time of server startup?

<servlet>
  <servlet-name>AuthenticationServlet</servlet-name>
  <display-name>AuthenticationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthenticationServlet</servlet-class>         
  <load-on-startup>3</load-on-startup>
</servlet>

<servlet>
  <servlet-name>AuthorizationServlet</servlet-name>
  <display-name>AuthorizationServlet</display-name>
  <servlet-class>com.modernpathshala.AuthorizationServlet</servlet-class>
  <load-on-startup>3</load-on-startup>
</servlet>

If two servlets have same/equal load-on-startup value then they are loaded in the sequence defined in web.xml file.

Question 27:

What's the difference between servlet and applet?

The main difference between servlet and applet is, servlet executes at server side whereas applet executes at clients side(browser).

The applet is downloaded to the client, and executes in a JRE inside the browser, and can display whatever it wants to display within the applet frame. The servlet instead runs on the server and produces a HTML page which is displayed in your browser.

Question 28:

Explain the difference between boolean parameter passed to method getSession() as getSession(true) and getSession(false)?

getSession(true) – When you call this method to get session, this method will check whether a session already exists for the user and returns the session and if the session does not exists it will create new session object and return that object.

getSession(false) – When you call this method to get session, this method will check whether a session already exists for the user and returns the session and if the session does not exists it will return null.

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media