What are Servlets?
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
What are the advantages of Servlet over CGI?
Servlet technology was introduced to overcome the shortcomings of CGI technology.
  • Servlets 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.
  • Servlets and 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 maintainable and learning curve is small because all we need to take care is business logic for our application.
Why servlet is mostly used?
Servlets are mostly used because they are platform-independent Java classes and are compiled to platform-neutral byte code. Java byte code can be loaded dynamically into and run by java enabled web server.

What is called servlet container?
A servlet container is a part of Web server that provides network services depends on request and response are sent, MIME based requests and responses. It contains and manages servlets through their life cycle.
What is a servlet context?
Servlet context contains servlet view of Web application in which servlet will be running. By using the context,
  • Log events
  • Obtain URL references to resources
  • Set and Store attributes
What is Pure Servlet?
Pure servlet is servlet which is used to create java objects that can be implemented from javax.servlet.Servlet interface.
What is Servlet Invoker?
Servlet Invoker allows web application to dynamically register new servlet definitions with the servlet tag in the /WEB-INF/web.xml.
What are common tasks performed by Servlet Container?
Servlet containers are also known as web container, for example Tomcat. Some of the important tasks of servlet container are:
  • Communication Support: Servlet Container provides easy way of communication between web client (Browsers) and the servlets and JSPs. Because of container, we don’t need to build a server socket to listen for any request from web client, parse the request and generate response. All these important and complex tasks are done by container and all we need to focus is on business logic for the applications.
  • Lifecycle and Resource Management: Servlet Container takes care of managing the life cycle of servlet. From the loading of servlets into memory, initializing servlets, invoking servlet methods and to destroy them. Container also provides utility like JNDI for resource pooling and management.
  • Multithreading Support: Container creates new thread for every request to the servlet and provide them request and response objects to process. So servlets are not initialized for each request and saves time and memory.
  • JSP Support: JSPs doesn’t look like normal java classes but every JSP in the application is compiled by container and converted to Servlet and then container manages them like other servlets.
  • Miscellaneous Task: Servlet container manages the resource pool, perform memory optimizations, execute garbage collector, provides security configurations, support for multiple applications, hot deployment and several other tasks behind the scene that makes a developer life easier.
What is a filter?
A filter is nothing but a piece of code which can be reusable that will be transforming the content of HTTP requests, response and header information.
How many objects of a servlet is created?
Only one object at the time of first request by servlet or web container.
Who is responsible to create the object of servlet?
The web container or servlet container.
What is different between web server and application server?
A web server responsibility is to handler HTTP requests from client browsers and respond with HTML response. A web server understands HTTP language and runs on HTTP protocol.
Apache Web Server is kind of a web server and then we have specific containers that can execute servlets and JSPs known as servlet container, for example Tomcat.
Application Servers provide additional features such as Enterprise JavaBeans support, JMS Messaging support, Transaction Management etc. So we can say that Application server is a web server with additional functionalities to help developers with enterprise applications.
What is the difference between GET and POST method?
  • GET is a safe method (idempotent) where POST is non-idempotent method.
  • We can send limited data with GET method and it’s sent in the header request URL whereas we can send large amount of data with POST because it’s part of the body.
  • GET method is not secure because data is exposed in the URL and we can easily bookmark it and send similar request again, POST is secure because data is sent in request body and we can’t bookmark it.
  • GET is the default HTTP method whereas we need to specify method as POST to send request with POST method.
  • Hyperlinks in a page uses GET method.
What is difference between ServletConfig and ServletContext?
Some of the differences between ServletConfig and ServletContext are:
  • ServletConfig is a unique object per servlet whereas ServletContext is a unique object for complete application.
  • ServletConfig is used to provide init parameters to the servlet whereas ServletContext is used to provide application level init parameters that all other servlets can use.
  • We can’t set attributes in ServletConfig object whereas we can set attributes in ServletContext that other servlets can use in their implementation.
What is difference between PrintWriter and ServletOutputStream?
PrintWriter is a character-stream class whereas ServletOutputStream is a byte-stream class. We can use PrintWriter to write character based information such as character array and String to the response whereas we can use ServletOutputStream to write byte array data to the response.
We can use ServletResponse getWriter() to get the PrintWriter instance whereas we can use ServletResponse getOutputStream() method to get the ServletOutputStream object reference.
What is difference between GenericServlet and HttpServlet?
The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.
What is called a session?
A session is an object which is used by a servlet and it is used to track user interaction with a web application across multiple HTTP requests.
What is servlet mapping?
Servlet Mapping is an association mapping between servlet and a URL pattern. This is used to map servlets with the requests.
What are the features added in Servlet 2.5?
Following are the features added in Servlet 2.5:
  • Dependency on J2SE 5.0
  • Support for annotations
  • Loading the class
  • Several web.xml
  • Removed restrictions
  • Edge case clarifications
