11

I used to use Eclipse Galileo, but now I have Helios. Galileo used to automatically add my servlets to web.xml, but in Helios I have to do this myself.

Is there something I can configure to make this automatic again?

phs
  • 10,687
  • 4
  • 58
  • 84
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145

4 Answers4

7

This hasn't been changed. Most likely you have used New > Class instead of New > Servlet to create the servlet.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I'm sure it's a servlet. I just tried it on my laptop and it works properly, so it's some kind of local problem i guess. Thanks for the answer. – Evgeni Dimitrov Mar 21 '11 at 13:49
  • 1
    Please note that when you have created a dynamic web project with Servlet 3.0 facet, but didn't tick the *Generate deployment descriptor* checkbox at end of project creation wizard, then a `web.xml` won't be created and instead the servlets will get the `@WebServlet` annotation. But your question didn't give the impression that the `web.xml` was physically absent. – BalusC Mar 21 '11 at 13:51
  • Yes, web.xml is here with only . The strange thing is that on my laptop when a make a servlet it works, but it's not in web.xml. If i add the servlet by myself, it works again but it's so confusing for me, how it can work when it's not in web.xml. The other thing I've changed is that I migrated from Tomcat 6 to 7 but it don't have to be a problem... I guess. – Evgeni Dimitrov Mar 21 '11 at 14:33
  • 20
    Since Servlet 3.0, servlets can be mapped by the `@WebServlet` annotation above the class instead of by `web.xml`. Tomcat 7 is a Servlet 3.0 container. – BalusC Mar 21 '11 at 14:35
  • @BalusC - Thanks. What do I need to have, besides `tomcat 7` and `eclipse java ee`, to use annotations like `@webservlet` ? Is there an `annotations jar` like we have in `hibernate` ? I haven't tried tom 7 yet. Still forced to use tom 6 since my textbook uses it. – Erran Morad Apr 26 '14 at 06:27
  • Perhaps useful to clarify BalusC's comments to say that at least currently you can have a `web.xml` (by default for me in Eclipse Mars, it lists `welcome-file`s) **and** have Eclipse use annotations instead of pushing mappings and names into `web.xml`. – ruffin Jun 30 '15 at 15:11
  • @BalusC It might be worth 1.) removing that first comment ("Please note...") if it no longer applies and 2.) Pushing the new info into the answer. I really like your answer [on the question](http://stackoverflow.com/a/14854907/1028230) better, and wonder if it isn't worth closing [this as a dupe](http://meta.stackexchange.com/a/147651/184684) now. ?? – ruffin Jun 30 '15 at 15:12
  • @ruffin: thank you for noting :) – BalusC Jun 30 '15 at 17:06
6

this post will help you ..

https://stackoverflow.com/a/14854907/1847899

"Tomcat 7 is a Servlet 3.0 compatible container. Since Servlet 3.0, the servlets can be configured by @WebServlet annotation on the class without the need for a web.xml configuration entry."

Community
  • 1
  • 1
Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35
4

You need to change some settings on the project:

  1. Create dynamic project
  2. Right-click on name of dynamic project
  3. Create servlet name and click Next (not `Finish)
    • The URL mapping will show
  4. Click on Add and give it the name you used in web.xml (like /mohan)
    • By default it will be name of servlet
  5. Click on remove
  6. Remove the default URL mapping name

Now it should work.

Windle
  • 1,385
  • 2
  • 14
  • 33
abhi_88
  • 62
  • 5
1

you dont need to worry about that Tomcat 7 is a Servlet 3.0 compatible container. Since Servlet 3.0, the servlets can be configured by @WebServlet annotation on the class there is no need for a web.xml configuration entry.If you are still confused just change the "dynamic web module version" to 2.5 instead of 3.0 at this point... new-> dynamic web project. then you can see that your servlets will be configured in web.xml automatically