• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How set up in web.xml an index.html running on Tomcat (eclipse project)?

 
Ranch Hand
Posts: 80
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. Im learning javaee so i saw an example in a book but it's not well described.

I have a project in Eclipse running on Tomcat with an index.html file who inside has a web form:



-> This file is on the root level: ../jee7_book_workspace/ej03_ch2/index.html

And this is the code of my servlet (I hid imports):



And I tried with a lot of different codes within the web.xml but neither worked.
-> This was my last try:



So, can someone psl give me a hand with the right web.xml?

Thanks!
 
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomcat expects that web applications will be in JEE standard format. That is, as WARs. Tomcat can handle WARs in their native format (an extended form of JAR file) or in their unzipped format ("exploded" WAR). You can put a WAR anywhere, although the default place to put a WAR directory or WAR file is in TOMCAT_HOME/webapps/. If you put one somewhere else, you have to tell Tomcat where it is, and that's usually too complicated for beginners.

The web.xml file is located in the WAR as the /WEB-INF/web.xml file. Your index.html file would be located in /index.html. Note that these are resource paths, so the top-level "/" means relative to the WAR directory, not the filesystem root. There's no confusion, because basically a WAR is a sandbox to prevent rogue systems from walking up out of the WAR and into sensitive operating-system files or other things that don't belong to it.
 
Saloon Keeper
Posts: 12142
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Pablo Napoli wrote:Im learning javaee


Be careful to note that servlets are only a small part of Java EE. Tomcat is a servlet container, you can't run full-blown enterprise applications in it. For that you need an enterprise container like TomEE or WildFly. However, it sounds like you're mostly interested in servlets, so Tomcat will do for now.

So, can someone psl give me a hand with the right web.xml?


Get rid of the web.xml file. Since Servlets Specification 3.0, you can configure your web application using annotations instead. All you need to do is configure your context path in context.xml and have a dependency on the Servlet API.

If you're using Maven, this is the bare minimum you need to have a servlet that listens to /example-application/example-servlet :

src/main/java/com/example/servlet/ExampleServlet.java

src/main/webapp/META-INF/context.xml

pom.xml
 
Tim Holloway
Saloon Keeper
Posts: 22265
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Tomcat is just fine for probably the majority of web applications out there. It's used for Nexus, it's used for Jenkins, I think Jira runs in Tomcat (it's been a while), and almost every in-house project I've done in my career was Tomcat-based. Servlets and JSPs are only part of the stack, but they're arguably the most important parts. Which is how both Tomcat and jetty have managed to survive and thrive over the years.

Tomcat lacks built-in JavaServer Faces, Java Persistence Architecture (and indeed the rest of EJB3), doesn't have built in JMS, and so forth. But if you only need a few of those features, it's no big deal to package in JSF and JPA libraries into a WAR.

And, of course, while the full-stack servers do implement the parts of the JEE spec that Tomcat lacks, they are expensive. Sometimes in literal money terms, but always in terms of system overhead relative to Tomcat.

So there's no shame in using Tomcat, especially when starting out.

Annotations have indeed made it possible to eliminate much, if not all of the web.xml file, and that's a good thing, since it makes JEE components more re-usable and self-configuring. Still, I'm pretty sure that some web.xml stuff is not supported by annotations, and, in fact, I think that the the welcome-page deployment parameter is one of those things. To use annotations, you need to have Java code to annotate, and as the welcome page is application-wide and perfectly capable of standing alone, I don't know where you would specify it as an annotation, offhand.

In any event, there's no harm in learning how to use web.xml properly. Annotations or not, there are still enough legacy apps using web.xml that being able to deal with it is a useful skill. Besides, web.xml can be used to override annotations in case the default annotations values aren't appropriate to a given situation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic