Open In App

Servlet – Packages

Improve
Improve
Like Article
Like
Save
Share
Report

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. 

A package in servlets contains numerous classes and interfaces 

Remember: In order to create servlet in packages, use command ” 

Types of Packages

There are two types of packages in Java Servlet that are providing various functioning features to servlet Applications. The two packages are as follows:

  1. javax.servlet package
  2. javax.servlet.http package

Type 1: javax.servlet package: This package of Servlet contains many servlet interfaces and classes which are capacity of handling any types of protocol sAnd This javax.servlet package containing large interfaces and classes that are invoked by the servlet or web server container as they are not specified with any protocol.

Type 2: javax.servlet.http package: This package of servlet contains more interfaces and classes which are capable of handling any specified http types of protocols on the servlet. This javax.servlet.http package containing many interfaces and classes that are used for http requests only for servlet

Interfaces and Classes in javax.servlet package 

Interfaces in javax.servlet package

  1. Servlet
  2. ServletRequest
  3. ServletResponse
  4. RequestDispatcher
  5. ServletConfig
  6. ServletContext
  7. SingleThreadModel
  8. Filter
  9. FilterConfig
  10. FilterChain
  11. ServletRequestListener
  12. ServletRequestAttributeListener
  13. ServletContextListener
  14. ServletContextAttributeListener

Classes in javax.servlet package

The Classes are in javax.servlet package are listed below:

  1. GenericServlet
  2. ServletInputStream
  3. ServletOutputStream
  4. ServletRequestWrapper
  5. ServletResponseWrapper
  6. ServletRequestEvent
  7. ServletContextEvent
  8. ServletRequestAttributeEvent
  9. ServletContextAttributeEvent
  10. ServletException
  11. UnavailableException

Servlet: This interface describes and connects all the methods that a Servlet must implement. It includes many methods to initialize the destroy of the Servlet, and a general (service()) method which is handling all the requests are made to it. This Servlet interface is used to creating this servlet class as this class having featuring to implementing these interfaces either directly or indirectly to within it on to fetching servlets

ServletRequest: This ServletRequest interface in which examining the methods for all objects as encapsulating data information about its all requests i.e. made to the servers, this object of the ServletRequest interface is used to retrieve the information data from the user

ServletResponse: An interface examining the methods for all objects which are returning their allowed responses from the servers and object of this current interfacing objects is used to estimate the response to the end-user on the system

ServletConfig: declaring this interface ServletConfig useful to gaining accessing the configuration of its main parameters which are passing through the Servlets during the phase time of initialization and this ServletConfig object is used for providing the information data to the servlet classes external to explicitly.

ServletContext: The object of the ServletContext interface is very helpful to featuring the info. data to the web applications are explaining to it for servlets

GenericServlet: This is a generic classes examination to implement the Servlet. if you want to write the Servlet’s protocols other than the HTTP, then the easy way of doing this is to extend GenericServlet rather than by directly implementing the Servlet interfaces

ServletException: it is an exception that can be thrown when the Servlet invoking a problem of some examples

ServletInputStream: This class ServletInputStream is used to reading the binary data from end user request

ServletContextEvent: in this any changes are made in the servlet context of its web application, this class notifies it to the end-user.

ServletOutputStream: This class ServletOutputStream is useful to send the transferring binary data to the end-user side of the system

Interfaces And Classes in javax.servlet.http package 

Interfaces: The javax.servlet.http packages have provides these feature classes that are unique to handling these HTTP requests allowing from it. It provides the HttpServlet classes that is usable as it accesses the selectively interfaces from javax.servlet class.

Interfaces in javax.servlet.http

  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. HttpSessionListener
  5. HttpSessionAttributeListener
  6. HttpSessionBindingListener
  7. HttpSessionActivationListener
  8. HttpSessionContext (deprecated now)
  9. HttpServletRequest – as the extension to ServletRequest interface is using for features specified to HTTP
  10. HttpServletResponse – as this extension to ServletResponse interface is using for functions are similar to HTTP
  11. HttpSession – this interface featuring the accessing to the sessions of tracking for API
  12. HttpSessionAttributeListener – This interface notifies if any changes/edits are prefetched in this HttpSession attribute
  13. HttpSessionListener – This HttpSessionListener interface notified any changes/edits are prefetched in this interface HttpSession lifecycle span process

