Newsletter

Servlet Hello World Example in Eclipse IDE with Tomcat Server

Servlets » on Jan 5, 2013 { 5 Comments } By Sivateja

We will see the first servlet application directly in Eclipse IDE and i am using Tomcat 7, friends doing java servlet with out eclipse is really tedious 😉 we cannot do every thing individually and copying related files into the folders and finally copying the application into tomcat webapps directory bla bla.., So we can do it very easily in Eclipse IDE will see how.

Open Eclipse IDE [ Download any version from Google ], Goto File > New > Dynamic web Project

Give Project name and click on Finish

So you can check the directory structure like this..

We should write .java files in src folder and .class files will be automatically copied into build folder including the package, so i am going to create .java file OnGeneric.java [ First application i am going to tell you using GenericServlet, from the next application i will take HttpServlet only ]

OnGeneric.java Before setting servlet-api.jar

Directory Screen short will showing some errors see….

So we need to create the classpath for servlet-api.jar > right click on ROOT folder [ Servlet-Hello-World-Java4s ] > Properties > now a window will open in front of you > left side click on Java Build Path > and click on Add External Jars and add the servlet-api.jar from the tomcat folder > and click on ok

That’s it,  check now all the errors will be gone 🙂

Now its the time to run our program, for that we should add the server [i will tell you about web.xml later], So in the server window right click and New > Server

Choose tomcat 7 > Next >

Click on Browse > choose Tomcat installation directory > Next

Choose our existing project at left side and click on Add and Finish

Now right click on project [Root] > Run As > Run on Server > Output

See in the URL ‘Servlet-Hello-World-Java4s’ is our servlet application directory, now come to web.xml

We should give the url pattern in the url to get the output…

Servlet Hello World Application

Files required….

  • OnGeneric.java
  • web.xml

Directory Structure

OnGeneric.java

package java4s;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class OnGeneric extends GenericServlet
{
	public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
	{
		res.setContentType("text/html");
		PrintWriter pw=res.getWriter();
		pw.println("This is first servlet program on GenericServlet___Java4s.com");
		pw.close();
	}

}

Explanation

  • We must import required packages javax.servlet.*; and java.io.*
  • And our class should extend GenericServlet or HttpServlet, i am using GenericServlet in this application
  • We must over ride service(-,-) method in our class having request and response object
  • res.setContentType(); means we are setting what type of response we are sending back
  • PrintWriter will be used to write the response, we can get the PrintWriter object from the response object [ res.getWriter() ]
  • That’s it, once we call close() method of PrintWriter, then response will be sent back

Web.xml

<web-app>

     <servlet>
          <servlet-name>first</servlet-name>
          <servlet-class>java4s.OnGeneric</servlet-class>
     </servlet>

     <servlet-mapping>
           <servlet-name>first</servlet-name>
           <url-pattern>/firstHelloWorld</url-pattern>
     </servlet-mapping>

</web-app>

Output i shown you already 🙂

​ ​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “Servlet Hello World Example in Eclipse IDE with Tomcat Server”
  1. xige says:

    I added the url in url pattern. But still get 404 error. I’don’t know why.

  2. Kaveen says:

    I too had the same problem, The solution is
    1) Either create a index.html or index.htm or index.jsp in web content folder. And inside the form tag(),give the action and method type.
    2)Or remove all the tag content

    This works fine for me

  3. Kaveen says:

    Remove all the tag content from Welcome-list tag and write the servlet and servlet-mapping in web.xml file in web content folder

  4. mohan says:

    please post how insert record in database using eclipse ide and also how to configure the database inside the eclipse ide

  5. Ayyappa says:

    Please Keep the jsf material also

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.