Servlets in a Module

You can use servlets or JAX-RS to provide a lightweight web integration or a web endpoint to a browser client. Servlets, rather than REST endpoints or portlets, let you control an application’s entire UI experience. Liferay DXP supports servlet based applications and embeds HTTP Whiteboard for servlets.

Here you’ll examine a servlet sample and create your own servlet based application.

Servlet Sample

The servlet sample uses HTTP Whiteboard to respond to requests at URLs that match the pattern http://localhost:8080/o/blade/servlet/*.

Figure 1: If users visit http://localhost:8080/o/blade/servlet, the servlet sample shows the message Hello World.

Figure 1: If users visit `http://localhost:8080/o/blade/servlet`, the servlet sample shows the message `Hello World`.

Here’s the sample servlet class:

package com.liferay.blade.samples.servlet;


import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.log.LogService;

 * @author Liferay
    immediate = true,
    property = {
    service = Servlet.class
public class BladeServlet extends HttpServlet {

    public void init() throws ServletException {
        _log.log(LogService.LOG_INFO, "BladeServlet init");


    protected void doGet(
            HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

        _log.log(LogService.LOG_INFO, "doGet");


     * Dummy contents
     * @return dummy contents string
    private String _generateSampleHTML() {
        StringBuffer sb = new StringBuffer();

        sb.append("<head><title>Sample HTML</title></head>");
        sb.append("<h2>Hello World</h2>");

        return new String(sb);

     * Write sample HTML
     * @param resp
    private void _writeSampleHTML(HttpServletResponse resp) {
        resp.setContentType("text/html; charset=UTF-8");

        try {
        catch (Exception e) {
            _log.log(LogService.LOG_WARNING, e.getMessage(), e);


    private static final long serialVersionUID = 1L;

    private LogService _log;


The sample servlet class uses the @Component annotation to declare itself an OSGi service of type Servlet. It uses OSGi HTTP Whiteboard to respond to requests at URLs matching http://localhost:8080/o/blade/servlet/*. Since the component’s osgi.http.whiteboard.context.path and osgi.http.whiteboard.servlet.pattern properties configure the servlet mapping, there’s no need to specify one in a WEB-INF/web.xml descriptor.

The Portal web application’s WEB-INF/web.xml defines Liferay’s Module Framework Servlet mapping:

      <servlet-name>Module Framework Servlet</servlet-name>

The servlet mapping starts at URL pattern /o/*. Combined with the @Component property "osgi.http.whiteboard.servlet.pattern=/blade/servlet/*", the servlet sample matches URL pattern /o/blade/servlet/*.

To develop your own servlet, you can copy and modify all (or part) of the Servlet sample module project or create a servlet in your own module.

Creating a Servlet

Here’s how to create your own servlet:

  1. Create a module project.

  2. Add the necessary dependencies. Here they are for Gradle:

    compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.4.0"
    compileOnly group: "org.osgi", name: "org.osgi.service.log", version: "1.4.0"
  3. Create a servlet class that extends javax.servlet.http.HttpServlet.

  4. Add the following @Component annotation.

        property = {
        service = Servlet.class

    service = Servlet.class: Makes the component an OSGi service of type Servlet.

  5. Set the following @Component property values to specify a context path and servlet URL pattern:

    "osgi.http.whiteboard.context.path=/": Sets the servlet’s context. Replace the value with your servlet’s context path.

    "osgi.http.whiteboard.servlet.pattern=/blade/servlet/*": Sets the servlet’s mapping pattern. Replace the value with your servlet’s pattern.

  6. Override HttpServlet methods to implement your servlet’s behavior.

  7. Deploy your module.

Your servlet is up and running. You’re well on your way to delivering custom user experiences using servlets.

Servlet Sample

Servlet Filters



« Introduction to ServletsServlet Filters »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0