79) What is servlet?
Ans: Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers.For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.
80) What are the classes and interfaces for servlets?
Ans: There are two packages in servlets and they are javax.servlet and javax.servlet.http.
Servlet Generic Servlet
81) What is the difference between an applet and a servlet?
Ans: a) Servlets are to servers what applets are to browsers.
b) Applets must have graphical user interfaces whereas servlets have no graphical user interfaces.
82) What is the difference between doPost and doGet methods?
Ans: a) doGet() method is used to get information, while doPost( ) method is used for posting information.
b) doGet() requests can’t send large amount of information and is limited to 240-255 characters. However, doPost( )requests passes all of its data, of unlimited length.
c) A doGet( ) request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.
83) What is the life cycle of a servlet?
Ans: Each Servlet has the same life cycle:
a) A server loads and initializes the servlet by init( ) method.
b) The servlet handles zero or more client’s requests through service( ) method.
c) The server removes the servlet through destroy() method.
84) Who is loading the init() method of servlet?
Ans: Web server
85) What are the different servers available for developing and deploying Servlets?
Ans: a) Java Web Server
g) Apache Server
h) Netscape Information Server
i) Web Logic
86) How many ways can we track client and what are they?
Ans: The servlet API provides two ways to track client state and they are:
a) Using Session tracking and b) Using Cookies.
87) What is session tracking and how do you track a user session in servlets?
Ans: Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:
a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password
b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server
c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.
d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.
e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session.maxresidents property
88) What is Server-Side Includes (SSI)?
Ans: Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with an .shtml extension.
89) What are cookies and how will you use them?
Ans: Cookies are a mechanism that a servlet uses to have a client hold a small amount of state-information associated with the user.
a) Create a cookie with the Cookie constructor:
public Cookie(String name, String value)
b) A servlet can send a cookie to the client by passing a Cookie object to the addCookie() method of HttpServletResponse:public void HttpServletResponse.addCookie(Cookie cookie)
c) A servlet retrieves cookies by calling the getCookies() method of HttpServletRequest:
public Cookie[ ] HttpServletRequest.getCookie( ).
90) Is it possible to communicate from an applet to servlet and how many ways and how?
Ans: Yes, there are three ways to communicate from an applet to servlet and they are:
a) HTTP Communication(Text-based and object-based)
b) Socket Communication
c) RMI Communication
(You can say, by using URL object open the connection to server and get the InputStream from
Steps involved for applet-servlet communication:
1) Get the server URL.
URL url = new URL();
2) Connect to the host
URLConnection Con = url.openConnection();
3) Initialize the connection
4) Data will be written to a byte array buffer so that we can tell the server the length of the data.
ByteArrayOutputStream byteout = new ByteArrayOutputStream();
5) Create the OutputStream to be used to write the data to the buffer.
DataOutputStream out = new DataOutputStream(byteout);
91) What is connection pooling?
Ans: With servlets, opening a database connection is a major bottleneck because we are creating and tearing down a new connection for every page request and the time taken to create connection will be more. Creating a connection pool is an ideal approach for a complicated servlet. With a connection pool, we can duplicate only the resources we need to duplicate rather than the entire servlet. A connection pool can also intelligently manage the size of the pool and make sure each connection remains valid. A number of connection pool packages are currently available. Some like DbConnectionBroker are freely available from Java Exchange Works by creating an object that dispenses connections and connection Ids on request.The ConnectionPool class maintains a Hastable, using Connection objects as keys and Boolean values as stored values. The Boolean value indicates whether a connection is in use or not. A program calls getConnection( ) method of the ConnectionPool for getting Connection object it can use; it calls returnConnection( ) to give the connection back to the pool.
92) Why should we go for interservlet communication?
Ans: Servlets running together in the same server communicate with each other in several ways.The three major reasons to use interservlet communication are:
a) Direct servlet manipulation - allows to gain access to the other currently loaded servlets and perform certain tasks (through the ServletContext object)
b) Servlet reuse - allows the servlet to reuse the public methods of another servlet.
c) Servlet collaboration - requires to communicate with each other by sharing specific information (through method invocation)
93) Is it possible to call servlet with parameters in the URL?
Ans: Yes. You can call a servlet with parameters in the syntax as (?Param1 = xxx || m2 = yyy).
94) What is Servlet chaining?
Ans: Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request.In servlet chaining, one servlet’s output is piped to the next servlet’s input. This process continues until the last servlet is reached. Its output is then sent back to the client.
95) How do servlets handle multiple simultaneous requests?
Ans: The server has multiple threads that are available to handle requests. When a request comes in, it is assigned to a thread, which calls a service method (for example: doGet(), doPost( ) and service( ) ) of the servlet. For this reason, a single servlet object can have its service methods called by many threads at once.
Interview Based Questions
1. What are the types of ServletEngines?
Standalone ServletEngine:A standalone engine is a server that includes built-in support for servlets.
Add-on ServletEngine:Its a plug-in to an existing server.It adds servlet support to a server that was not originally designed with servlets in mind.
2.What is the difference between a Generic Servlet and Http Servlet?
Generic Servlet Http Servlet
Class which internally implements An abstract class which acts as a child class both for Servlet and ServletConfig GenericServlet and in addition provides interfaces. some additional methods
like doGet(),doPost(),doDelete() & doPut().
3.What is a Session Id?
It is a unique id assigned by the server to the user when a user first accesses a site or an application ie. when a request is made.
4. List out Differences between CGI Perl and Servlet?
Platform independent Platform dependent.
Language dependent Language independent.
9. What is servlet tunnelling?.
Used in applet to servlet communications, a layer over http is built so as to enable object serialization.
10. What is a cookie?.
Cookies are a way for a server to send some information to a client to store and for the server to later retrieve its data from that client.Web browser supports 20 cookies/host of 4kb each.
16. Servlet Class defines init.
62.ServletRunner options are:
m-maximum no.of connection handlers(100).
t-connection timeout in milliseconds
d-servlet directory (current directory)
s-servlet properties file
63.How many standard ports are available?.
65. What are different ways of Session-Tracking?.
(ii) Hidden Files
(iii) Persistant Cookies
(iv) URL Rewriting.
66. If the browser does not support cookies or if they are disabled, how is session tracking done?.
Session tracking is done by URL Rewriting.
Multiple requests can be handled by a servlet and it also can synchronize them.ex: On-line conferencing.
Servlets have no Graphic User Interface.
We can synchronize the service() method for a major performance impact as multiple requests are involved in case of servlets.
We can make a servlet handle a single client/request by implementing single threadmodel interface.
88. What is the default HttpRequest method?. doGet().
89. What is the life cycle of a servlet?.
Removing Handling zero or more client requests.Loading and Initializing.
84. What is servlet exception?. It indicates that there is a problem in the servlet.