Java EE 6 Servlet – TomEE – Caused by: java.lang.IllegalArgumentException: Invalid in servlet mapping

How bad a plain vanilla servlet can go with annotations??? Let’s see what happens if we miss a simple “/”.

So I configured a simple Servlet in eclipse, see the code below

package com.mumz.test.javaee.servlet;

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;

/**
 * Servlet implementation class MySecondServlet.
 */
@WebServlet(urlPatterns={"/second","/secondServlet"})
public class MySecondServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * The Constructor.
     *
     * @see HttpServlet#HttpServlet()
     */
    public MySecondServlet() {
        super();
    }

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException the IO exception
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().println("Welcome to second servlet");
	}
}

I am using Apache TomEE, so I wrote this servlet and started my TomEE.

My console was filled up with below stacktrace:

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MyServlet]]
	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
	at java.util.concurrent.FutureTask.get(FutureTask.java:188)
	at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
	at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:799)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/MyServlet]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
	... 6 more
Caused by: java.lang.IllegalArgumentException: Invalid <url-pattern> secondServlet in servlet mapping
	at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3279)
	at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3254)
	at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1430)
	at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1341)
	at org.apache.tomee.catalina.OpenEJBContextConfig.webConfig(OpenEJBContextConfig.java:363)
	at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:873)
	at org.apache.tomee.catalina.OpenEJBContextConfig.configureStart(OpenEJBContextConfig.java:113)
	at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:371)
	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5355)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	... 6 more

And what is the reason???

In my URL pattern I had forgotten a slash “/”

@WebServlet(urlPatterns={"/second","secondServlet"})
public class MySecondServlet extends HttpServlet

And the correction is:

@WebServlet(urlPatterns={"/second","/secondServlet"})
public class MySecondServlet extends HttpServlet {

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s