stanfordnlp/CoreNLP · error · javax.servlet.ServletException

Invalid preserveSpacing setting.

Error message

Invalid preserveSpacing setting.

What it means

NERServlet.init() reads the 'preserveSpacing' init-parameter and throws ServletException if it is null or blank. The servlet requires this boolean configuration (parsed case-insensitively as 'true') to start.

Solutions

  1. Add <init-param><param-name>preserveSpacing</param-name><param-value>true</param-value></init-param> to the servlet definition
  2. Set the value to 'true' or 'false' explicitly (any non-blank value; only exact 'true' enables spacing preservation)
  3. Redeploy and confirm init() completes without ServletException

Example fix

<!-- before -->
<init-param><param-name>preserveSpacing</param-name><param-value></param-value></init-param>
<!-- after -->
<init-param><param-name>preserveSpacing</param-name><param-value>true</param-value></init-param>
Defensive patterns

Strategy: validation

Validate before calling

String sp = getServletConfig().getInitParameter("preserveSpacing");
if (sp == null || sp.trim().isEmpty()) throw new ServletException("preserveSpacing init-param required");

Try / catch

try { servlet.init(cfg); } catch (ServletException e) { failDeployment("preserveSpacing not configured"); }

Prevention

When it happens

Trigger: Deploying without an <init-param> named preserveSpacing, or providing it as whitespace/empty string.

Common situations: Missing init-param after refactoring web.xml; environment-specific overlays (production web.xml) omitting the parameter; typos in the param-name.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/8e2d2069327a3ad7. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ie/ner/webapp/NERServlet.java:50

  private String format;
  private boolean spacing;
  private String defaultClassifier;
  private List<String> classifiers = new ArrayList<>();
  private Map<String, CRFClassifier<CoreMap>> ners;

  private static final int MAXIMUM_QUERY_LENGTH = 3000;

  @Override
  public void init() throws ServletException {
    format = getServletConfig().getInitParameter("outputFormat");
    if (format == null || format.trim().isEmpty()) {
      throw new ServletException("Invalid outputFormat setting.");
    }

    String spacingStr = getServletConfig().getInitParameter("preserveSpacing");
    if (spacingStr == null || spacingStr.trim().isEmpty()) {
      throw new ServletException("Invalid preserveSpacing setting.");
    }
    //spacing = Boolean.valueOf(spacingStr).booleanValue();
    spacingStr = spacingStr.trim().toLowerCase();
    spacing = "true".equals(spacingStr);

    String path = getServletContext().getRealPath("/WEB-INF/data/models");
    for (String classifier : new File(path).list()) {
      classifiers.add(classifier);
    }
    // TODO: get this from somewhere more interesting?
    defaultClassifier = classifiers.get(0);

    for (String classifier : classifiers) {
      log(classifier);
    }

    ners = new HashMap<>();
    for (String classifier : classifiers) {

View on GitHub (pinned to 1b7edd19c4)