In this article we will create a Hello World web-application using Java’s Servlets API. It will take just couple minutes if you have Java and Maven installed on your computer.
Servlet is a class for handling HTTP requests. Servlet objects are manipulated by Servlet container (like Tomcat, Jetty, etc.) that handles its lifecycle. It means that developers do not create Servlet instances.
Servlet lifecycle is pretty simple and consists of 3 phases – init, service, destroy. We will talk about it a little bit further.
Servlets Hello World application
The whole application create contains 4 parts:
- configure maven
- create a Servlet
- map the Servlet in the web.xml file
- run your app
1. Create a java Maven project with such structure:
For this project we will need 1 maven dependency (servlet-api) and 1 maven plugin (maven-jetty-plugin). This is how pom.xml will look like:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>servlets2example</groupId> <artifactId>servlets2example</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.26</version> <dependencies> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>el-impl</artifactId> <version>2.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
This plugin is a great choise for small application. It allows to quickly deploy your application to embedded Jetty servlet container.
2. Create a Servlet class that will handle http requests.
Here is the code:
package example; import javax.servlet.*; import java.io.IOException; import java.io.PrintWriter; public class HelloServlet implements Servlet { private ServletConfig servletConfig; public void init(ServletConfig servletConfig) throws ServletException { this.servletConfig = servletConfig; } public ServletConfig getServletConfig() { return servletConfig; } public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { servletResponse.setContentType( "text/html" ); PrintWriter out = servletResponse.getWriter(); out.println( "<html><head>" ); out.println( "<title>A Sample Servlet!</title>" ); out.println( "</head>" ); out.println( "<body>" ); out.println( "<h1>Hello, World!</h1>" ); out.println( "</body></html>" ); out.close(); } public String getServletInfo() { return "Hello Servlet"; } public void destroy() { } }
The most interesting part in this class is method service. It takes 2 arguments:
- ServletRequest servletRequest – stores request data
- ServletResponse servletResponse – handles response output
We use response’s PrintWriter to return the HTML code from our server.
3. Map the Servlet in web.xml
Web.xml is a configuration file that lists Java web-application settings. Among them – servlet mappings:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> <web-app> <servlet> <servlet-name>hello</servlet-name> <servlet-class>example.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
In <servlet> section we set the servlet-name and servlet-class to wire this class to some specific name in configuration. Later, in <servler-mapping> section we use the servlet-name to map out HelloServlet to some URL pattern.
4. Run the application
Now we can use the Jetty plugin to run the app. Just execute this command in project directory:
mvn jetty:run
This command will start a servlet container and this Hello World application will be accessible via URL: http://localhost:8080/servlets2example/hello
When you reach this URL you would see the HTML page that is returned from service method of our Servlet:
Leave a Reply
Be the First to Comment!