:::: MENU ::::

Hands On Java Servlet With Eclipse and Apache Tomcat

Hands On Java Servlet With Eclipse and Apache Tomcat

This is a continuation of our previous article Servlet Introduction. if you haven’t gone through that I would recommend you to quickly look at that first and then come back here.

Today we will create a Servlet which simply prints “Hello World!”. Later we will start adding parameters to our request and will try to print that. We will use other features of Servlet like session and context in our subsequent tutorials.

Create a Dynamic Web Project

Now quickly we will create a new Dynamic Web Project called com.codestrive.servletTest by selecting FileNewOtherWebDynamic Web Project.

Hands On Java Servlet With Eclipse and Apache Tomcat

Click Next, you will get the screen where you have to give your project a name.

Hands On Java Servlet With Eclipse and Apache Tomcat

Here you can press Finish, Eclipse may ask you, to switch to the Java EE Perspective, You can choose Yes. But I will recommend you to click on Next now. You will get the next screen.

Hands On Java Servlet With Eclipse and Apache Tomcat

Here Eclipse shows us the packages which will stay inside src folder and what’s our output folder would be. We will click Next and the next screen comes up.

Hands On Java Servlet With Eclipse and Apache Tomcat

Here it tells what’s our context root and what’s our content directory. We can name a different content directory for our use. Eclipse will create that and put all the source content inside that.
Now you can click Finish.

Create A Servlet

Now we will create our First Servlet named ServletTest inside com.codestrive.servletTest.servlets package. Right click on the project com.codestrive.servletTestNewServlet OR if you couldn’t find it there try com.codestrive.servletTestNewOtherWebServlet. Follow along the illustration below.

Hands On Java Servlet With Eclipse and Apache Tomcat

Click Finish, It will create a java class and open it up.

You could also create a servlet without the wizard. The wizard creates a Java class which extends the javax.servlet.http.HttpServlet and adds the servlet settings to the web.xml file.
Now we will replace everything with the following code.

The ServletTest.java

Run Your Servlet

Now select your newly created servlet, right-click on it and select Run AsRun on Server.
Select your server and include your servlet so that it runs on the server. Please follow the below illustrations to know how. If don’t have any server, quickly configure one and if you have a problem with that check this article over here.

Hands On Java Servlet With Eclipse and Apache Tomcat

Check the checkbox for trouble free future and click Finish to get the next screen.

Hands On Java Servlet With Eclipse and Apache Tomcat

Add your project to Configured tab and click Finish. The Server will load the Servlet and in your Eclipse console you will be getting Hello World! message. That means your First Servlet worked fine. Make sure there is no other server running in your local system on the port 8080 or else the server won’t start.

Lets Understand What’s Happening

Now we will check what we have done in our code. Our ServletTest class is having three methods inside – one constructor and two service methods. We can configure the servlet while it initialize itself through the constructor. Basic database connection is an example that we can do with it. Now in our service method, doGet() is the default method that gets called when we don’t specify any method type. doPost() will be fired if we deal with a form and we post the data with post method.

service methods are defined in HttpServlet and if we don’t override that, we will get an exception. In any servlet, whichever the method we are using, we must override it as per our use. Here we are simply printing Hello World into the console with doGet(). We can do things with its parameter request and response, you can check it in our next tutorial.

Now how servlet knows that while we hit this URL(http://localhost:8080/com.codestrive.servletTest/ServletTest) into the browser, our particular servlet should handle that? The answer is it is already configured the mapping in the web.xml. web.xml is the file where servlet container looks for the configuration details while it starts up. As I am using Tomcat6, so annotation feature is not there. I am depending on xml configuration for the moment. But we will check the annotation part also in future tutorials. Lets quickly understand how its configured in the web.xml file. Come to Web ContentWEB-INFweb.xml and open it.

The web.xml

Here we can see XML doctype first. Then the project name itself, then the welcome file list starts. If we hit http://localhost:8080/com.codestrive.servletTest/ in URL bar, the tomcat will try to fetch the first file which is available from the given list. If it doesn’t find any of them related to the project, it will give 404 error. Generally it searches from top to toe.

After that the servlet node and the related servlet-mapping node is placed. The main thing is both are pointing to the same servlet if we check carefully. The first one defines a display name, the actual name and the actual class path while the second maps it with the actual name and defines its URL pattern. It tells the container that whenever the URL matches with the same value, push the request to the servlet that is specified here as a servlet-name.

Conclusion

That is all for now, I think we have understood the basic flow of Java Servlet till now. In our next tutorial we will go for handling parameter values with different methods, storing variables into the session and the context object.

Thanks for reading.

Indrajit Das
Follow me

Indrajit Das

Co-founder of CodeStrive [ Kind of a seeker for new dimensions in WEB world, sort of a Thinker, Blogger, Die-Hard Coder, Wash-Room Singer, Football Player, Gamer, Guitar Player, Cartoonist and the list goes on.. ]
Indrajit Das
Follow me

Latest posts by Indrajit Das (see all)

Factory Design Pattern
Java Servlet Introduction, Overview and Life Cycle
Pride that quarells at self-breath