When servlet is loaded?
A servlet can be loaded when:
  • First request is made
  • Auto loading and Server starts up
  • There is a single instance that answers all requests concurrently which saves memory
  • Administrator manually loads.
When Servlet is unloaded?
A servlet is unloaded when:
  • Server shuts down
  • Administrator manually unloads
Is it good idea to create servlet constructor?
We can define a constructor for servlet but I don’t think its of any use because we won’t be having access to the ServletConfig object until unless servlet is initialized by container. Ideally if we have to initialize any resource for servlet, we should override init() method where we can access servlet init parameters using ServletConfig object.
What is servlet attributes and their scope?
Servlet attributes are used for inter-servlet communication, we can set, get and remove attributes in web application. There are three scopes for servlet attributes – request scope, session scope and application scope.
Servlet Request, Http Session and ServletContext interfaces provide methods to get/set/remove attributes from request, session and application scope respectively.
Servlet attributes are different from init parameters defined in web.xml for ServletConfig or ServletContext.
How do we call one servlet from another servlet?
We can use RequestDispatcher forward() method to forward the processing of a request to another servlet. If we want to include the another servlet output to the response, we can use RequestDispatcher include() method.
What are the phases of servlet life cycle?
We know that Servlet Container manages the life cycle of Servlet, there are four phases of servlet life cycle.
  • Servlet Class Loading – When container receives request for a servlet, it first loads the class into memory and calls it’s default no-args constructor.
  • Servlet Class Initialization – Once the servlet class is loaded, container initializes the ServletContext object for the servlet and then invoke it’s init method by passing servlet config object. This is the place where a servlet class transforms from normal class to servlet.
  • Request Handling – Once servlet is initialized, its ready to handle the client requests. For every client request, servlet container spawns a new thread and invokes the service() method by passing the request and response object reference.
  • Removal from Service – When container stops or we stop the application, servlet container destroys the servlet class by invoking it’s destroy() method.
What are life cycle methods of a servlet?
Servlet Life Cycle consists of three methods:
public void init(ServletConfig config) – This method is used by container to initialize the servlet, this method is invoked only once in the lifecycle of servlet.
public void service(ServletRequest request, ServletResponse response) – This method is called once for every request, container can’t invoke service() method until unless init() method is executed.
public void destroy() – This method is invoked once when servlet is unloaded from memory.
What is URL Encoding?
URL Encoding is the process of converting data into CGI form so that it can travel across the network without any issues. URL Encoding strip the white spaces and replace special characters with escape characters. We can use java.net.URLEncoder.encode(String str, String unicode) to encode a String. URL Decoding is the reverse process of encoding and we can use java.net.URLDecoder.decode(String str, String unicode) to decode the encoded string.
What are different methods of session management in servlets?
Session is a conversional state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.
Some of the common ways of session management in servlets are:
  • User Authentication
  • HTML Hidden Field
  • Cookies
  • URL Rewriting
  • Session Management API
What is URL Rewriting?
We can use HttpSession for session management in servlets but it works with Cookies and we can disable the cookie in client browser. Servlet API provides support for URL rewriting that we can use to manage session in this case.
The best part is that from coding point of view, it’s very easy to use and involves one step – encoding the URL. Another good thing with Servlet URL Encoding is that it’s a fallback approach and it kicks in only if browser cookies are disabled.
We can encode URL with HttpServletResponse encodeURL() method and if we have to redirect the request to another resource and we want to provide session information, we can use encodeRedirectURL() method.
What is the difference between encodeRedirectUrl and encodeURL?
HttpServletResponse provide method to encode URL in HTML hyperlinks so that the special characters and white spaces are escaped and append session id to the URL. It behaves similar to URLEncoder encode method with additional process to append jsessionid parameter at the end of the URL.
However HttpServletResponse encodeRedirectUrl() method is used specially for encode the redirect URL in response.
So when we are providing URL rewriting support, for hyperlinks in HTML response, we should use encodeURL() method whereas for redirect URL we should use encodeRedirectUrl() method.
What are the different mode that servlets can be used?
Following are the modes that servlets can be used:
  • Filter chains can be used to collect servlets together
  • Support HTTP protocol
  • Used for CGI based applications
  • Dynamic generation of servlets
Request to Download PDF

Post A Comment: