LoginSignup
0
1

More than 1 year has passed since last update.

Java + Jetty + Maven で簡単に Web アプリケーションを開発する (2022)

Posted at

概要

JavaでWebアプリケーションをちょっと作ってみたいというときに、サーバーのインストールや設定も不要な方法です。高度なWebアプリケーションフレームワークは利用せず、とりあえずJSPでの表示をするだけです。

前提

Maven 実行環境がセットアップされている。
IDEとしてEclipseを利用
Windows 環境

Maven Project の作成

Eclipse の [File] - [New] - [Project] から [Maven Project] を選択します。

image.png

[Create a simple project] を選択します。

image.png

[Group ID] [Artifact Id] を入力します。
[Packaging] を war にします。Webアプリケーションを作成するので。

ソースコード用のフォルダとして以下を作成します。スラッシュ(/)は階層構造です。すでにフォルダが作成されている場合はそのままでOKです。

src/main/java
src/main/resources
src/test/java
src/test/resources

pom.xml の定義

以下のように Plugin, Dependency を設定します。

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>nlp4j</groupId> <!-- ←自分で適当に変更してください -->
	<artifactId>hello-jetty-maven-plugin</artifactId> <!-- ←自分で適当に変更してください -->
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging> <!-- WEBアプリケーション -->
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.9.0</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
			<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>3.3.2</version>
			</plugin>
			<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin/11.0.9 -->
			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>11.0.9</version>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
</project>

JSPの作成

src/main/webapp/index.jsp を作成し、内容を以下のようにします。
ページが動的に生成されたことがわかるように時刻が表示されるようにしています。

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
	<%=new java.util.Date()%>
</body>
</html>

Webアプリケーションのビルドと起動

「mvn jetty:run」を起動します。

> (pom.xmlのディレクトリに移動)
> mvn jetty:run
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< nlp4j:hello-jetty-maven-plugin >-------------------
[INFO] Building hello-jetty-maven-plugin 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] >>> jetty-maven-plugin:11.0.9:run (default-cli) > test-compile @ hello-jetty-maven-plugin >>>
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-jetty-maven-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.9.0:compile (default-compile) @ hello-jetty-maven-plugin ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-jetty-maven-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.9.0:testCompile (default-testCompile) @ hello-jetty-maven-plugin ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< jetty-maven-plugin:11.0.9:run (default-cli) < test-compile @ hello-jetty-maven-plugin <<<
[INFO]
[INFO]
[INFO] --- jetty-maven-plugin:11.0.9:run (default-cli) @ hello-jetty-maven-plugin ---
[INFO] Configuring Jetty for project: hello-jetty-maven-plugin
[INFO] Classes = C:\xxx\hello-jetty-maven-plugin\target\classes
[INFO] Context path = /
[INFO] Tmp directory = C:\xxx\hello-jetty-maven-plugin\target\tmp
[INFO] web.xml file = null
[INFO] Webapp directory = C:\xxx\hello-jetty-maven-plugin\src\main\webapp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.13+8
[INFO] Session workerName=node0
[INFO] Started o.e.j.m.p.MavenWebAppContext@23313922{/,[file:///C:/xxx/hello-jetty-maven-plugin/src/main/webapp/],AVAILABLE}{file:///C:/xxx/hello-jetty-maven-plugin/src/main/webapp/}
[INFO] Started ServerConnector@c3b45062{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[INFO] Started Server@3658ce21{STARTING}[11.0.9,sto=0] @9363ms
[INFO] Automatic redeployment disabled, see 'mvn jetty:help' for more redeployment options

以上で起動完了です。

Webアプリへのアクセス

http://localhost:8080/index.jsp をブラウザで開いてみます。

以下のような表示になればOKです。

image.png

Webアプリの停止

コマンドプロンプトで Ctrl + C を入力します。

以上。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1