Types of Servlet
Types of Servlet
2 Http Servlet - Http Servlet is HTTP (Hyper Text Transfer Protocol ) specific servlet.
Methods Description
public Object getAttribute(String name) Returns the value of the named attribute as an Object.
public int getContentLength( ) Returns the length of the request body in bytes.
public String getContentType( ) Returns the MIME type of the request body.
public String getParameter(String name) Returns the value of a request parameter as a string.
public String getProtocol( ) Returns the name and version of the request protocol.
public int getRemotePort( ) Returns the Internet Protocol (IP) port of the client.
public String getServerName( ) Returns the host name of the server to which the request was sent.
ServletResponse Interface-
The ServletResponse interface defines an object to help a Servlet in sending a response to the client.
ServletContext Interface-
-The object of this interface can be used to get configuration information from web.xml file.
Method Description
public String getInitParameter(String name) Returns the parameter value for the specified parameter name.
public Enumeration getInitParameterNames() Returns the names of the context's initialization parameters.
public void setAttribute(String name,Object sets the given object in the application scope.
object)
public Object getAttribute(String name) Returns the attribute for the specified name.
public void removeAttribute(String name) Removes the attribute with the given name from the servlet
context.
ServletConfig interface
Servlet creates ServletConfig object for each Servlet during initialization, to pass information to the Servlet.
Method Description
public String getInitParameter(String name) Returns the parameter value for the specified parameter name
public Enumeration getInitParameterNames() Returns an enumeration of all the initialization parameter names.
public String getServletName() Returns the name of the servlet.
Method Description
public void init(ServletConfig config) is used to initialize the servlet.
public abstract void service(ServletRequest provides service for the incoming request. It is invoked at each time
request, ServletResponse response) when user requests for a servlet.
public void destroy() is invoked only once throughout the life cycle and indicates that
servlet is being destroyed.
public String getInitParameter(String name) returns the parameter value for the given parameter name.
Steps to create a servlet example-
The servlet example can be created by three ways:
1.By implementing Servlet interface,
2.By inheriting GenericServlet class, (or)
3.By inheriting HttpServlet class
Here, we are going to use apache tomcat server in this example. The steps are as follows:
1. Create a directory structure
2. Create a Servlet
3. Compile the Servlet
4. Create a deployment descriptor
5. Start the server and deploy the project
6. Access the servlet
1)Create a directory structures-
The directory structure defines that where to put the different types of files so that web container may get the
information and respond to the client.
2)Create a Servlet
import java.io.*;
import javax.servlet.*;
public class First implements Servlet{
ServletConfig config=null;
public void init(ServletConfig config){ this.config=config; System.out.println("servlet is
initialized");
}
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");
}
}