1.Which HTTP method are non-idempotent?
A HTTP method is said to be idempotent if it returns the
same result every time. HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are
idempotent method and we should implement our application to make sure these
methods always return same result. HTTP method POST is non-idempotent method
and we should use post method when implementing something that changes with
every request.
2.What is MIME Type?
The “Content-Type” response
header is known as MIME Type. Server sends MIME type to client to let them know
the kind of data it’s sending. It helps client in rendering the data for user.
Some of the mime types are text/html, text/xml, application/xml etc.
We can use ServletContext getMimeType() method to get the correct MIME
type of the file and use it to set the response content type.
3.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.
4.Can we get
PrintWriter and ServletOutputStream both in a servlet?
We can’t get instances of
both PrintWriter and ServletOutputStream in a single servlet method, if we
invoke both the methods; getWriter() and getOutputStream() on response; we will
get
java.lang.IllegalStateException at runtime with message as other method has already been
called for this response.
We can use
ServletResponse getWriter() to get the PrintWriter instance whereas we can use
ServletResponse getOutputStream() method to get the ServletOutputStream object
reference.
5.can we
create deadlock situation in servlet?
We can create deadlock in servlet
by call doPost() method from doGet()
method and doGet() method to doPost() method to create deadlock situation in
servlet.
6.What is the use of servlet wrapper classes?
Servlet HTTP API provides two wrapper classes –
HttpServletRequestWrapper and HttpServletResponseWrapper. These wrapper classes are provided
to help developers with custom implementation of servlet request and response
types. We can extend these classes and override only specific methods we need
to implement for custom request and response objects.7.What is SingleThreadModel interface?
SingleThreadModel interface was provided for thread
safety and it guarantees that no two threads will execute concurrently in the
servlet’s service method. However SingleThreadModel does not solve all thread
safety issues of servlets, thats why this interface is Deprecated
8.How can we
invoke another servlet in a different application?
We can’t use RequestDispatcher to invoke servlet from
another application because it’s specific for the application. If we have to
forward the request to a resource in another application, we can use
ServletResponse sendRedirect() method and provide complete URL of another
servlet. This sends the response to client with response code as 302 to forward
the request to another URL. If we have to send some data also, we can use
cookies that will be part of the servlet response and sent in the request to
another servlet
9.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.
10. Servlets are Thread Safe? How to achieve thread safety in servlets?
HttpServlet init() method
and destroy() method are called only once in servlet life cycle, so we don’t
need to worry about their synchronization. But service methods such as doGet()
or doPost() are getting called in every client request and since servlet uses
multithreading, we should provide thread safety in these methods.
if we have a shared resource then we have to provide thread safety in servlets.
Synchronization is the easiest and most widely used tool
for thread safety in java.
Use of Atomic Wrapper classes from java.util.concurrent.atomic packageUse
of locks from java.util.concurrent.locks package.
Using volatile keyword with variables to make every
thread read the data from memory, not read from thread cache
11.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.
No comments:
Post a Comment