eclipseでweb開発をする

helpから行きます。

これでいいのかな?

入りました!!

servletの作成

run on servlet
tomcatが起動する。localhost:8080は404

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello, World!</title>
</head>
<body>
    <%= new java.util.Date() %>
</body>
</html>

何故だ?ようわからん。。

Eclipseを使おう

txtファイルのエンコーディングを設定します。

Caluclation.java

package myPackage.calc;

public class Calculation {
protected int value = 0;
protected int result = 0;

/*
* 計算に使用する値を取得します。
* @return
*/
public int getValue() {
return value;
}
/*
* 計算に使用する値を設定します。
* @param value計算に使用する値
*/

public void setValue(int value) {
this.value = value;
}
/**
* 計算結果をコンソールに出力します。
*/

public void output(){
System.out.println(this.result);
}

}
[/java]

package myPackage.calc;

public class Square extends Calculation {
	
	/*
	 * 値を二乗します。
	 */
	public void calculate(){
		this.result = this.value * this.value;
		this.output();
	}

}
package myPackage.main;

import myPackage.calc.Square;

public class Program {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Square square = new Square();
		
		square.setValue(Integer.parseInt(args[0]));
		square.calculate();

	}

}

Android studioにやや似ているから助かってるが、これどーしろっていうんだろう。。

strutsライブラリ

HTML:タグなどからなるフォーム部品。ほぼHTMLのフォームタグに対応するが、Modelで生成された値のデフォルト表示やエラー時のフィードバックなどリッチな機能を提供する
Logic:条件分岐や繰り返し、値の比較など、プログラミング言語が提供する基本的な制御ロジックをタグ化する
Beans:Modelから引き継がれたJavaBeans(データオブジェクト)にアクセスするための手段を提供する。ヘッダ値やクッキーなど不可視のリクエストデータについても、Beansタグライブラリを介してアクセスすることができる。また、国際化対応メッセージを制御する
Nested:タグで囲まれた内部の属性(変数)名を略記可能とします。例えば、「xxxx.yyyy.zzzz」のような変数を「zzzz」と記述することが可能になる
Tiles:テンプレート機能を提供する。Tilesを利用することで、ヘッダ、メニュー、コンテンツなどの部分から構成される画面を分割管理することが可能になり、デザインの統一を図りやすくなる

アクションサーブレット:Strutsにおけるコアエンジンです。アクションサーブレットは、ユーザーが別に用意した設定ファイルを手掛かりにして、入出力データの振り分けや画面遷移の制御を一手に引き受けます。

アクションフォームBeans:ユーザーからの入力データを格納し、ビジネスロジックに引き渡すための役割を果たします。入力データの検証処理やデフォルト値のセットなど、これまでアプリケーション本流のロジックに混在しがちであった部分を一元的に管理することで、ビジネスロジックをスリム化することができます。

Validator:「Validator」もまたStruts 1.1から導入された機能です。クライアントからの入力データを設定ファイルで定義された内容(検証対象、チェック内容、エラーメッセージ)に基づいて検証します。

strutsに必要なソフトウェア
J2SE(Java2 Software Development Kit, Standard Edition):Javaアプリケーション開発の基本的なツール、および実行環境を提供します。Java仮想マシンをはじめ、コンパイラ(javac)、ドキュメント生成エンジン(javadoc)、エンコーディングユーティリティ(native2ascii)などを含みます。Strutsもまたサーバサイドで動作するJavaアプリケーションですので、まずはこのJ2SEをサーバ上にインストールしておく必要があります。

サーブレットコンテナ
Strutsは「サーブレット」技術をベースにおくアプリケーション・フレームワークです。実行に際しては、サーブレットを動作させるための実行エンジン(コンテナ)を必要とします。一般的には、サーブレットコンテナはユーザーからのリクエスト処理を制御するWebサーバと連携して動作しますが、Tomcatには簡易なWebサーバとしての機能も実装されています。

