0

I am using the @WebServlet annotation in my app, like so:

@WebServlet("/tokenize")
public class TokenizerServlet extends HttpServlet {
   ...
}

This does not work in the "J2EE Preview at localhost" within Eclipse. Any request to http://localhost:8080/myApp/tokenizer returns a 404. When mapped from the web.xml, this same URL works fine.

However, when the app is deployed (to Azure, in my case), it does work. So the problem isn't in the code or the config, it's something to do with the preview server in Eclipse.

How do I need to configure the preview server to respect my annotation? I've looked everywhere, and there doesn't seem to be much config to speak of. The root element of my web.xml looks like this:

<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">

Under the "J2EE Preview" node in the project, I have this:

enter image description here

UPDATE --

I never got a solution to this, but I found it so easy to run Tomcat locally, that I just started doing that instead. I'm leaving this question here in case anyone else has the same problem, just so you know you're not alone, and perhaps someone will provide an answer at some point.

Deane
  • 8,269
  • 12
  • 58
  • 108

1 Answers1

0

The concept of annnotation in servlet came into picture while introducing servlet 3.0. Any lower version below wont support annotation. I think in your IDE you have been using a servlet version below 3.0. While creating a dynamic web project it is possible to change this version.

enter image description here

Nithin
  • 748
  • 1
  • 10
  • 27
  • Doesn't my web.xml root tag (provided above) indicate that I am using 3.0? – Deane Dec 28 '17 at 03:45
  • If not, how do I use 3.0? I'm using Eclipse Oxygen -- the latest version. Does it not support 3.0? – Deane Dec 28 '17 at 03:47
  • sorry ... my answer wasnt complete... please check the completed answer – Nithin Dec 28 '17 at 03:54
  • No, 3.0 is the default, and I didn't change it. I just verified under Project Properties -- "Dynamic Web Project" is set to 3.0. – Deane Dec 28 '17 at 03:59
  • What was the server used in eclipse ?? – Nithin Dec 28 '17 at 04:00
  • It's just called the "J2EE Preview Server." – Deane Dec 28 '17 at 04:11
  • I have been using annotations in my recent eclipse dynamic web projects with apache server (never worked J2EE Preview Server before). Usually it avoids the usage of **web.xml** file. Running your app by deleting web.xml (keep a backup) might work in this context. – Nithin Dec 28 '17 at 04:16
  • In your example @WebServlet("/tokenize") acts as a substitute to TokenizerServlet tokenize in **web.xml** if the file is not used – Nithin Dec 28 '17 at 04:34
  • Honestly, I just fired up a Tomcat instance locally and start running it out of there. I have no idea what the problem was with my local Eclipse, but it's probably better than I run in Tomcat anyway. Thanks for your help, all the same. – Deane Dec 28 '17 at 14:44
  • Keeping tomcat thing aside, i think deleting **web.xml** from your current server might work. Felt happy helping you.. – Nithin Dec 28 '17 at 14:56
  • To know more about using apache in eclipse please check this site : http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html – Nithin Dec 28 '17 at 15:11