Help the world stop coronavirus! Stay home!

Prev Next

Servlet / Servlet Interview Questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

What is servlet?

Java Servlet is a server side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.

Advantages of Servlet over CGI.

Servlet technology was introduced to overcome the shortcomings of CGI technology.

  • Servlet provide better performance that CGI in terms of processing time, memory utilization because servlets uses benefits of multithreading and for each request a new thread is created, that is faster than loading creating new Object for each request with CGI.
  • Servlet is platform and system independent, the web application developed with Servlet can be run on any standard web container such as Tomcat, JBoss, Glassfish servers and on operating systems such as Windows, Linux, Unix, Solaris, Mac etc.
  • Servlets are robust because container takes care of life cycle of servlet and we don?t need to worry about memory leaks, security, garbage collection etc.
  • Servlets are easy to maintain and scalable.
Can we retrieve remote client MAC address from the Servlet request?

No. The Remote client's hardware address cannot be retrieve from the servlet request.

How do you logout a session user in servlet?

Calling session.invalidate method logs out the user.

How to protect session cookies in Servlet?

For Java EE 6 (Servlet 3.0) the setHttpOnly and setSecure methods can be used to protect HTTP Cookies.

For older Java versions, there are no API available to directly set the HttpOnly and Secure flags. The workaround is creating a custom SET-COOKIE header.

String sessionId = request.gerSession().getId();
Response.setHeader("SET-COOKIE", "JSESSIONID=" + sessionId + "; HttpOnly; Secure");
Can we call servlet destroy() from service()?

destroy() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destroy(). In case, if you call destroy method from service(), it just execute the code written in the destroy(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.

Difference between out.println and System.out.println in JSP Scriptlet.

out.println prints to the browser and System.out.println() prints to the server console.

What happens when JSTL expression encounter NullPointerException?

The condition will be evaluated to false as JSTL suppresses NullPointerException.


Comments & Discussions