Overview of the Servlet API
The Servlet 2.3 API consists of two packages: javax.servlet and javax.servlet.http. The base functionality is defined in the javax.servlet package whose classes and interfaces outline a generic, protocol-independent implementation. This means you can use it for non-Web applications, too. Of course, the exam targets the Web, so the HTTP protocol is the only one discussed in this blog. The javax.servlet.http interface defines classes and interfaces that are specific to the Hypertext Transfer Protocol (HTTP).
The javax.servlet Package
The javax.servlet package defines 12 interfaces, 7 classes, and 2 exceptions. These interfaces, classes, and exceptions are as follows.
Interfaces
• Filter:- Preprocessor of the request before it reaches a servlet. It can also be a postprocessor of the response leaving a servlet. It can modify a request or response (for example, change headers), the request to a resource (a servlet or static content), or the response from a resource.
• RequestDispatcher:- This is the servlet version of a redirect. It enables requests to be processed and then forwarded to other components of a Web application, such as another servlet, HTML file, or JSP file.
• Servlet:- Defines the life-cycle methods that are implemented by all servlets.
• ServletConfig:- This class has the methods for accessing the servlet configuration information such as the servlets name (from the web.xml file), the initialization parameters, and the ServletContext object.
• ServletContext:- These methods enable your servlet to communicate with its servlet container. This is how you get the MIME type of a file, dispatch requests, or write to a log file. Notice that this information has application scope. The most important features of the ServletContext are application-scope attributes access, logging, and context initialization parameters.
• ServletContextAttributeListener:- Implementations of this interface receive notifications of changes to the attribute list on the servlet context of a Web application. Supports the handling of the ServletContextAttributeEvent class.
• ServletContextListener:- An interface that supports the handling of the ServletContextEvent class. Defines a set of methods that a servlet uses to communicate with its servlet container. It can get the MIME type of a file, dispatch requests, or write to a log file. Notice that there is one context per "Web application" per Java Virtual Machine. The specification defines a "Web application" as a collection of servlets and content installed under a specific subset of the server's URL namespace, such as /catalog, and possibly installed via a .war file.
• ServletRequest:- This interface forms the base for the class that provides client request information to a servlet. It is protocol-independent.
• ServletResponse:- This interface forms the base for the class that represents the response sent from the servlet to the client.
• SingleThreadModel:- An interface that ensures a given servlet handles only one request at a time.
Classes
• GenericServlet:- An abstract class that implements ServletConfig. It defines a generic, protocol-independent servlet.
• ServletContextAttributeEvent:- This is the event class for notifications about changes to the attributes of the servlet context of a Web application.
• ServletContextEvent:- This is the event class for notifications about changes to the servlet context of a Web application (parent of ServletContextAttributeEvent).
• ServletInputStream:- Provides an input stream for reading binary data from a client request. You can modify it; it already has the readLine method for reading data one line at a time.
• ServletOutputStream:- An abstract class providing an output stream for sending binary data to the client. You print HTML, XML, or other output to the client using ServletOutputStream's print() and println() methods.
• ServletRequestWrapper:- Provides a convenient implementation of the ServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This is where you can modify the behavior of request objects.
• ServletResponseWrapper:- Provides a convenient implementation of the ServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet. This is where you can modify the behavior of response objects.
Exceptions
• ServletException:- Extends java.lang.Exception to provide a base class for defining servlet-related extensions.
• UnavailableException:- Extends ServletException to indicate that a servlet is temporarily or permanently unavailable.
The javax.servlet.http Package
The javax.servlet.http package defines eight interfaces and seven classes. These interfaces and classes are as follows:
Interfaces
The interfaces include the following:
• HttpServletRequest:- Extends javax.servlet.ServletRequest to support HTTP.
• HttpServletResponse:- Extends javax.servlet.ServletResponse to support HTTP.
• HttpSession:- Defines methods that provide access to persistent session-state information.
• HttpSessionActivationListener:- Implemented to handle the HttpSessionActivationEvent.
• HttpSessionAttributeListener:- Implemented to handle the HttpSessionAttributeEvent.
• HttpSessionBindingListener:- Implemented by objects that listen for HttpSessionBindingEvent events.
• HttpSessionListener:- Implemented to handle the HttpSessionEvent.
Classes
The classes include the following:
• Cookie:- Encapsulates HTTP cookies.
• HttpServlet:- An abstract class that extends javax.servlet.GenericServlet to provide support for HTTP.
• HttpServletRequestWrapper:- Provides the capability to wrap and modify incoming HttpServletRequest objects.
• HttpServletResponseWrapper:- Provides the capability to wrap and modify outgoing HttpServletResponse objects.
• HttpSessionBindingEvent:- Extends java.util.EventObject to define an event that is sent to an HttpSessionBindingListener when an object is bound or unbound from the current HttpSession.
• HttpSessionEvent:- Parent class of HttpSessionBindingEvent.
Java Applet is an application designed to transmit on internet to execute on java compatible browsers. Java Servlet is a server side program used to provide services to clients.
Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.
You cannot. HTML is a static file and it cannot interact with a Java Servlet. A Servlet can always redirect to a HTML page but the other way round cannot happen.
Applet: It is run on browser, i mean client side. Servlet: It is run on server, i mean server side.
A Servlet is a Java programming language class used to extend the capabilities of a server. Although servlets can respond to any types of requests, they are commonly used to extend the applications hosted by web serversA Servlet is a Java-based server-side web technology.The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
The Java API is the API for the main Java Library.
Java Servlet is used for Server Side programming for developing Web Applications. It easily employs Database Connectivity. We can also use JSP however it cannot replace a Java Servlet.
Java Applet is an application designed to transmit on internet to execute on java compatible browsers. Java Servlet is a server side program used to provide services to clients.
Networking is a basic action. A servlet in Java is a single part of networking, a single task.
Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.
No. Javascript code can be present inside a JSP but not inside a servlet. A Servlet is a pure java class.
Tomcat is a server. It is used to deploy and run Servlets and not compile them. A Servlet is a java file and has to be compiled just like any other Java Class.
Advance java is the implementation of servlet in web pages.
You cannot. HTML is a static file and it cannot interact with a Java Servlet. A Servlet can always redirect to a HTML page but the other way round cannot happen.
Java Servlet Development Kit is an integrated development kit used to build, test, and deploy Java Servlet applications. JSDK allows most standard Web server such as Netscape servers, IIS, Apache and others to load servlets . JSDK is a suite of software for easing the development of Java servlets.
Perl, php and Java are all examples of programming languages.
API stands for Application Programming Interface It usually referrs to documentation on how a specific class works. It lists,describes, and details the class' methods, properties, constructors, etc.. Here is a link for a the API of the String class in Java: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html