How to Run Servlet in Tomcat?
In this example, we will create a basic servlet that displays a Hello World message from a Java program to the user in the browser without using any Java IDE like Eclipse.
Note:
Running a server like Tomcat to run our servlet. if it is not already installed, you can install it using this article: How to Install Apache Tomcat on Windows?
Basic Terms
GenericServlet
- A servlet class is usually created by extending either the GenericServlet class or its descendant javax.servlet.http.HttpServlet class.
- The GenericServlet class defines a generic protocol-independent servlet, in the sense that it can be extended to provide
implementation of any protocol, such as HTTP, FTP, and SMTP. - It provides implementations of the life cycle methods init() and destroy(), as well as methods in the ServletConfig interface.
HttpServlet
- Provides a framework for handling HTTP requests.
- It provides an implementation for the service() method.
- The service() method in the HttpServlet class reads the method type stored in the HTTP request message and invokes a specific method based on this value.
- Specifically, if the method type is GET, it calls doGet(); if the method type is POST, it calls doPost(); and so on. These are the methods that we need to override.
Steps For Creating Servlet
- Our servlet is a simple servlet designed to handle the HTTP GET method.
- Create the following servlet HelloWorldServlet.java using any text editor like Notepad.
Java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType( "text/html" ); PrintWriter out = response.getWriter(); out.println( "<html><head><title>Hello World Servlet</title></head>" ); out.println( "<body>" ); out.println( "<h1>Hello World!</h1>" ); out.println( "</body></html>" ); out.close(); } } |
- The class HelloWorldServlet extends the HttpServlet class.
- It is written to handle only the GET method. Therefore, only the doGet() method is overridden.
- Finally, it sends the following HTML document.
HTML
< html > < head > < title >Hello World Servlet</ title > </ head > < body > < h1 >Hello World!</ h1 > </ body > </ html > |
Here we are assuming we have installed Tomcat in the D directory of our computer.
Building and Installing Servlet
- To compile our HelloWorldServlet.java file, servlet class files are required.
- Tomcat comes with a .jar file (servlet-api.jar) that contains necessary class files
- You can find this jar file usually in the TOMCAT_HOME\lib directory
- Compile the HelloWorldServlet.java using the following command in the Command Prompt and press enter:
javac HelloWorldServlet.java
- This generates the file HelloWorldServlet.class that contains the byte code for our servlet.
- Create the following directory structure under the webapps subdirectory
- Put the HelloWorldServlet.class file in the TOMCAT_HOME\webapps\net\WEB-INF\classes directory.
- Inform the web server about the existence of this servlet and the URL that will be used to refer to this servlet
- Specify in the TOMCAT_HOME\net\WEB-INF\web.xml file,
XML
< servlet > < servlet-name >HelloWorld</ servlet-name > < servlet-class >HelloWorldServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >HelloWorld</ servlet-name > < url-pattern >/servlet/HelloWorld</ url-pattern > </ servlet-mapping > |
Accessing the servlet
- The complete URL of this servlet will be http://172.16.5.81:8080/net/servlet/HelloWorld where 8080 is the port number.
- Type the following URL in the address bar of your web browser and press enter
http://172.16.5.81:8080/net/servlet/HelloWorld
Contact Us