Creating the Eclipse Project
Contents
For this chapter, you should be familiar with Eclipse. There are two options from which you can choose how you create this project. One is to create a Maven project and skip archetype selection. The other option is to create a “Dynamic Web Project” for which you need a local Tomcat installation. You should go for option one, by any means, because the following pom.xml
listing creates the complete project scaffolding for you:
<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>org.psychomechanics.iot</groupId> <artifactId>arduin_recv</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>arduin_recv</name> <properties> <failOnMissingWebXml>false</failOnMissingWebXml> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <finalName>ArduinoRecv</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <inherited>true</inherited> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Tomcat plugin --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://192.168.1.105:8080/manager/text</url> <update>true</update> <!-- *(From maven > settings.xml)* --> <server>TomcatArduinoServer</server> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
The only class that is needed in your project is a servlet which listens to the URL from our ESP-01 code (http://<your_server_ip>:<port>/arduin_recv/ArduinoRecvServlet). As you can see, there are already two components in its context path which is arduin_recv
and then ArduinoRecvServlet
. Easy enough to guess that the servlet class ought to be named ArduinoRecvServlet because then the preset path in the @WebServlet annotation will already match. Somewhat more difficult might be to locate the arduin_recv
part of the context path. Where did we define that one? Under normal circumstances, the name is recruited from the <build><finalName>
element in the pom.xml.
package org.psychomechanics.iot.arduin_recv; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ArduinoRecvServlet */ @WebServlet("/ArduinoRecvServlet") public class ArduinoRecvServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ArduinoRecvServlet() { super(); // TODO Auto-generated constructor stub } /** * Usage: http://<your-servers-ip-address>:<port>/arduin_recv/ArduinoRecvServlet?GPIO0=123 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Path FILE_PATH = Paths.get(getServletContext().getRealPath("/"), "temp.txt"); String text = request.getParameter("GPIO0") + "\n"; //Writing to the file temp.txt try (BufferedWriter writer = Files.newBufferedWriter(FILE_PATH, StandardCharsets.UTF_8, Files.exists(FILE_PATH) ? StandardOpenOption.APPEND : StandardOpenOption.CREATE)) { writer.write(text); } catch (IOException e) { e.printStackTrace(); } response.getWriter().append("Received: ").append(text + "\n"); //response.getWriter().append(FILE_PATH.toString()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }