Going my way

いいなと思ったことをメモしていきます。

Eclipse上でTomcatを利用する方法


Advertisements


■はじめに
ウィンドウ→設定
左側のツリーから「Tomcat」を選択する。

以下の項目を設定する。

Tomcatバージョン:使用するTomcatのバージョン
Tomcatホーム:Tomcatをインストールしたディレクトリ
コンテキスト宣言モード:Tomcatのコンテキストファイルの場所。「\apache-tomcat-6.0.35\conf\Catalina\localhost」の下に、プロジェクト名.xmlという名前のコンテキストファイルが作成される。


Eclipseのメニューから
「ファイル」→「新規」→「その他」→「Java」→「Tomcatプロジェクト」を選択

任意のプロジェクト名(ここではbasic)を入力して「終了」

JSPファイルの作成
プロジェクトアイコンを右クリック→新規→ファイルを選択

「hello.jsp」で作成

<html>
<body>
<h2> hello </h2>

<%= new java.util.Date() %>
</body>
</html>

eclipseのメニューの「Webブラウザーで開く」をクリックして、

http://localhost:8080/basic/hello.jsp

と入力すると、現在の日付が表示される。


eclipseサーブレットの作成
WEB-INF/srcを右クリック。
「新規」→「クラス」を選択

パッケージ名とクラス名を入力する。

以下のソースを入力

package test;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
	      throws ServletException, IOException {
		PrintWriter out = resp.getWriter();
		out.println("Hello Eclipse");
	}
}


web.xmlを登録する必要がある。

WEB-INFを右クリックして、新規ファイルの作成、web.xmlを作成する。

Tomcatをインストールしたディレクトリ/webapps/ROOT/WEB-INF/web.xmlをコピペして使うとミスが少ない。

以下のようにweb.xmlを定義する。
web.xmlを定義しないと、tomcatサーブレットを見つけられないのだ。

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <servlet>
     <servlet-name>HelloServlet</servlet-name>
     <servlet-class>test.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>HelloServlet</servlet-name>
     <url-pattern>/HelloServlet</url-pattern>
  </servlet-mapping>
  

</web-app>


Tomcatを再起動して以下のURLを入力すると、ブラウザにHello Eclipseと表示される。
http://localhost:8080/basic/HelloServlet


【余談】
文字エンコーディングとは、出力する文字のエンコードの種類のこと。

エンコードとは、文字列を2進数のデータに変換すること。
デコードとは、2進数のデータを文字列に変換すること。

iSO-8859-1:欧米で使われている文字コード
Shift_JIS:日本語Windows環境で利用される文字コード
EUC-JP:日本語UNIX環境で標準で利用される文字コード
UTF-8:ユニコードをもとにした文字コードの1つで、国際標準として推奨されている
Windows-31J:MicrosoftShift_JISの拡張


MIMEタイプとは、インターネット上でやりとりされるデータの種類のこと。