Tích hợp Tomcat vào Eclipse

Hướng dẫn cài đặt plugin tomcat cho Eclipse, gồm các mục sau :

  • Cài đặt plugin Tomcat
  • Tạo project Tomcat trong Eclipse

1. Cài đặt plugin Tomcat

Bước  1:

Download plugin Tomcat tại : http://www.eclipsetotale.com/tomcatPlugin.html#A3

Hãy download package support đúng version Eclipse. Ở đây mình download bản lastest .

Bước 2:

Giải nén và copy file .jar và thư mục  ~\eclipse\plugins

Bước 3:

Khởi động lại Eclipse. Sau khi khởi động lại xuất hiện 3 icon như bên dưới là việc cài đặt đã xong.

Bước 4:

Cấu hình tích hợp với môi trường Tomcat.

Chọn  Window / References / Tomcat

Trong màn hình tác vụ này hãy điền thông tin : version Tomcat, đường dẫn tới thư mục chứa Tomcat và thông tin admin quản lý Tomcat như 2 hình bên dưới.

Để thục hiện bước này thì bạn cần có môi trường Tomcat đã được cài đặt.

Có thể tìm hiểu thêm bài viết Cài đặt môi trường Tomcat.

Sau khi cài đặt thông tin xong, chú ý save lại chọn OK nhé.

Start , Stop, Restart Tomcat : Sau khi cấu hình xong bạn chọn icon start Tomcat sẽ thấy login  tại màn hình consol như sau

2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server version: Apache Tomcat/8.5.9
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server built: Dec 5 2016 20:18:12 UTC
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Server number: 8.5.9.0
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Name: Windows 8.1
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: OS Version: 6.3
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Architecture: amd64
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Java Home: C:\Program Files\Java\jre1.8.0_121
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Version: 1.8.0_121-b13
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: JVM Vendor: Oracle Corporation
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_BASE: D:\eclipse4.4\apache-tomcat-8.5.9
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: CATALINA_HOME: D:\eclipse4.4\apache-tomcat-8.5.9
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:54218
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.home=D:\eclipse4.4\apache-tomcat-8.5.9
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Djava.endorsed.dirs=D:\eclipse4.4\apache-tomcat-8.5.9\endorsed
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dcatalina.base=D:\eclipse4.4\apache-tomcat-8.5.9
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Djava.io.tmpdir=D:\eclipse4.4\apache-tomcat-8.5.9\temp
2 05, 2017 12:21:58 午前 org.apache.catalina.startup.VersionLoggerListener log
情報: Command line argument: -Dfile.encoding=MS932
2 05, 2017 12:21:58 午前 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
情報: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_121\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_121/bin/server;C:/Program Files/Java/jre1.8.0_121/bin;C:/Program Files/Java/jre1.8.0_121/lib/amd64;C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Java\jdk1.8.0_101\bin;C:\Program Files\Git\cmd;D:\eclipse\apache-tomcat-8.5.9\bin;D:\eclipse4.4\eclipse4.4;;.
2 05, 2017 12:21:59 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["http-nio-9999"]
2 05, 2017 12:22:00 午前 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
情報: Using a shared selector for servlet write/read
2 05, 2017 12:22:00 午前 org.apache.coyote.AbstractProtocol init
情報: Initializing ProtocolHandler ["ajp-nio-8009"]
2 05, 2017 12:22:00 午前 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
情報: Using a shared selector for servlet write/read
2 05, 2017 12:22:00 午前 org.apache.catalina.startup.Catalina load
情報: Initialization processed in 3556 ms
2 05, 2017 12:22:00 午前 org.apache.catalina.core.StandardService startInternal
情報: サービス Catalina を起動します
2 05, 2017 12:22:00 午前 org.apache.catalina.core.StandardEngine startInternal
情報: Starting Servlet Engine: Apache Tomcat/8.5.9
2 05, 2017 12:22:00 午前 org.apache.catalina.startup.HostConfig deployDescriptor
情報: 設定記述子 D:\eclipse4.4\apache-tomcat-8.5.9\conf\Catalina\localhost\SampleTomcat.xml を配備します
2 05, 2017 12:22:01 午前 org.apache.catalina.startup.HostConfig deployDescriptor
情報: Deployment of configuration descriptor D:\eclipse4.4\apache-tomcat-8.5.9\conf\Catalina\localhost\SampleTomcat.xml has finished in 1,187 ms
2 05, 2017 12:22:01 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\docs を配備します
2 05, 2017 12:22:01 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\docs has finished in 94 ms
2 05, 2017 12:22:01 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\examples を配備します
2 05, 2017 12:22:03 午前 org.apache.catalina.core.ApplicationContext log
情報: ContextListener: contextInitialized()
2 05, 2017 12:22:03 午前 org.apache.catalina.core.ApplicationContext log
情報: SessionListener: contextInitialized()
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\examples has finished in 1,504 ms
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\host-manager を配備します
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\host-manager has finished in 172 ms
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\manager を配備します
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\manager has finished in 109 ms
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\ROOT を配備します
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\ROOT has finished in 94 ms
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Webアプリケーションディレクトリ D:\eclipse4.4\apache-tomcat-8.5.9\webapps\testJSP を配備します
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.HostConfig deployDirectory
情報: Deployment of web application directory D:\eclipse4.4\apache-tomcat-8.5.9\webapps\testJSP has finished in 78 ms
2 05, 2017 12:22:03 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler [http-nio-9999]
2 05, 2017 12:22:03 午前 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler [ajp-nio-8009]
2 05, 2017 12:22:03 午前 org.apache.catalina.startup.Catalina start
 情報: Server startup in 3464 ms

Như vậy là xong. Access vào địa chỉ  http://localhost:9999/  để vào màn hình quản lý Tomcat. Tương tự với việc chọn icon Stop và Reset thì Tomcat sẽ thực hiện các tác vụ tương ứng.

2. Tạo project Tomcat trong Eclipse

Bước 1:

Ở Eclipse chọn  New / Project / Tomcat Project

Điền tên project vào ( ở đây mình đang điền tên là  SampleTomcat)

Sau Tạo project bạn sẽ thấy Eclipse tự tạo ra project SampleTomcat với cấu trúc như hình dưới .

Như vậy là đã tạo xong project Tomcat trên Eclipse

Bước 2:

Tạo file web.xml  và  .Jsp để kiểm tra

Tạo file web.xml trong thư mục  WEB-INF 

<?xml version="1.0" encoding="UTF-8"?>
<!-- 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://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">

<display-name>Sampe Tomcat</display-name>
<description>
Sampe Tomcat
</description>
 </web-app>

Tạo file  index.jsp  đặt thưc mục ngoài của project

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Tomcat</h1>
</body>
 </html>

Bước 3:

Start Tomcat và access  http://localhost:9999/SampleTomcat/  để xem kết quả

Việc tạo file web.xml và file index.jsp có thể tạo luôn bằng Eclipse bằng cách chọn  New / Other / Chọn File cần thiết

5 (100%) 1 vote
Click Here to Leave a Comment Below 0 comments