0% found this document useful (0 votes)
37 views

Types of Servlet

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Types of Servlet

Uploaded by

nihalpatil0021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Types of servlet

There are mainly two types of servlets -


1. Generic Servlet - Generic servlet is protocol independent servlet. It implements the Servlet and ServletConfig
interface.
-Generic servlets are sub classes of javax.servlet.GenericServlet .

2 Http Servlet - Http Servlet is HTTP (Hyper Text Transfer Protocol ) specific servlet.

-HTTP servlets are sub classes of javax.servlet.HttpServlet

6.2 Servlet API


Servlet API contains a number of classes and interfaces
1. javax.servlet
2. javax.servlet.http
6.3.1 javax.servlet-
Servlet Interface-
- All servlet must implement the servlet interface.
- This interface defines methods to initialize a servlet,to service request and to remove a servlet from the
server
Method
1.public void init(ServletConfig config)- initializes the servlet.
2.public void service(ServletRequest request,ServletResponse response)- provides response for the
incoming request.
3. public void destroy()- is invoked only once and indicates that servlet is being destroyed.
4. public ServletConfig getServletConfig()- returns the object of ServletConfig.
5. public String getServletInfo() -returns information about servlet such as writer, copyright, version etc.
ServletRequest Interface-
- The ServletRequest Interface is used to handle client request to access a 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.

public ServletContext getServletContext() Returns an object of ServletContext


GenericServlet class-
GenericServlet class can handle any type of request so it is protocol-independent.

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");
}

public void service(ServletRequest req,ServletResponse res)throws IOException,ServletException


{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello simple servlet</b>");
out.print("</body></html>");
}
public void destroy(){System.out.println("servlet is destroyed");}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return "copyright 2007-1010";}
}
Create html file at client side index.html

<a href="hello">Invoke Generic Servlet</a>


3)Compile the servlet-
For compiling the Servlet, jar file is required to be loaded. Different Servers provide different jar files:

Jar file Server

1) servlet-api.jar Apache Tomcat


2) weblogic.jar Weblogic
3) javaee.jar Glassfish
4) javaee.jar JBoss

Two ways to load the jar file


1.set classpath
2. paste the jar file in JRE/lib/ext folder
Put the java file in any folder. After compiling the java file, paste the class file of servlet in WEB-INF/classes directory.
4)Create the deployment descriptor (web.xml file) <web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Description of the elements of web.xml file </web-app>

<web-app> represents the whole application.


<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the servlet.
5) Start the Server and deploy the project
To start Apache Tomcat server, double click on the startup.bat file under apache-tomcat/bin directory.
Steps in simple words-
1. Set variable path
1.CATALINA_HOME- c:\program file\Apache\Tomcat9.0
2.CLASSPATH- c:\program file\Apache\Tomcat9.0\lib\servlet-api.java
3.JAVA_HOME- c:\program file\java\jdk1.8
2. Copy servlet- api.java file from c:\program file\Apache\Tomcat9.0\lib\servlet-api.java folder and paste it in
to c:\program file\java\jdk1.8\jre\lib\ext
3. Create folder in webapp folder of tomcat
e.g sample(Create folder) then WEB_INF(createfolder) then classes (create folder) then create .java file
and compile it so .class file created in same folder
4. Copy web.xml file and paste into sample\WEB_INF
5. Put html file into sample folder
6. Start tomcat service
5 execute means run html file in browser like http://localhost:8080/sample/pass1.html
Generic servlet

Create html file at client side index.html

<a href="hello">Invoke Generic Servlet</a>


import java.io.*;
import javax.servlet.*;

public class First extends GenericServlet{


public void service(ServletRequest req,ServletResponse res) throws IOException,ServletException
{

res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");

}
}

You might also like