How to use a custom plain old Java servlet alongside CUBA

Hello,

I’m trying to use a custom servlet for a specific URL within my CUBA app. I’ve tried using the following class (in the web module source):

@WebServlet("/example")
public class ExampleServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //...
    }
}

The servlet doesn’t even get registered (the @WebServlet annotation seems to be useless). So I tried defining the servlet in web.xml instead of using annotations. The servlet does now get registered (I can see it in getServletContext().getServletRegistrations()) but any requests to /app/example are still just getting redirected back to /app/, even when logged in to an existing session.

Is there any way to use a custom servlet like this?

1 Like

Hi,

you can add your custom servlet to web.xml file with explicit servlet mapping:


<servlet>
    <servlet-name>mydemo</servlet-name>
    <servlet-class>com.company.DemoServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>mydemo</servlet-name>
    <url-pattern>/mydemo/*</url-pattern>
</servlet-mapping>

In this case you have to use URL in the following form: http://domain:port/mydemo/ with the trailing slash since URLs without it are consumed by the Vaadin servlet mapped as /*.

Also I recommend to use Spring MVC Controllers instead of plain-old servlets. You can define them or setup component-scan in dispatcher-spring.xml file. All Spring MVC controllers are available under /dispatch/ path.

Hi I want to use servlet in my cuba application for SMS (long code) service integration(done).

We are sending a SMS to some number with some fixed unique code and that intern will we calling some url of our application.

we were trying with rest controller(custom) but SMS vendor is telling to use servlet because they will give the request in the form of POST request with HttpServletRequest request .

inorder to handle this I have created servlet mapping inside web.xml (portal module) as below

<servlet>
	    <servlet-name>mydemo</servlet-name>
	    <servlet-class>com.company.saas.portal.restApis.Posturl</servlet-class>
	</servlet>
	<servlet-mapping>
	    <servlet-name>mydemo</servlet-name>
	    <url-pattern>/mydemo/*</url-pattern>
	</servlet-mapping>

and below is my vendor code for processing the request

package com.company.saas.portal.restApis;


import java.io.IOException;
 
import javax.servlet.ServletException;
 
import javax.servlet.annotation.WebServlet;
 
import javax.servlet.http.HttpServlet;
 
import javax.servlet.http.HttpServletRequest;
 
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
 
@WebServlet(urlPatterns = {"/mydemo"})
public class Posturl extends HttpServlet {
 
  private Logger log = LoggerFactory.getLogger(UserRegistrationHandler.class);
	
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 
  response.setContentType("text/html;charset=UTF-8");
  
  log.debug("Welcome to servlet");
 
  try {
 
  String sender = request.getParameter("sender");
 
  String keyword = request.getParameter("keyword");
 
  String content = request.getParameter("content");
 
  String comments = request.getParameter("comments");
 
  String inNumber = request.getParameter("inNumber");
 
  String email = request.getParameter("email");
 
  String credits = request.getParameter("credits");
 
  System.out.println("content:" + content + ",keyword:" + keyword + ",inNumber:" + inNumber);
 
  } catch (Exception e) {
 
  log.debug("Error : "+e.getMessage());
 
  e.printStackTrace();
 
  }
 
  }
 
 
  @Override
 
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
 
  throws ServletException, IOException {
 
  processRequest(request, response);
 
  }
 
 
  @Override
 
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
 
  throws ServletException, IOException {
 
  processRequest(request, response);
 
  }
 
  /**
 
  * Returns a short description of the servlet.
 
  *
 
  * @return a String containing servlet description
 
  */
 
  @Override
 
  public String getServletInfo() {
 
  return "Short description";
 
  }
 
}

Now what should be the URL to call/invoke this servlet

Thaks
Shanur