I am using Eclipse Juno 4.2, Java 1.7 and Tomcat 7. But in my system when I create servlet the web.xml
file doesn't create automatically, but another system it's create automatically web.xml
file. I am totally confused, is there anything to configure?
I also add web.xml
file when I am going to create a dynamic project.
To get access to web. xml in Eclipse, even though it's in the webapps\ch11\WEB-INF directory, you can make it a linked file . To do that, right-click the ServletInPlace project, select New→ File, click the Advanced button, check the “Link to file in the file system” checkbox, and click the Browse button.
Servlets and URL Pathsxml defines mappings between URL paths and the servlets that handle requests with those paths. The web server uses this configuration to identify the servlet to handle a given request and call the class method that corresponds to the request method (e.g. the doGet() method for HTTP GET requests).
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. Look closer at the servlet class you just created, there's a @WebServlet
annotation on it containing all information you specified in New Servlet wizard.
Effectively, this new way of configuring servlets
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {}
does exactly the same as this legacy way of configuring servlets
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
If you still want Eclipse to create a web.xml
entry for some reason, then you should change the Dynamic Web Module version back from 3.0 to 2.5 in Project Facets section of project's properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With