Servlet
- Servlet is a technology which is used to create a web application.
- Servlet is an API that provides many interfaces and classes including documentation.
- Servlet is an interface that must be implemented for creating any Servlet.
- Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests.
- Servlet is a web component that is deployed on the server to create a dynamic web page.
- Before Servlet, CGI (Common Gateway Interface) scripting language was common as a server-side programming language.
Common Gateway Interface (CGI)
CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.
Disadvantages of CGI:-
- If the number of clients increases, it takes more time for sending the response.
- For each request, it starts a process, and the web server is limited to start processes.
- It uses platform dependent language e.g. C, C++, perl.
Advantages of Servlet:-
- Better performance: because it creates a thread for each request, not process.
- Portability: because it uses Java language.
- Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc.
- Secure: because it uses java language.
Servlet vs CGI
Servlet API
- The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet api.
- The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.
- The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
Servlet Interface
Servlet interface provides common behavior to all the servlets. Servlet interface defines methods that all servlets must implement.
Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.
Methods of Servlet Interface
Method |
Description |
public void init(ServletConfig config) |
initializes the servlet. It is the life cycle method of servlet and invoked by the web container only once. |
public void service(ServletRequest request,ServletResponse response) |
provides response for the incoming request. It is invoked at each request by the web container. |
public void destroy() |
is invoked only once and indicates that servlet is being destroyed. |
public ServletConfig getServletConfig() |
returns the object of ServletConfig. |
public String getServletInfo() |
returns information about servlet such as writer, copyright, version etc. |
Program Code Implementing Servlet Interface
First.java |
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 this is an example of 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";}
}
|
web.xml |
<?xml version="1.0" encoding="UTF-8"?>
<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>
<display-name>Simple</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
|
index.html |
<a href="hello">Invoke Servlet</a> |
Output
For more RTU V Sem Advance Java Lab Experiments Click here