Servlet Important Questions



Q)  What is the servlet?
A) 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.
Servlets are to servers what applets are to browsers. Unlike applets, however, servlets have no graphical user interface.

Q)  Whats the advantages using servlets than using CGI?
A) Servlets provide a way to generate dynamic documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension.

Q) What are the uses of Servlets?
A)
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 type or organizational boundaries.

Q) Which pakage provides interfaces and classes for writing servlets?
A)
javax

Q) Whats the Servlet Interfcae?
A)
The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.
Servlets–>Generic Servlet–>HttpServlet–>MyServlet.
The Servlet interface declares, but does not implement, methods that manage the servlet and its communications with clients. Servlet writers provide some

Q) What information that the ServletRequest interface allows the servlet access to?
A)
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.

Q) What information that the ServletResponse interface gives the servlet methods for replying to the client?
A)
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.

Q) What is Servlet chaining?
A)
response object from one servlet is passed as request to another Servlet. Try this example and you will come to know what servlet chaining is all about.
public class ServletA extends HttpServlet
{
public void init(ServletConfig config) throws ServletException {}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException
{
String aValue = request.getParameter(“valueA”);
System.out.println(“ServletA read: “+aValue);
request.setAttribute(“ReadTheValue”,”Yes”);
//do other servlet stuff…just don’t open a write and output to the page….
request.getRequestDispatcher(“/servletB”).forward(req,resp);
}
}
public class ServletB extends HttpServlet
{
public void init(ServletConfig config) throws ServletException {}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException
{
String aValue = request.getParameter(“valueA”);
System.out.println(“ServletB also got the value: “+aValue);
System.out.println(“ServletB also found attribute: “+request.getAttribute(“ReadTheValue”));
//finish the task and write to the page.
}
}

Advantages:
1) Chaining reduces the demand on a single servlet
2) Modular design
3) Enables different complex processes to be maintained by more than one person.
4) Allows steps in a process to be modified (eg servlet 1 -> servlet 2 or servlet 1 -> servlet 3 -> servlet 2, etc)
5) Removes the complexity in a single servlet

Q) Suppose that Myservlet implements SingleThreadModel and there are Local  variables, Instance variables, Class variables,Request attributes,Session attributes and Context attributes. Which among  these variables would be thread safe?
A)
Local,Instance and request variables are thread safe.
I have the following deployed in my web.xml file.
<session-config>
<session-timeout>30</session-timeout>
</session-config>

Q) then I perform setMaxInactiveInterval(2400) for my session object.The First one denotes 30 minutes and second one denoted 40 minutes. After how long would the session expire?
A)
40 minutes. The timeout defined in your DD is used until you redefine it with
setMaxInactiveInterval

Random Posts

  • Core Java Inteview Questions
  • GoTo statement in Java
  • Date Format
  • File Uploading Using Servlet
  • Difference between session.save() and session.saveOrUpdate()

Post a Comment