[JAVA] I made a web server with Raspberry Pi to watch anime

Motivated

There are a lot of animations on the HDD, but it is troublesome to connect the HDD to a computer and watch it, so let's make it visible from the browser.

environment

Since the place where the animation is saved is not a NAS but just an HDD, it is assumed that the HDD is a NAS using Raspberry Pi. The procedure for turning an HDD into a NAS is not shown here.

HDD(NAS)Location


/mnt/hdd/HD-V3/Anime/

Introduction of tomcat

The procedure on this site is super easy to understand, so follow here. Raspberry Pi Tomcat-8 environment settings

If you do the basics as written, it will work smoothly. However, if you do it with wget in exactly the same way, it will not work because apache-tomcat-8.5.4.tar.gz is no longer available. Since 8.5.31 was up to date, I think you should download apache-tomcat-8.5.31.tar.gz on Windows and send it to Raspberry Pi with scp or something. It seemed like I was using gedit (?) To edit the script, but it was like that, so I wrote the script in my favorite nano. If you follow the steps in the link above, tomcat will be in / usr / local / tomcat. This time I will proceed with the story with tomcat here.

location of tomcat


/usr/local/tomcat/

Directory structure, hierarchy

Work is done under a folder called workspace. It seems that tomcat specifies the name of the folder to put the class file of the web application. If you create an app with a hierarchy based on it, the hierarchy will be as follows. 2018-05-08 (2).png Originally, this time, it seems that I will create a folder of my own application under tomcat / webapps /, but since the NAS is on a higher layer, I do not want to create a web application under webapps. That's why the workspace is placed on the HDD.

workspace location


/mnt/hdd/HD-V3/Anime/workspace/

Since the workspace is not under webapps, I need to tell tomcat. Write as follows. The name of the file must be the name of the directory. (There seems to be a way to write it in server.xml, but it didn't work.)

/usr/local/tomcat/conf/Catalina/localhost/workspace.xml


<Context path="/workspace" docBase="/mnt/hdd/HD-V3/Anime/workspace" />

Editing web.xml

Write the page structure of the workspace.

/mnt/hdd/HD-V3/Anime/workspace/WEB-INF/web.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>
	<servlet-name>cookie1</servlet-name>
	<servlet-class>cookie1</servlet-class>
</servlet>

<servlet>
	<servlet-name>cookie2</servlet-name>
	<servlet-class>cookie2</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>
		cookie1
	</servlet-name>
	<url-pattern>
		/servlet/cookie1
	</url-pattern>
</servlet-mapping>

<servlet-mapping>
	<servlet-name>
		cookie2
	</servlet-name>
	<url-pattern>
		/servlet/cookie2
	</url-pattern>
</servlet-mapping>

</web-app>

Creating the main program

I originally planned to use cookies, so this name is used, but I haven't used it in the program. cookie1.java Get the file name list in / mnt / hdd / HD-V3 / anime / workspace / anime /, add it to the pull-down menu, select one title, and transition to cookie2.java.

cookie1.java


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

public class cookie1 extends HttpServlet {     
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException, ServletException {
	         response.setContentType("text/html; charset=utf-8");
	         PrintWriter out = response.getWriter();
                 request.setCharacterEncoding("UTF-8"); 
                 out.println("<html><head>"); 
                 out.println("<title>cookie1</title>");
                 out.println("</head><body>");
                 out.println("<h1>Wiki Anime DB</h1><hr>");
		          String root="/mnt/hdd/HD-V3/Anime/workspace/anime/"; 
				  File dir=new File(root);
				  File[] list=dir.listFiles();
				  out.println("Number of titles:"+list.length+"<br>");
      	         out.println("<form action=\"/workspace/servlet/cookie2\" method=\"GET\">");
      	         out.println("name: <input type=\"text\" name=\"name\" size=32>");
       	         out.println("<br>");         
       	         out.println("<input type=\"submit\" value=\"Code generation\">");   
				 out.println("<br>");  
				 out.println("<select name=\"anime\">");
				 for(int i=0;i<list.length;i++){
					 String dst=list[i].toString();
					 dst=dst.replace(root,"");
					 out.println("<option value=\""+dst+"\">"+dst+"</option>");
				 }
				 out.println("<select>");       
       	         out.println("</form>"); 
       	         out.println("</body></html>"); 
        }
} 

cookie2.java Based on the animation title received from cookie1.java, only the files with the extension .mp4 | .MP4 in the hierarchy one level below that folder are output by sandwiching them with video tags.

cookie2.java


import java.io.*; 
import java.net.*;
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.File;
 
public class cookie2 extends HttpServlet {     
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws  IOException, ServletException {
	        response.setContentType("text/html; charset=utf-8");
	        PrintWriter out = response.getWriter();
            request.setCharacterEncoding("UTF-8");
            String aname=request.getParameter("anime");
            String eaname=URLDecoder.decode(aname,"UTF-8");
            String root="../anime/";
            String root_t="/mnt/hdd/HD-V3/Anime/workspace/anime/";
            out.println("<html><head>");
            out.println("<title>cookie2</title>");  
            out.println("</head><body>");      
            out.println("<h1>"+eaname+"</h1><hr>"); 
            out.println("Selected anime folder="+eaname+"<br>");
	        out.println("<form action=\"/workspace/servlet/cookie1\" method=\"GET\">");
	        out.println("<input type=\"submit\" value=\"Return\">");     
            out.println("</form>"); 
            File dir=new File(root_t+eaname);
				  File[] list=dir.listFiles();
                  out.println("File="+list.length+"<br>");
                  for(int i=0;i<list.length;i++){
                      String dst=list[i].toString();
                      dst=dst.replace(root_t+eaname+"/", "");
                      if(dst.indexOf(".mp4")!=-1  || dst.indexOf(".MP4")!=-1){
                        dst=root+eaname+"/"+dst;
                        out.println(dst+"<br>");
                        out.println("<video src=\""+dst+"\" preload=\"none\" controls></video><br>");
                      }
				  }
	        out.println("</body></html>");
	  }
}

compile

You have to compile by adding servlet-api.jar with the classpath option.

/mnt/hdd/HD-V3/Anime/workspace/WEB-INF/classes/


$ javac -classpath "/usr/local/tomcat/lib/servlet-api.jar" cookie1.java
$ javac -classpath "/usr/local/tomcat/lib/servlet-api.jar" cookie2.java

Caution Initially, compilation failed because /usr/local/tomcat/lib/servlet-api.jar does not have permission.

Authorization


$sudo chmod 755 /usr/local/tomcat/lib/servlet-api.jar

I granted the permissions and then compiled it and it worked.

Run

python


$ sudo /etc/init.d/tomcat start

cookie1 screen

2018-05-08.png

cookie2 screen

2018-05-08 (1).png

Task

  1. The number of stories is not in ascending order
  2. There are too many titles and it is hard to find from the pull-down menu

But for the time being, ZOY allows you to watch the animation on the HDD more comfortably from your smartphone.

Recommended Posts

I made a web server with Raspberry Pi to watch anime
I made a resource monitor for Raspberry Pi with a spreadsheet
I made a surveillance camera with my first Raspberry PI.
I made a WEB application with Django
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
[Electronic work] I made a Suica touch sound detector with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I made a ready-to-use syslog server with Play with Docker
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
I talked to Raspberry Pi
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a web server --3. Use MySQL
Create a web surveillance camera with Raspberry Pi and OpenCV
I made a package to filter time series with python
I tried connecting Raspberry Pi and conect + with Web API
I tried to automate [a certain task] using Raspberry Pi
I made you to execute a command from a web browser
I made a fortune with Python.
VPN server construction with Raspberry Pi
I made a daemon with Python
Using a webcam with Raspberry Pi
I made a server with Python socket and ssl and tried to access it from a browser
I made a library to easily read config files with Python
I tried to automate the watering of the planter with Raspberry Pi
I made a poker game server chat-holdem using websocket with python
Build a Tensorflow environment with Raspberry Pi [2020]
I made a character counter with Python
Improved motion sensor made with Raspberry Pi
I made a script to display emoji
Make a wash-drying timer with a Raspberry Pi
I made a Hex map with Python
I tried to make a Web API
I made a life game with Numpy
I made a stamp generator with GAN
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
A memo to simply use the illuminance sensor TSL2561 with Raspberry Pi 2
I tried to communicate with a remote server by Socket communication with Python.
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a web application in Python that converts Markdown to HTML
Note: I want to do home automation with Home Assistant + Raspberry Pi + sensor # 1
[Django] I made a field to enter the date with 4 digit numbers
I tried to discriminate a 6-digit number with a number discrimination application made with python
I made a tool to convert Jupyter py to ipynb with VS Code
I made a bot to post on twitter by web scraping a dynamic site with AWS Lambda (continued)
I made a pet camera that is always connected with WebRTC (Nuxt.js + Python + Firebase + SkyWay + Raspberry Pi)
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
I made a stamp substitute bot with line
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
Python Web Content made with Lolipop cheap server
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
I tried L-Chika with Raspberry Pi 4 (Python edition)