Java - Servlet Java class

> Procedural Languages > Java

1 - About

A Servlet is an java object that receives a HTTP request and generates a HTTP response based on that request.

A Java servlet is:

  • a J2ee web component that responds to HTTP requests.
  • a HTTP-specific servlet class in Java EE that conforms to the Java Servlet technology API, a protocol by which a Java class may respond to requests.

Servlets are the Java counterpart to non-Java dynamic Web content technologies such as:

  • Php,
  • ASP,
  • and CGI

They are not tied to a specific client-server protocol, but are most often used with the HTTP protocol. Therefore, the word “Servlet” is often used in the meaning of “HTTP Servlet”.

Servlets are best suited for:

  • service-oriented applications (web service endpoints can be implemented as servlets)
  • and the control functions of a presentation-oriented application, such as dispatching requests and handling nontextual data.
Advertising

3 - Use

Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL rewriting.

Servlets are most often used to

  • process or store data that was submitted from an HTML form
  • provide dynamic content such as the results of a database query
  • manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer.

4 - Example

Note that HttpServlet is a subclass of GenericServlet, an implementation of the Servlet interface. The service() method dispatches requests to methods doGet(), doPost(), doPut(), doDelete(), etc., according to the HTTP request.

4.1 - Simple

Here is a simple servlet that just generates HTML.

import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
//or use
//import java.io.*;
//import javax.servlet.*;
//import javax.servlet.http.*;
 
public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
                "Transitional//EN\">\n" +
                "<html>\n" +
                "<head><title>Hello World</title></head>\n" +
                "<body>\n" +
                "<h1>Hello, world!</h1>\n" +
                "</body></html>");
  }
}

4.2 - With a Bean

With a enterprise bean:

The myServlet class uses dependency injection to obtain a reference to the Bean MyBean. The javax.ejb.EJB (@EJB) annotation is added to the declaration of the private member variable myBean, which is of type MyBean.

In this case, MyBean exposes a local, no-interface view, so the enterprise bean implementation class is the variable type.

@WebServlet
public class myServlet extends HttpServlet {
@EJB
MyBean myBean;
...
}

Note the annotation @webservlet (javax.servlet.annotation.WebServlet)

Advertising

5 - Documentation / Reference

lang/java/servlet.txt · Last modified: 2019/09/30 21:52 by gerardnico