•What are Servlets?
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a requests coming from a Web browser or other HTTP client anddatabases or applications on the HTTP server.

Using Servlets, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.

Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer several advantages in comparison with the CGI:-
1. Performance is significantly better.
2. Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request.
3. Servlets are platform-independent because they are written in Java.
4. Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets are trusted.
5. The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.

•Servlets Architecture:-


The following figure depicts a typical servlet architecture scenario:-
1.First the HTTP requests coming to the server are delegated to the servlet container.
2. The servlet container loads the servlet before invoking the service() method.
3. Then the servlet container handles multiple requests by spawning multiple threads, each thread executing the service() method of a single instance of the servlet.

•Use of servlet and some major tasks:-

1.Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
2. Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
3. Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
4. Send the explicit data (i.e., the document) to the clients . This
document can be sent in a variety of formats, including text (HTML or XML),binary , Excel, etc.
5. Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g.,HTML), setting cookies and caching parameters, and other such tasks.

•Example of servlet in java:-

// Import required java libraries
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {private String message;public void init() throws ServletException
// Do required initialization
message = “Hello World”;

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
// Set response content type

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println( message);

public void destroy()
// do nothing.

%d bloggers like this: