Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Servlets Interview Questions and Answers

Test your skills through the online practice test: Servlets Quiz Online Practice Test

Intermediate / 1 to 5 years experienced level questions & answers

Ques 1. If a servlet is not properly initialized, what exception may be thrown?

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

Is it helpful? Add Comment View Comments
 

Ques 2. Given the request path below, which are context path, servlet path and path info?

/bookstore/education/index.html

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

Is it helpful? Add Comment View Comments
 

Ques 3. What is filter? Can filter be used as request or response?

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.

Is it helpful? Add Comment View Comments
 

Ques 4. When using servlets to build the HTML, you build a DOCTYPE line, why do you do that?

Is it helpful? Add Comment View Comments
 

Ques 5. What is new in ServletRequest interface ? (Servlet 2.4)

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()

Is it helpful? Add Comment View Comments
 

Ques 6. Request parameter How to find whether a parameter exists in the request object?

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

Is it helpful? Add Comment View Comments
 

Ques 7. How can I send user authentication information while making URL Connection?

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

Is it helpful? Add Comment View Comments
 

Ques 8. Why don't we write a constructor in a servlet?

Container writes a no argument constructor for our servlet.

Is it helpful? Add Comment View Comments
 

Ques 9. When we don't write any constructor for the servlet, how does container create an instance of servlet?

Container creates instance of servlet by calling Class.forName(className).newInstance().

Is it helpful? Add Comment View Comments
 

Ques 10. What is the difference between callling a RequestDispatcher using ServletRequest and ServletContext?

>We can give relative URL when we use ServletRequest and not while using ServletContext.

Is it helpful? Add Comment View Comments
 

Ques 11. What are advantages of servlets over CGI?

In CGI for every request there is a new process started which is quiet an overhead. In servlets JVM stays running and handles each request using a light weight thread. In CGI if there are 5000 request then 5000 CGI program is loaded in memory while in servlets there are 5000 thread and only one copy of the servlet class.

Is it helpful? Add Comment View Comments
 

Ques 12. Can you explain in detail "javax.servlet" package?

javax.servlet package has interfaces and classes which define a framework in which servlets can operate. Let's first make a walk through of all the interfaces and methods and its description.

Interfaces in javax.servlet :-
Servlet Interface: This interface has the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. Following are the method in Servlet interface :-

void destroy( ):- Executed when servlet is unloaded from the web server memory.

ServletConfig getServletConfig() :- Returns back a ServletConfig object that contains initialization data.

String getServletInfo( ):- Returns a string describing the servlet.

init method :- Called for first time when the servlet is initialized by the web server.

void service() method :- Called to process a request from a client.

ServletConfig Interface:- This interface is implemented by the servlet container. Servlet can access any configuration data when its loaded. The methods declared by this interface are summarized here: Following are the methods in ServletConfig interface:-

ServletContext getServletContext():- Gives the servlet context.

String getInitParameter(String param):- Returns the value of the initialization parameter named param.

Enumeration getInitParameterNames():- Returns an enumeration of all initialization parameter names.
String getServletName():- Returns the name of the invoking servlet.

rn

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
©2023 WithoutBook