Simple servlet example – how to create servlet without eclipse
These days we are all surrounded by heavy frameworks that make life easier for us. Like maven, spring and hibernate and more. But how can we create a simple servlet website without the need of all those.
The main idea of servlet lies around very simple and intuitive files and structures. Knowing those only is good enough to built a good web app and that is what I will be showing here.
The main parts of the java web app are the following.
1. The Servlet – the java class that is handling the POST and GET http verbs
2. the deployment descriptor – web.xml. File telling how the webapp should be interacted like the url pattern
3. Servlet jar file – This is the jar handling the magic of interaction with the web for the servlet.
Structure of the webapp
The web app requires some basic structure to create our simple servlet or any other big ones.
The most important part will be that of the WEB-INF directory where the final compiled classes will be listed. It is a good practice to list the classes and libraries needed to the web application in this folder
META-INF
WEB-INF
–web.xml
–classes
–lib
—-javax.servlet-api-3.1.0.jar
src
–com
—-gullele
——simple
——–LightServlet.java
Steps to create simple servlet
1. Go ahead and download the servlet api from here. Look for download and get the jar
2. Create the above directory structure under simple-servlet folder.
3. Add the following in web.xml file
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Simple Servlet Webapp</display-name>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>com.gullele.simple.LightServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/show</url-pattern>
</servlet-mapping>
</web-app>
The above descriptor is telling when you hit localhost:8080/simpleservlet/show it will go ahead to LightServlet and provide you the content.
4. Add the following content in LightServlet.java
package com.gullele.simple;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
public class LightServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
writer.println("hey");
}
}
This is the code that would be called when you hit /show on your browser. As you can see most of the imports are coming from the jar file that you downloaded and others are from the java.io.
It will simply print hey when you hit localhost:8080/simple/show later when you finish compiling and deploy it on tomcat webserver.
5. Now all should be good assuming you have followed all correctly. The next part is compiling the project and making sure no problem is happening.
Being on your root folder, that is simple-servlet run the following
javac -classpath WEB-INF/lib/javax.servlet-api-3.1.0.jar src/com/gullele/simple/LightServlet.java -d WEB-INF/classes
The above command tells java to compile javac
the servlet and to use the jar file we downloaded while compiling. And the -d will tell where to put the class files for usage. If not provided, by default the current directory is used.
Now our simple servlet example is ready to be packaged as war
6. Run the following command to get the war file – web archive
jar -cvf simple-servlet.war *
Just make sure you are including the whole file for now. In real case you don’t need the actual java file and stuff. But for now, you can add all. The final war file expects the META-INF
and WEB-INF
folders.
Now you have the war file so you can deploy it to the webserver of your choice like tomcat.
here is the source code simple-servlet.zip
here is the war file you can deploy simple-servlet.war