• +91 9971497814
  • info@interviewmaterial.com

Servlet Interview Questions Answers

Question 1 : Can we  use the constructor, instead of init(), to initialize servlet?

Answer 1 : Yes , of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won't have access to a ServletConfig or ServletContext.

Question 2 : Difference between GET and POST ?

Answer 2 : In GET your entire form submission can be encapsulated in one URL, like a hyperlink. query length is limited to 260 characters, not secure, faster, quick and easy. In POST Your name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the form's output. It is used to send a chunk of data to the server to be processed, more versatile, most secure.

Question 3 : Explain the life cycle of Servlet.

Answer 3 : Loaded(by the container for first request or on start up if config file suggests load-on-startup), initialized( using init()), service(service() or doGet() or doPost()..), destroy(destroy()) and unloaded.

Question 4 : Given the request path below, which are context path, servlet path and path info?

Answer 4 : /bookstore/education/index.html context path: /bookstore servlet path: /education path info: /index.html

Question 5 : How can I send user authentication information while making URL Connection?

Answer 5 : You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.

Question 6 : If a servlet is not properly initialized, what exception may be thrown?

Answer 6 : During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.

Question 7 : Once the destroy() method is called by the container, will the servlet be immediately destroyed? What happens to the tasks(threads) that the servlet might be executing at that time?

Answer 7 : Yes, but Before calling the destroy() method, the servlet container waits for the remaining threads that are executing the servlet’s service() method to finish.

Question 8 : Request parameter How to find whether a parameter exists in the request object?

Answer 8 : 1.boolean hasFoo = !(request.getParameter("foo") == null || request.getParameter("foo").equals("")); 2. boolean hasParameter = request.getParameterMap().contains(theParameter); (which works in Servlet 2.3+)

Question 9 : What is filter? Can filter be used as request or response?

Answer 9 : A filter is a reusable piece of code that can transform the content of HTTP requests,responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.

Question 10 : What is new in ServletRequest interface ? (Servlet 2.4)

Answer 10 : The following methods have been added to ServletRequest 2.4 version: public int getRemotePort() public java.lang.String getLocalName() public java.lang.String getLocalAddr() public int getLocalPort()

Servlet Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners