Wednesday, December 8, 2021

Servlet Packages & Servlet API

 Servlet Packages

Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification. Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java's enterprise edition, an expanded version of the Java class library that supports large-scale development projects.

These classes implement the Java Servlet and JSP specifications. At the time of writing this tutorial, the versions are Java Servlet 2.5 and JSP 2.1.

Java servlets have been created and compiled just like any other Java class. After you install the servlet packages and add them to your computer's Class path, you can compile servlets with the JDK's Java compiler or any other current compiler.

Servlet API:   There are two packages that are required to build servlets.

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

In general javax.servlet package has all the classes/interfaces not directly tied to HTTP, and javax.servlet.http package has the classes/interfaces that deal with the http protocol.

 1. javax.servlet package:

There are number of interfaces and classes present in this package, they are described below.

Interfaces:

Servlet: Declares life cycle methods for a servlet ServletConfig: Allows servlets to get initialization parameters ServletContext: Enables servlets to log events ServletRequest: Used to read data from a 


Classs:

     GenericServlet: Implements the Servlet and ServletConfig

ServletInputStream: Provides an input stream for reading requests from a client ServletOutputStream: Provides an output stream for writing responses to a client. ServletException: Indicates that a servlet error occurred.


Following are the interfaces and their methods:

Servlet Interface:

void init(): Called when the servlet is initialized

void service(): Called to process a request from a client void destroy(): Called when the servlet is unloaded

ServletConfig getServletConfig(): Returns a ServletConfig object that contains any initialization parameters

String getServletInfo: Returns a string describing the servlet

ServletConfig Interface:

ServletContext getServletContext: Returns the context for this servlet.

String getInitParmeter(String param): Returns the value of the initialization parameter name param.

getInitParameterNames(): Returns all initialization parameter names

 ServletContext interface:

getAtrribute(String attr): Returns the value of the server attribute named attr. String 

getServiceInfo(): Returns information about the server.

Servlet getServlet(String sname): Returns the servlet named sname. getServletNames(): Returns the names of servlets in the server

 ServletRequest Interface:

String getParameter(String pname): Returns the value of the parameter named pname getParameterNames(): Returns the parameter names for this request

String[ ] getParameterValues(): Returns the parameter values for this request String getProtocol(): Returns a description of the protocol

String getServerName(): Returns the name of the serverIntgetServerPort():Returns the port number.

 

ServletResponse Interface:

PrinterWriter getWriter(): Returns a PrintWriter that can be used to write character data to the response.

ServletOutputStream getOutputStream(): Returns a ServletOutputStream that can be used to write binary data to the response.

 Following are the classes and their methods:

GenericServlet class implements Servlet and ServletConfig interface.

Methods: public void init(), public void destroy(), public abstract void service()

 

ServletInputStream class extends InputStream. It is implemented by the server and provides an input stream that a servlet developer can use to read the data from a client request. In addition to this, one more method is added which returns the actual number of bytes read Int readLine(byte[ ] buffer, int offset, int size)

 

ServletOutputStream class extends OutputStream. It defines the print() and println() methods, which output data to the stream.

 

ServletException class indicates that a servlet problem has occurred.

The class has the following contructor ServletException( ) & ServletException(String s)


 

2. javax.servlet.http package:

There are number of classes and interfaces present in this package, they are as follows:

 

Interface

HttpServletRequest: Enables servlets to read data from an HTTP request HttpServletResponse: Enables servlets to write data to an HTTP response. HttpSession: Allows session data to be read and written HttpSessionContext: Allows sessions to be managed

 

Class

HttpServlet: Provides methods to handle HTTP requests and responses Cookie: Allows state information to be stored on a client machine

Following are the interfaces and their methods description HttpServletRequest Interface

Cookie[ ] getCookies: Returns an array of the cookies in this request

String getMethod(): Returns the HTTP method for this request String getQueryString(): Returns any query string in the URL

String getRemoteUser(): Returns the name of the user who issued this request. String getRequestedSessionId(): Returns the ID of the session

String getServletPath(): Returns the part of the URL that identifies the servlet

 

HttpServletResponse Interface

Void addCookie(Cookie cookie): Adds cookie to the HTTP response. Void sendError(int c): Send the error code c to the client

Void sendError(int c , String s): Send the error code c and the message s Void sendRedirect(String url): Redirects the client to url

No comments:

Post a Comment

A simple Java program to find the inverse of a given matrix

  import java.util.Scanner; public class MatrixInverse { public static void main (String[] args) { Scanner scanner =...