Wednesday 8 June 2011

How to compile and execute servlets on tomcat server using IDE






Now, the complete directory structure of your Project will be automatically created by Eclipse IDE.

Click on Demo2 project, go to Java Resources -> src. Right click on src select New -> Servlet

Give Servlet class name and click Next

click on Next Button



Leave everything else to default and click Finish

Now your Servlet is created, write some code inside it. You can take reference from the code in the picture



Add servlet-api.jar JAR file to your project. Click on Libraries, right click on Web App Libraries select Build Path -> Configure Build Path

Click on Add External JARs



Select servlet-api.jar from Apache Tomcat Directory
select servlet-api click on ok button

This JAR is now added to your project's build path.

Create web.xml file in WEB-INF folder under WebContent

Add the following code in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
 <servlet-name>example</servlet-name>
 <servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>example</servlet-name>
 <url-pattern>/myfirstservlet</url-pattern>
</servlet-mapping>
</web-app>



Now all you have to do is Start the server and run the application.

In case TomCat server is not downloaded then you need to install How to download Tomcat Server

Select required projects. Now we need only current project that is Demo2.


We need to add url pattern at the end of the address bar:
http://localhost:8080/Demo2/myfirstservlet

Again press enter

You may like the following posts:
How to download Tomcat Server
Prerequisite to learn Servlets

No comments:

Post a Comment