How do you create a website in Java? In the first step, the deliverable will only display one HTML page. This article aims to operate a web server and display it on a browser. If it works, I think it's a good idea to make corrections and remember it.
In August 2020, we confirmed the operation with the following requirements.
The sample code used here can be found on GitHub.
Terminal
$ mkdir -p helloworld; cd $_
Terminal
$ gradle init
From here, you can proceed interactively, so select as follows.
This completes the initialization of the Gradle Project.
Terminal
$ git clone https://github.com/ryoyakawai/java_gradle_springboot_helloworld.git
Terminal
$ cd java_gradle_springboot_helloworld
$ gradle bootRun
If you start it without any errors, you should see the following in Terminal:
Terminal
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.0.RELEASE)
2020-08-18 15:47:55.672 INFO 64412 --- [ main] c.e.helloworld.HelloworldApplication : Starting HelloworldApplication on S1031198.local with PID 64412 (/..../java_gradle_springboot_helloworld/build/classes/java/main started by ryoya.kawai in /..../java_gradle_springboot_helloworld)
2020-08-18 15:47:55.674 INFO 64412 --- [ main] c.e.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default
2020-08-18 15:47:56.180 INFO 64412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-08-18 15:47:56.187 INFO 64412 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-08-18 15:47:56.187 INFO 64412 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2020-08-18 15:47:56.228 INFO 64412 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-08-18 15:47:56.229 INFO 64412 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 527 ms
2020-08-18 15:47:56.323 INFO 64412 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-18 15:47:56.422 INFO 64412 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-18 15:47:56.424 INFO 64412 --- [ main] c.e.helloworld.HelloworldApplication : Started HelloworldApplication in 1.139 seconds (JVM running for 1.368)
<=========----> 75% EXECUTING [10s]
> :bootRun
Check the operation from the browser here.
If you access http: // localhost: 8080
with a browser and the following is displayed, it means that the web server is operating normally.
This is the file structure immediately after cloning in "1.3. Clone sample code". I made some changes after executing "1.2. Initialize Gradle Project" in the part where the explanation is written on the right side of the file and directory in the figure below.
├── build.gradle (Must be modified: Gradle config file)
├── settings.gradle
└── src
├── main
│ ├── java
│ │ └── helloworld (The following must be modified: Program code)
│ │ ├── HelloworldApplication.java
│ │ ├── HelloworldController.java
│ │ ├── HelloworldErrorController.java
│ │ └── HelloworldServletInitializer.java
│ ├── resources
│ │ ├── application.yml (Must be modified)
│ │ ├── static
│ │ │ └── assets (The following is newly created)
│ │ │ └── sample-300x300.jpg
│ │ └── templates
│ │ ├── error.html (Must be modified: HTML)
│ │ └── helloworld.html (Must be modified: HTML)
│ └── webapp
│ └── WEB-INF
│ └── appengine-web.xml
└── test
└── java
└── helloworld
└── HelloworldApplicationTests.java (Must be modified: test file)
--build.gradle
: Write the Build settings. Modifications are required as necessary.
--settings.gradle
: Describe the project name that will be the entry point.
src> main> java> helloworld
The following files--HelloworldApplication.java
: Declares to use the Spring Boot framework.
--HelloworldController.java
: Controller class. It mainly describes what Path is accessed and what is displayed.
--HelloworldErrorController.java
: A class that handles application errors. It needs to be modified accordingly.
--HelloworldServletInitializer.java
: WebApplicationInitializer implementation class required in the environment where the WAR file is deployed and operated. (Although it exists, it is not used in the operation here and is not necessary originally)
src> main> resources
The following filesPlace HTML files etc. in this directory.
--ʻApplication.yml: The YML file that defines the message. --
templates> error.html: HTML to be displayed when an error occurs. --
templates> helloworld.html: HTML to be displayed during normal operation. --
static> assets`: Images should be placed here.
References -Google App Engine (Java 8) + Spring Boot + Gradle with Hello World --Qiita ――We are very helpful. Thank you very much. ―― What do you need after all to make a web application using java? A quick explanation of how it works and what you need for learning --Qiita --Explanation up to the framework (Spring Boot).
Recommended Posts