Classes in javax.servlet.http package

  1. HttpServlet
  2. Cookie
  3. HttpServletRequestWrapper
  4. HttpServletResponseWrapper
  5. HttpSessionEvent
  6. HttpSessionBindingEvent
  7. HttpUtils (deprecated now)

HttpServlet: in this HttpServlet purely abstracted class having features as functionality to extending and applying on the HTTP requests. They have like Service() method that is declared in the Servlet interfaces will now call its methods similar to doGet() and the doPost(), which are enabled to providing behavior to the Calling Servlet

Cookie: This Class provides the feature Servlet an interface for the storage of small portions of data information on the end-user computer or system.

HttpServletRequestWrapper and HttpServletResponseWrapper: this two wrapper classes allowing capability of the HttpServletResponse and HttpServletRequest interfaces to the servlet by its functions

HttpSessionEvent: This class HttpSessionEvent notified as any activity or changes/editing are encountered in the session of web applications in servlet

HttpSessionBindingEvent: This class notified when any attribute is bounded, unbounded or replaced in any Current session

Implementation: Example on servlet by implements Servlet Interface is as follows: 

File: gfg.html

HTML




// gfg.html code
<html>
    <body>
        <form action="./go">
            <input type="submit" value="Submit"/>
        </form>
      <title><p1>Welcome to MyFirstServlet</p1></title>
     </body>
</html>


we created a submit button as – Submit

File: page.xml

XML




<web-app>
  
  <servlet>
    <servlet-name>Demo</servlet-name>
     
    <servlet-class>Demo</servlet-class>
  </servlet>
  
  <servlet-mapping>
     
    <servlet-name>Demo</servlet-name>
     
    
    <url-pattern>/go</url-pattern>
     
  </servlet-mapping>
</web-app>


we checking that the URL name should be match with an HTML form that is/go use in the code

File: MyFirstServlet.java

Java




// Java Program to Illustrate Usage of Servlet-packages
 
// Importing required classes
import java.io.*;
import javax.servlet.*;
 
// Class
public class MyFirstServlet implements Servlet {
 
    ServletConfig config = null;
 
    // Method 1
    public void init(ServletConfig config)
    {
        // this keyword refers to current instance itself
        this.config = config;
 
        System.out.println("servlet is initialized:::"
                           + config);
    }
 
    // Method 2
    public void service(ServletRequest request,
                        ServletResponse response)
        throws IOException, ServletException
    {
 
        response.setContentType("text/html");
 
        // Response interface of servlet is use
        PrintWriter out = response.getWriter();
 
        out.print("<html><body>");
 
<h1><b>Welcome to GFG PORTAL</b></h1>");
out.print("
<h2><b> Example of Servlet Interface <b></h2>");
out.print("</body></html>");
    }
 
    // Method 3
    public void destroy()
    {
        System.out.println("servlet is destroyed now");
    }
 
    // Method 4
    public ServletConfig getServletConfig()
    {
        return config;
    }
 
    // Method 5
    public String getServletInfo()
    {
        return "copyright 2007-2011";
    }
}


Output: Following have the page when user clicking on the button to 

Java




// Java Program to Illustrate Usage of Servlet-packages
 
// Importing required classes
import java.io.*;
import javax.servlet.*;
 
// Class
public class MyFirstServlet implements Servlet {
 
    ServletConfig config = null;
 
    // Method 1
    public void init(ServletConfig config)
    {
        // this keyword refers to current instance itself
        this.config = config;
 
        System.out.println("servlet is initialized:::"
                           + config);
    }
 
    // Method 2
    public void service(ServletRequest request,
                        ServletResponse response)
        throws IOException, ServletException
    {
 
        response.setContentType("text/html");
 
        // Response interface of servlet is use
        PrintWriter out = response.getWriter();
 
        out.print("<html><body>");
 
<h1><b>Welcome to GFG PORTAL</b></h1>");
out.print("
<h2><b> Example of Servlet Interface <b></h2>");
out.print("</body></html>");
    }
 
    // Method 3
    public void destroy()
    {
        System.out.println("servlet is destroyed now");
    }
 
    // Method 4
    public ServletConfig getServletConfig()
    {
        return config;
    }
 
    // Method 5
    public String getServletInfo()
    {
        return "copyright 2007-2011";
    }
}


 to the page

After the Servlet Properly  The output will be shown as shown below:



Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads