[JAVA] I made a bulletin board using Docker 1

What is this

A memo when implementing Chapter 3 of this book on a Docker container for studying Docker and Web application creation.

file organization

$ tree .
.
├── Dockerfile
└── testbbs
    └── WEB-INF
        ├── classes
        ├── src
        │   ├── Message.java
        │   ├── PostBBS.java
        │   └── ShowBBS.java
        └── web.xml

4 directories, 5 files

Dockerfile

Dockerfile


FROM tomcat:8.5.54-jdk11-adoptopenjdk-hotspot
WORKDIR /usr/local/tomcat/webapps/
RUN mkdir -p ./testbbs
COPY ./testbbs ./testbbs/
RUN javac -classpath $CATALINA_HOME/lib/servlet-api.jar -d ./testbbs/WEB-INF/classes ./testbbs/WEB-INF/src/*.java

Java file

Same as the reference book.

Message.java


import java.util.*;

public class Message {
	public static ArrayList<Message> messageList = new ArrayList<Message>();
	String title;
	String handle;
	String message;
	Date date;

	Message (String title, String handle, String message) {
		this.title = title;
		this.handle = handle;
		this.message = message;
		this.date = new Date();
	}
}

PostBBS.java


import java.io.*;
import javax.servlet.http.*;

public class PostBBS extends HttpServlet {
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException {
		request.setCharacterEncoding("UTF-8");
		Message newMessage = new Message(request.getParameter("title"),
				request.getParameter("handle"),
				request.getParameter("message"));

		Message.messageList.add(0, newMessage);
		response.sendRedirect("/testbbs/ShowBBS");
	}
}

ShowBBS.java


import java.io.*;
import javax.servlet.http.*;

public class ShowBBS extends HttpServlet {
	private String espaceHTML (String src) {
		return src.replace ("&", "&amp;").replace("<", "&lt;")
			.replace (">", "&gt;").replace ("\"", "&quot;")
			.replace ("'", "&#39;");
	}

	@Override
	public void doGet (HttpServletRequest request, HttpServletResponse response) 
			throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Test bulletin board</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Test bulletin board</h1>");
		out.println("<form action='/testbbs/PostBBS' method='post'>");
		out.println("title:<input type='text' name='title' size='60'>");
		out.println("<br />");
		out.println("Handle name:<input type='text' name='handle'>");
		out.println("<br />");
		out.println("<textarea name='message' rows='4' cols='60'></textarea>");
		out.println("<br />");
		out.println("<input type='submit' />");
		out.println("</form>");
		out.println("<hr />");

		for (Message message: Message.messageList) {
			out.println("<p> 『" + espaceHTML(message.title) + "』&nbsp; &nbsp;");
			out.println(espaceHTML(message.handle) + "Mr.&nbsp;&nbsp;");
			out.println(espaceHTML(message.date.toString()) + "</p>");
			out.println("<p>");
			out.println(espaceHTML(message.message).replace("\r\n", "<br />"));
			out.println("</p><hr />");
		}
		
		out.println("</body>");
		out.println("</html>");
	}

}

web.xml file

This is the same as a book.

web.xml


<web-app xmlns="http://xmlns.jcp.org/xml/nx/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
						http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1"
	metadata-complete="true">

	<servlet>
		<servlet-name>ShowBBS</servlet-name>
		<servlet-class>ShowBBS</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>PostBBS</servlet-name>
		<servlet-class>PostBBS</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ShowBBS</servlet-name>
		<url-pattern>/ShowBBS</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>PostBBS</servlet-name>
		<url-pattern>/PostBBS</url-pattern>
	</servlet-mapping>
</web-app>

Build and run

$ docker build -t henacat .
Sending build context to Docker daemon  11.78kB

Step 1/6 : FROM tomcat:8.5.54-jdk11-adoptopenjdk-hotspot
 ---> 66317f378ae0
Step 2/6 : RUN apt-get update && apt-get install -y wget
 ---> Using cache
 ---> 871dd39a71cc
Step 3/6 : WORKDIR /usr/local/tomcat/webapps/
 ---> Using cache
 ---> 19d29e246ff6
Step 4/6 : RUN mkdir -p ./testbbs
 ---> Using cache
 ---> c2765cdc59e3
Step 5/6 : COPY ./testbbs ./testbbs/
 ---> Using cache
 ---> dbd09272e03b
Step 6/6 : RUN javac -classpath $CATALINA_HOME/lib/servlet-api.jar -d ./testbbs/WEB-INF/classes ./testbbs/WEB-INF/src/*.java
 ---> Using cache
 ---> 41e02fb5b101
Successfully built 41e02fb5b101
Successfully tagged henacat:latest

$ docker run -d -p 8080:8080 -it henacat
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED              STATUS              PORTS                    NAMES
249799090cb1   henacat   "catalina.sh run"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   elegant_ishizaka

When you access http: // localhost: 8080/testbbs/ShowBBS, the bulletin board is displayed properly.

Screen Shot 2021-01-11 at 2.42.20.png

I was addicted to

The first Docker image I used was tomcat: 10.0.0-jdk11-adoptopenjdk-hotspot, but it didn't compile. Apparently I can't find the package for the Servlet. I wondered if I made a mistake in the classpath and tried various changes, but the compile error did not go away, and when I went to the document thinking that it was a problem with tomcat, it seems that the package name changed from tomcat-10. In tomcat-8, it was javax.servlet.http, but in tomcat-10, it is jakarta.servlet.http, so the package cannot be found and an error is thrown. When. That's why I solved it safely by using the same tomcat-8 Docker image as the book.

References

Kazuya Maebashi "Introduction to Web Application Development from the Basics of Learning While Creating a Web Server" Gijutsu-Hyoronsha (2016)

Recommended Posts

I made a bulletin board using Docker 1
[Rails] I made a draft function using enum
I made a Docker image of SDAPS for Japanese
I made a chat app.
I made a development environment with rails6 + docker + postgreSQL + Materialize.
I made a simple MVC sample system using Spring Boot
I made a Dockerfile to start Glassfish 5 using Oracle Java
Ruby: I made a FizzBuzz program!
I made a shopify app @java
I made a GUI with Swing
I made a simple recommendation function.
I made a matching app (Android app)
I made a package.xml generation tool.
[Android] I made a pedometer app.
I made a command line interface with WinMerge Plugin using JD-Core
I tried to make a group function (bulletin board) with Rails
Create a web environment quickly using Docker
[Ruby] I made a simple Ping client
I tried using Scalar DL with Docker
I made a risky die with Ruby
I made a plugin for IntelliJ IDEA
I made a rock-paper-scissors app with kotlin
I made a calculator app on Android
I made a new Java deployment tool
I made a rock-paper-scissors app with android
I made a Diff tool for Java files
I made a primality test program in Java
Create a Privoxy + Tor environment instantly using Docker
Create a simple bulletin board with Java + MySQL
I made StringUtils.isBlank
I made a mosaic art with Pokemon images
Building a CICD pipeline using Docker (personal memorandum)
I made a gender selection column with enum
Try to create a bulletin board in Java
Build a Kotlin app using OpenJDK's Docker container
I made blackjack with Ruby (I tried using minitest)
I made a rock-paper-scissors game in Java (CLI)
I made a viewer app that displays a PDF
I made a Ruby extension library in C
I tried running Ansible on a Docker container
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
I made a LINE bot with Rails + heroku
Delegate pattern between views. I also made a sample page transition using NavigationLink.
I tried to implement a server using Netty
I made a portfolio with Ruby On Rails
I made a lock pattern using the volume key with the Android app. Fragment edition
I tried using a database connection in Android development
I made a simple calculation problem game in Java
A simple CRUD app made with Nuxt / Laravel (Docker)
I made a check tool for the release module
I made a method to ask for Premium Friday
[Ruby] I made a crawler with anemone and nokogiri.
I made a drawing chat "8bit paint chat" on WebAssembly
I made a Restful server and client in Spring.
I made a library that works like a Safari tab !!
I tried using Docker Desktop for Windows on Windows 10 Home
I made a library for displaying tutorials on Android.
I made a Wrapper that calls KNP from Java
Try to build a Java development environment using Docker
I tried scraping a stock chart using Java (Jsoup)
[2021] Build a Docker + Vagrant environment for using React / TypeScript