Friday, May 4, 2012

Servlets and JSPs


What is the hierarchy, who calls the doXXX method?
public interface Servlet {
      void service();
      public void init();
      public void destroy();
}
abstract class GenericServlet implements Servlet
public void init(){}
public void destroy(){}
public abstract void service();
public abstract class HttpServlet extends GenericServlet{
     
protected void service(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {

if (method.equals(METHOD_HEAD)) {
                 long lastModified = getLastModified(req);
                 maybeSetLastModified(resp, lastModified);
                 doHead(req, resp);
     
             } else if (method.equals(METHOD_POST)) {
                 doPost(req, resp);
                 
             } else if (method.equals(METHOD_PUT)) {
                 doPut(req, resp);        
                 
             } else if (method.equals(METHOD_DELETE)) {
                 doDelete(req, resp);
                 
             } 
Can servlet have a constructor ?
One can definitely have constructor in servlet. Even you can use the constructor in servlet for initialization purpose, but this type of approach is not so common. You can perform common operations with the constructor as you normally do. The only thing is that you cannot call that constructor explicitly by the new keyword as we normally do. In the case of servlet, servlet container is responsible for instantiating the servlet, so the constructor is also called by servlet container only.
NB: Constructor is called when the servlet object is created and using this obj init() and all doXXX methods will be called (so constructor first, then init)
What are the types of protocols supported by HttpServlet ?
It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. So, HttpServlet only supports HTTP and HTTPS protocol.

What's the difference between GenericServlet and HttpServlet?
GenericServlet
HttpServlet
The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods.
An abstract class that simplifies writing HTTP servlets.
It extends the GenericServlet base class and provides an framework
for handling the HTTP protocol.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers.
The HttpServlet subclass passes generic service method requests
to the relevant doGet() or doPost() method.
GenericServlet is not specific to any protocol.
HttpServlet only supports HTTP and HTTPS protocol.

Why is HttpServlet declared abstract?
The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. This is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet() requests for example, there is no need to write a doPost() method too.

Should I override service()
How do I call one servlet from another servlet?
[ Short answer: there are several ways to do this, including
  • use a RequestDispatcher
  • use a URLConnection or HTTPClient
  • send a redirect
  • call getServletContext().getServlet(name) (deprecated, doesn't work in 2.1+)

How does a servlet communicate with a JSP page?

try {
             govi.FormBean f = new govi.FormBean();
             String id = request.getParameter("id");
             f.setName(request.getParameter("name"));
             f.setAddr(request.getParameter("addr"));
             f.setAge(request.getParameter("age"));
                //use the id to compute 
                //additional bean properties like info 
               //maybe perform a db query, etc.
                // . . .
                f.setPersonalizationInfo(info);
             request.setAttribute("fBean",f);
             getServletConfig().getServletContext().getRequestDispatcher
                       ("/jsp/Bean1.jsp").forward(request, response);
           } catch (Exception ex) {
             . . .
           }

How to Enable Cross Context

It is possible to access variables/object created in one web apps in other webapps, if the <Context crossContext="true"> is set.

Tomcat
Add context.xml inside \webapps\TestWebApps\META-INF

JBoss
C:\Program Files\jboss-5.0.1.GA\server\default\deploy\jbossweb.sar\context.xml

In the source webapps, add the variable in ServletContext
servletContext.setAttribute("GVMap", globalVariablesMap);

In the destination webapps
Map globalVariablesMap = (Map<String, String>)filterConfig.getServletContext().getContext("/taxanswer-webapps").getAttribute("GVMap");





No comments:

Post a Comment