A simple servlet that uses Maven, JDBC, and Jetty to say 'Hello world'
First, make a database, user, table and add a record:
CREATE DATABASE mjjs DEFAULT CHARACTER SET utf8;
GRANT ALL ON mjjs.* TO 'mjjsuser'@'localhost' IDENTIFIED BY 'mjjspassword';
GRANT ALL ON mjjs.* TO 'mjjsuser'@'127.0.0.1' IDENTIFIED BY 'mjjspassword';
CREATE TABLE mjjs (name TEXT) ENGINE = InnoDB DEFAULT CHARSET=utf8;
INSERT INTO mjjs (name) VALUES ('tsugi');
If you have changed any of the values in the example SQL above, edit
the file src/main/resources/application.properties
and edit these
properties:
mjjs.datasource.url=jdbc:mysql://localhost:8889/mjjs
mjjs.datasource.username=mjjsuser
mjjs.datasource.password=mjjspassword
mjjs.datasource.driverClassName=com.mysql.jdbc.Driver
mvn clean compile install jetty:run
Then navigate to
http://localhost:8080/mjjs/hello
If you get this message:
Reading /application.properties ...
Your database is missing or inaccessible
Fire up MAMP, go to localhost:8888/phpMyAdmin, click on SQL tab and copy paste these lines of SQL into the text box and hit "Go"
CREATE DATABASE mjjs DEFAULT CHARACTER SET utf8;
GRANT ALL ON mjjs.* TO 'mjjsuser'@'localhost' IDENTIFIED BY 'mjjspassword';
GRANT ALL ON mjjs.* TO 'mjjsuser'@'127.0.0.1' IDENTIFIED BY 'mjjspassword';
Refresh the phpMyAdmin page, and mjjs now appears on the left. Click on the mjjs database and then click on the SQL tab. Copy paste this command into the text box and hit go
CREATE TABLE mjjs (name TEXT) ENGINE = InnoDB DEFAULT CHARSET=utf8;
INSERT INTO mjjs (name) VALUES ('tsugi');
Refresh this page: http://localhost:8080/mjjs/hello
If all goes well, you will see output like the following:
Welcome to hello world!
Reading /application.properties ...
name=tsugi
Successfully read 1 rows from the database
-
The
pom.xml
file controls the build process - it tellsmvn
where the source files are located and what output files to produce. It also tellsmvn
to download library code for the declared code libraries that this application "depends on". -
The
src/main/java/mjjs/HelloServlet.java
contains the source code for our Java Servlet. If you look at the code, you will see methods for doGet() and doPost() - these methods are called when there is a GET or POST to the application's URLs. It defines a class calledmjjs.HelloServlet
that is referenced in the next file. You can debug this program by adding calls toSystem.out.println()
and those print statements will come out on your console. -
The
src/main/webapp/WEB-INF/web.xml
file tells the servlet container (Jetty in this case) which URLs are to be handed to which classes. If you lok at this file, it has two sections - one defines a servlet and maps it to the java class and the other takes a URL pattern and indicates that it needs to be sent to a particular servlet (i.e. which Java class).
These are some online resources to read that explain how things in this servlet works:
- How to Write a Hello World Servlet
- HttpServlet interface - javax.servlet.http.HttpServlet
- Request Object interface - javax.servlet.http.HttpServletRequest
- Response Object Interface - javax.servlet.http.HttpServletResponse
- Lesson: JDBC Basics
- Database Connection interfaces - java.sql
If you end up with this error,
No plugin found for prefix 'jetty' in the current project and in the
plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available
from the repositories