Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting automatically web.xml file while creating servlet in Eclipse Juno 4.2

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.

like image 830
Chintamani Avatar asked Feb 13 '13 13:02

Chintamani


People also ask

How do I get Web xml in Eclipse?

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.

How does URL pattern for servlet work in the Web xml?

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).


1 Answers

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.

like image 150
BalusC Avatar answered Sep 28 '22 21:09

BalusC