Java - Servlet Java class

Java Conceptuel Diagram

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.

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.

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.

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

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)

Documentation / Reference





Discover More
Card Puncher Data Processing
Design Pattern - Dependency Injection

Dependency injection is: a dependency resolution mechanism where components (object, bean, client) are given their (type) dependencies (service, ..) (Dependencies are injected) and therefore are...
Java Conceptuel Diagram
J2EE - Web.xml - How do I define an environment variable that can be accessed in all servlets in a Web application ?

A J2EE application may require one or more initialization parameters to be accessed by one or more servlets in a web application. An initialization parameter be for a context or for a specific servlet....
Card Puncher Data Processing
JPA - Entity Manager

To manage entities in your persistence application, you need to obtain an javax/persistence/EntityManagerentity manager from an javax/persistence/EntityManagerFactoryEntityManagerFactory. An javax/persistence/EntityManagerEntityManager...
Java Conceptuel Diagram
Java - ( JDK | J2EE | J2SE )

SDK in Java core APIs for writing J2EE components, core development tools, and the Java virtual machine JDBC API 2.0 The Java 2 Platform, Standard Edition (J2SE) SDK is required to run...
J2ee Server
Java - Application Server

application server in Java A J2EE Server is application server on the J2EE platform. It contains the web-tier and the business tier where the components are placed in logical container. Tier Components...
J2ee Server
Java - Container

A container is a logical part of a J2EE Server which contains java components Before any component can be executed, it must be assembled. The assembly process involves per logical container: the...
Java Conceptuel Diagram
Java - Deployment descriptors (Metadata)

Deployment descriptors are XML documents included in the JARs that describes: component's configuration, deployment settings of an application, a module, or a component of and instructions to the...
Java Conceptuel Diagram
Java - J2EE Web components

J2EE Web components can be either: Java Servlet JavaServer Pages (JSP) or web pages implemented with JavaServer Faces technology, web service endpoints Servlets are Java programming language...
Java Conceptuel Diagram
Java - JavaServer Pages (JSP)

JavaServer Pages technology lets you put: snippets of servlet code directly into a text-based document. A JSP page is a text-based document that contains two types of text: static template...
Java Conceptuel Diagram
Java - Jetty (Web Server)

is a java web server Jetty provides: an HTTP server (to send back static content), HTTP client (to make http request), and javax.servlet container (to send back...



Share this page:
Follow us:
Task Runner