Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to do servlet mapping in eclipse IDE other than manually?

Recently,i have started to develop servlets using Eclipse.Every time i write a servlet program,i need to manually map them into web.xml.Is there any way to do automatic mapping of servlets??.Also the Eclipse asks for URL pattern whenever i create a new servlet file.Why is it asking when it doesnt maps into web.xml by itself??Note:also recommend any useful plugin for servlets/jsp development...

like image 349
PraveenMax Avatar asked Nov 28 '10 16:11

PraveenMax


People also ask

Why my servlet is not working in Eclipse?

Add the server runtime libraries In the window that appears, choose Java Build path on the left. Then choose the Libraries tab in the view that appears. Click the Add Runtime option and then select Server Runtime. Add the server runtime to the web project to fix the HttpServlet not found Eclipse error.

How do I map a servlet in Web XML?

Configuring and Mapping a Servlet This is done using the <servlet> element. Here you give the servlet a name, and writes the class name of the servlet. Second, you map the servlet to a URL or URL pattern. This is done in the <servlet-mapping> element.

Where do I put servlet in Eclipse?

For adding a jar file, right click on your project -> Build Path -> Configure Build Path -> click on Libraries tab in Java Build Path -> click on Add External JARs button -> select the servlet-api. jar file under tomcat/lib -> ok.


2 Answers

Upgrade to Servlet 3.0 (Apache Tomcat 7.0, Glassfish 3, etc), then all you need to do is to add the @WebServlet annotation to the servlet class.

@WebServlet("/foo")
public class FooServlet extends HttpServlet {
    // ...
}

That's it.

If you're still sticking to Servlet 2.5 or older, then you need to create the Servlet class as a Servlet class, not as a Java class. Rightclick project, choose New > Servlet and complete the wizard. This way Eclipse will just autogenerate the necessary web.xml mapping.

alt text

like image 131
BalusC Avatar answered Oct 26 '22 18:10

BalusC


If you want to use web.xml for servlet mapping then you need to select dynamic web facet 2.5 version instead of 3.0 in dynamic web project

like image 28
kiran khairnar Avatar answered Oct 26 '22 18:10

kiran khairnar