データベースサーバ:
MySQL

サーバー: J2SE, Tomcat, Strutus, MySQL

struts-config.xml

<?xml version="1.0" encoding="Shift_JIS" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
  <data-sources></data-sources>
  <form-beans></form-beans>
  <global-exceptions></global-exceptions>
  <global-forwards></global-forwards>
  <action-mappings></action-mappings>
</struts-config>

warファイルとは

Java 製のWebアプリで利用されるクラスファイル(servlet)、設定ファイル、JSPやHTMLファイル、JAR形式のライブラリなどがまとめられているアーカイブ。 また、web.xmlが含まれ、TomcatなどのアプリケーションサーバにWARファイルを配布すると、これを元にデプロイされる。

warファイルはzip形式で圧縮されたアーカイブにすぎません。しかし、ほかの形式とは異なり、warファイルを規定のアプリケーションフォルダ(Tomcatなら、%CATALINA_HOME%\webapps)にコピーしておくと、コンテナの(再)起動時に自動的に展開され、Webブラウザからアクセス可能になる

jarコマンドにcvfオプションを付加して、そのパス名を引数として渡すだけで作成できる。

なるほど、動くことは分かったので、strutsがどういう仕組みで、どういう風に書いていけばいいのかですな。
課題としては
– ファイル構成がわからない
– jar, war, jsp, javaなど使用するファイルが、phpやpython、rbなど他のスクリプト言語に較べて多い


「Maven」とは、要するにJavaプログラムをビルドするためのツールです。
C言語を触ったことがある人は、makeのようなものと言えばイメージがしやすいかと思います。
似たようなツールに、AntやGradleなどありますが、使い方とかが違うだけで、本質的には同じです。
コンパイルの前後に依存するライブラリを自動で揃えたり、XMLさえ書けば、いろんなことがビルドと一緒にできるツール

JSPとは、JavaServer Pages の略で、Javaによるサーバサイド・スクリプティングを実現するためのテクノロジです。 … JavaScriptは、Webサーバから転送されるHTMLファイルの中に記述されており、その実行は、クライアントであるWebブラウザ上で行われる。
Webサーバ(ホームページのファイルを置くサーバ)上でお仕事をするJavaのプログラムで、HTMLファイルとJavaのプログラムが合体したもの

jspの例

<html>
<head>
	<title>title</title>
</head>

<body>
<%
	String word1 = "hello world!";
	out.println(world1);
%>
</body>
</html>

View JSP/HTML
Control サーブレット
Model JavaBeans

<table border="1">
	<tr><th>タイトル</th><th>著者</th><th>出版社</th><th>価格</th></tr>
	<logic:iterator id="data" name="aryData" scope="request">
	<tr>
		<td><bean:write name="data" property="title"></td>
		<td><bean:write name="data" property="author"></td>
		<td><bean:write name="data" property="publish"></td>
		<td><bean:write name="data" property="price"></td>
	</tr>
</logic:iterator>
</table>

vagrantでstrutsを動してみよう♪

まずlibの権限変更
[vagrant@localhost apache-tomcat-8.5.33]$ sudo chmod 755 lib

strutsのjarファイルをtomcatのlibに移動する。
[vagrant@localhost struts-1.3.10]$ sudo mv lib/*.jar /opt/tomcat/apache-tomcat-8.5.33/lib

これでOKらしい。

続いて、appsのwarファイルをtomcatのwebappsに移動
mv apps/struts-examples-1.3.10.war /opt/tomcat/apache-tomcat-8.5.33/webapps

[vagrant@localhost webapps]$ ls
ROOT examples manager struts-examples-1.3.10.war
docs host-manager struts-examples-1.3.10

tomcat再起動
[vagrant@localhost webapps]$ sudo /opt/tomcat/apache-tomcat-8.5.33/bin/shutdown.sh
[vagrant@localhost struts-1.3.10]$ sudo /opt/tomcat/apache-tomcat-8.5.33/bin/startup.sh
Using CATALINA_BASE: /opt/tomcat/apache-tomcat-8.5.33
Using CATALINA_HOME: /opt/tomcat/apache-tomcat-8.5.33
Using CATALINA_TMPDIR: /opt/tomcat/apache-tomcat-8.5.33/temp
Using JRE_HOME: /usr
Using CLASSPATH: /opt/tomcat/apache-tomcat-8.5.33/bin/bootstrap.jar:/opt/tomcat/apache-tomcat-8.5.33/bin/tomcat-juli.jar
Tomcat started.

以下のURLを叩く
http://192.168.33.10:8080/struts-examples-1.3.10/welcome.do

うおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお
なにこれ?????????????????????

動いたのは分かったが、framework触ってる気が全くしないぞ。。

java Struts

公式よりDLします。

[vagrant@localhost struts]$ wget http://ftp.kddilabs.jp/infosystems/apache/struts/1.3.10/struts-1.3.10-all.zip
–2018-09-17 09:15:23– http://ftp.kddilabs.jp/infosystems/apache/struts/1.3.10/struts-1.3.10-all.zip
ftp.kddilabs.jp をDNSに問いあわせています… 192.26.91.193, 2001:200:601:10:206:5bff:fef0:466c
ftp.kddilabs.jp|192.26.91.193|:80 に接続しています… 接続しました。
HTTP による接続要求を送信しました、応答を待っています… 200 OK
長さ: 46541251 (44M) [application/zip]
`struts-1.3.10-all.zip’ に保存中

100%[======================================>] 46,541,251 621K/s 時間 77s

2018-09-17 09:16:43 (591 KB/s) – `struts-1.3.10-all.zip’ へ保存完了 [46541251/46541251]

[vagrant@localhost struts]$ ls
struts-1.3.10-all.zip

zipを解凍する。
$ unzip struts-1.3.10-all.zip

[vagrant@localhost struts]$ ls
struts-1.3.10 struts-1.3.10-all.zip

strutus

tomcat

tomcatにデプロイするのか?

Chef Workstation

Chef Workstation
– chef
– knife-solo
– knife configure

[vagrant@localhost chef]$ gem list

*** LOCAL GEMS ***

actioncable (5.0.0.1)
actionmailer (5.0.0.1)
actionpack (5.0.0.1)
actionview (5.0.0.1)
activejob (5.0.0.1)
activemodel (5.0.0.1)
activerecord (5.0.0.1)
activesupport (5.0.0.1)
addressable (2.4.0)
airbrussh (1.3.0)
arel (7.1.4)
backports (3.6.8)
bigdecimal (default: 1.2.8)
builder (3.2.2)
bundler (1.13.6)
byebug (9.0.6)
capistrano (3.11.0)
chef (12.16.42)
chef-config (12.16.42)
chef-zero (5.1.0)
chunky_png (1.3.8)
coffee-rails (4.2.1)
coffee-script (2.4.1)
coffee-script-source (1.10.0)
colorator (1.1.0)
compass (1.0.3)
compass-core (1.0.3)
compass-import-once (1.0.5)
concurrent-ruby (1.0.2)
debug_inspector (0.0.2)
did_you_mean (1.0.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.7.0)
ffi (1.9.14)
ffi-yajl (2.3.0)
forwardable-extended (2.6.0)
fuzzyurl (0.9.0)
globalid (0.3.7)
haml (4.0.7)
hashie (3.4.6)
highline (1.7.8)
httparty (0.13.7)
i18n (0.7.0)
iniparse (1.4.2)
io-console (default: 0.4.5)
ipaddress (0.8.3)
jbuilder (2.6.0)
jekyll (3.3.0)
jekyll-feed (0.8.0)
jekyll-sass-converter (1.4.0)
jekyll-watch (1.5.0)
jquery-rails (4.2.1)
json (default: 1.8.3)
knife-solo (0.6.0)
kramdown (1.12.0)
libv8 (3.16.14.15 x86_64-linux)
libyajl2 (1.2.0)
liquid (3.0.6)
listen (3.0.8)
loofah (2.0.3)
lunchy (0.10.4)
mail (2.6.4)
mercenary (0.3.6)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minima (2.0.0)
minitest (5.9.1, 5.8.3)
mixlib-archive (0.2.0)
mixlib-authentication (1.4.1)
mixlib-cli (1.7.0)
mixlib-config (2.2.4)
mixlib-log (1.7.1)
mixlib-shellout (2.2.7)
multi_json (1.12.1)
multi_xml (0.5.5)
mustache (1.0.3)
net-scp (1.2.1)
net-sftp (2.1.2)
net-ssh (3.2.0)
net-ssh-gateway (1.2.0)
net-ssh-multi (1.2.1)
net-telnet (0.1.1)
nio4r (1.2.1)
nokogiri (1.6.8.1)
ohai (8.21.0)
pathutil (0.14.0)
pg (0.19.0)
plist (3.2.0)
power_assert (0.2.6)
proxifier (1.0.3)
psych (default: 2.0.17)
puma (3.6.0)
rack (2.0.1, 1.6.5, 1.6.4)
rack-protection (1.5.3)
rack-test (0.6.3)
rails (5.0.0.1)
rails-dom-testing (2.0.1)
rails-html-sanitizer (1.0.3)
railties (5.0.0.1)
rake (11.3.0, 10.4.2)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
rdoc (default: 4.2.1)
ref (2.0.0)
rouge (1.11.1)
rspec (3.5.0)
rspec-core (3.5.4)
rspec-expectations (3.5.0)
rspec-its (1.2.0)
rspec-mocks (3.5.0)
rspec-support (3.5.0)
rspec_junit_formatter (0.2.3)
rubygems-update (2.6.8)
safe_yaml (1.0.4)
sass (3.4.22)
sass-rails (5.0.6)
serverspec (2.37.2)
sfl (2.3)
sinatra (1.4.7)
sinatra-contrib (1.4.7)
specinfra (2.66.0)
spring (2.0.0)
spring-watcher-listen (2.0.1)
sprockets (3.7.0)
sprockets-rails (3.2.0)
sshkit (1.17.0)
syslog-logger (1.6.8)
systemu (2.6.5)
test-unit (3.1.5)
therubyracer (0.12.2)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5, 2.0.2)
turbolinks (5.0.1)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
uglifier (3.0.3)
uuidtools (2.1.5)
web-console (3.4.0)
websocket-driver (0.6.4)
websocket-extensions (0.1.2)
wmi-lite (1.0.0)

gem listでknife-soloとchefが入っています。

Chef

概要
– インフラをコードで記述するためのツール
– https://www.chef.io/

Ship software faster, safer and better
Chef is the leader in Continuous Automation software, an innovator in application automation and one of the founders of the DevOps movement. Chef works with more than a thousand of the most innovative companies around the world to deliver their vision of digital transformation, providing the practices and platform to deliver software at speed.

Surviving and thriving with digital transformation
Continuous change is inevitable, and so continuous automation is necessary. The digital leaders of the near-future, both individual and corporate, will be those who are able to provide automation for effortless infrastructure, compliance at velocity, and delivery of any app, anywhere.

Unix, Ruby

chef 概念 / y用語

Chef server: Chefサーバーでは送られてきたcookbookを元に、複数あるnodeサーバーに対して一気に指示を送り、cookbook通りの構成が瞬時に整う
Node
Workstation:「インフラの構成をこうしたい」というコードが書かれた「cookbook」を用意

Hash set

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		// HashSet
		HashSet<Integer> sales = new HashSet<>();

		sales.add(10);
		sales.add(20);
		sales.add(30);
		sales.add(10);

		System.out.println(sales.size());

		for(Integer sale: sales){
			System.out.println(sale);
		}

		sales.remove(30);

		for(Integer sale: sales){
			System.out.println(sale);
		}
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
3
20
10
30
20
10

import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		// HashMap<String, Integer> sales = new HashMap<>();
		Map<String, Integer> sales = new HashMap<>();

		sales.put("tom", 10);
		sales.put("bob", 20);
		sales.put("steve", 30);

		System.out.println(sales.get("tom"));
		System.out.println(sales.size());

		for (Map.Entry<String, Integer> sale: sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}

		sales.put("tom", 100);
		sales.remove("steve");

		for (Map.Entry<String, Integer> sale : sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}
	}
}
import java.util.*;

public class MyApp {

	public static void main(String[] args){
		
		List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9));
		sales
			.stream()
			.filter(e -> e % 3 == 0)
			.map(e -> "(" + e + ")")
			.forEach(System.out::println);

		System.out.println();
	}
}
import java.time.*;
import java.time.format.DateTimeFormatter;

public class MyApp {

	public static void main(String[] args){
		LocalDateTime d = LocalDateTime.now();
		System.out.println(d.getYear());
		System.out.println(d.getMonth());
		System.out.println(d.getMonth().getValue());
		System.out.println(d.plusMonths(2).minusDays(3));
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy!MM!dd!");
		System.out.println(d.format(dtf));
	}
}

[vagrant@localhost java]$ javac MyApp.java
[vagrant@localhost java]$ java MyApp
2018
SEPTEMBER
9
2018-11-13T23:12:54.812
2018!09!16!

スレッド

class MyRunnable implements Runnable{
@Override
public void run(){
for (int i = 0; i < 500; i++){ System.out.print('*'); } } } public class MyApp { public static void main(String[] args){ MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); for (int i = 0; i < 500; i++){ System.out.print('.'); } } } [/java] [vagrant@localhost java]$ java MyApp ..............................................................................................................................................******************************************************************************...................................................................................................................................................**********************************************************************************************************************************************************************************************************************************************...................................................................................................................................................................................................................**************************************************************************************************************************************************************************************** String class [java] public class MyApp { public static void main(String[] args){ String s = "abcdef"; System.out.println(s.length()); System.out.println(s.substring(2, 5)); System.out.println(s.replaceAll("ab", "AB")); String s1 = "ab"; String s2 = "ab"; if (s1.equals(s2)){ System.out.println("same!"); } String ss1 = new String("ab"); String ss2 = new String("ab"); if(ss1 == ss2){ System.out.println("same!same!same!"); } } } [/java] [java] public class MyApp { public static void main(String[] args){ int score = 50; double height = 165.8; String name = "yokoi"; System.out.printf("name: %s, score: %d, height: %f\n", name, score, height); System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height); } } [/java] [java] public class MyApp { public static void main(String[] args){ double d = 53.234; System.out.println(Math.ceil(d)); System.out.println(Math.floor(d)); System.out.println(Math.round(d)); System.out.println(Math.PI); } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 54.0 53.0 53 3.141592653589793 Random処理 [java] Random r = new Random(); System.out.println(r.nextDouble()); System.out.println(r.nextInt(100)); System.out.println(r.nextBoolean()); [/java] [java] import java.util.*; public class MyApp { public static void main(String[] args){ // ArrayList sales = new ArrayList<>();
List sales = new ArrayList<>();

sales.add(10);
sales.add(20);
sales.add(30);

for(int i = 0; i < sales.size(); i++){ System.out.println(sales.get(i)); } sales.set(0, 100); sales.remove(2); for(Integer sale : sales){ System.out.println(sale); } } } [/java] [vagrant@localhost java]$ javac MyApp.java [vagrant@localhost java]$ java MyApp 10 20 30 100 20