Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

November 6, 2009

servlet is a piece of code which resides at server side and job is to generate dynamic responces.

Which code line must be set before any of the lines that use the PrintWriter?
setContentType() method must be set before transmitting the actual document.

When a servlet accepts a call from a client, it receives two objects. What are they?
ServeltRequest: which encapsulates the communication from the client to the server.
ServletResponse: which encapsulates the communication from the servlet back to the client.
ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.

What information that the ServletResponse interface gives the servlet methods for replying to the client?
It Allows the servlet to set the content length and MIME type of the reply. Provides an output stream, ServletOutputStream and a Writer through which the servlet can send the reply data.

What information that the ServletRequest interface allows the servlet access to?
Information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. The input stream, ServletInputStream.Servlets use the input stream to get data from clients that use application protocols such as the HTTP POST and PUT methods.

What are the uses of Servlets?
A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task.

What are Java Servlets?

Servlets are generic extensions to Java-enabled servers. Their most common use is to extend Web servers providing a very secure portable and easy-to-use replacement for CGI. A servlet is a dynamically loaded module that services requests from a Web server. It runs entirely inside the Java Virtual Machine. Because the servlet is running on the server side it does not depend on browser compatibility.

Can we call destroy() method on servlets from service method?

destroy() is a servlet life-cycle method called by servlet container to kill the instance of the servlet.

The answer to your question is "Yes". You can call destroy() from within the service(). It will do whatever logic you have in destroy() (cleanup, remove attributes, etc.) but it won't "unload" the servlet instance itself. That can only be done by the container

RE: What is difference between sendRedirect() and forward()..? Which one is faster then other and which ...
When you invoke a forward request the request is sent to another resource on the server without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container. When a sendRedirtect method is invoked it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.

RE: do we have a constructor in servlet ? can we explictly provide a constructor in servlet programme as...
Cetainly we can have a constructor in servlet class but only default constructor. We can not have parametrized constructor in servlet because the servlet gets instantiated by the container dynamically and java does not allow to call parametrized constuctor for dynamically loaded clases.

No comments: