Note that there are some explanatory texts on larger screens.

plurals
  1. POEmbedded tomcat 7 servlet 3.0 annotations not working
    primarykey
    data
    text
    <p>I have a stripped down test project which contains a Servlet version 3.0, declared with annotations like so:</p> <pre><code> @WebServlet("/test") public class TestServlet extends HttpServlet { private static final long serialVersionUID = -3010230838088656008L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{ response.getWriter().write("Test"); response.getWriter().flush(); response.getWriter().close(); } } </code></pre> <p>I also have a web.xml file like so:</p> <pre><code>&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt; &lt;servlet&gt; &lt;servlet-name&gt;testServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;g1.TestServlet&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;testServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/testWebXml&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>I've tried to make a JUnit test using Embedded Tomcat 7. When I start the Embedded Tomcat I can only access the servlet via the url-pattern declared in web.xml (/testWebXml). If I try to access it via the url-pattern declared via annotation (/test) it sais 404 page not found.</p> <p>Here's the code for my test:</p> <pre><code> String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.addWebapp("/jerseyTest", new File(webappDirLocation).getAbsolutePath()); tomcat.start(); tomcat.getServer().await(); </code></pre> <p>Just to make sure I've set up my project correctly, I've also installed an actual Tomcat 7 and deployed the war. This time, both web.xml declared url and annotation url for my servlet work ok.</p> <p>So my question is: does anyone know how to make Embedded Tomcat 7 take into account my Servlet 3.0 annotations?</p> <p>I should also state that it's a Maven project, and the pom.xml contains the following dependencies:</p> <pre><code> &lt;dependency&gt; &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt; &lt;artifactId&gt;tomcat-catalina&lt;/artifactId&gt; &lt;version&gt;7.0.29&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt; &lt;artifactId&gt;tomcat-embed-core&lt;/artifactId&gt; &lt;version&gt;7.0.29&lt;/version&gt; &lt;scope&gt;provided&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt; &lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt; &lt;version&gt;7.0.29&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;junit&lt;/groupId&gt; &lt;artifactId&gt;junit&lt;/artifactId&gt; &lt;version&gt;4.8.1&lt;/version&gt; &lt;scope&gt;test&lt;/scope&gt; &lt;/dependency&gt; </code></pre> <h1>== UPDATE ==</h1> <p>Here's an issue that seems similar to this (except the Servlet 3.0 annotation that is not working is on Listener, not Servlet), which has a suggested fix:</p> <p><a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=53903" rel="noreferrer">https://issues.apache.org/bugzilla/show_bug.cgi?id=53903</a></p> <p>I've tried it and it didn't work:</p> <p>Changed the Embedded Tomcat start code to:</p> <pre><code>String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.enableNaming(); tomcat.setPort(8080); Context ctx = tomcat.addWebapp(tomcat.getHost(), "/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); ((StandardJarScanner) ctx.getJarScanner()).setScanAllDirectories(true); tomcat.start(); tomcat.getServer().await(); </code></pre> <p>Other things I've tried, also without success:</p> <ul> <li><p>specifically setting metadata-complete="false" in web.xml "web-app" tag</p></li> <li><p>updating the Maven dependencies to version 7.0.30</p></li> <li><p>debugging the <code>org.apache.catalina.startup.ContextConfig</code> class. There's code there that checks for <code>@WebServlet</code> annotations, it's just that it never gets executed (line 2115). This may be a good way to get to the root of the issue, but the class is pretty big, and I don't have time to do this now. Maybe if someone would be willing to look how this class works, and under which conditions (config params) does it get to correctly check your project's classes for that annotation, it might get to a valid answer.</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload