將 Tomcat 和 Eclipse 相關(guān)聯(lián)
Eclipse J2EE下載后,解壓即可使用,我們打開Java EE ,選擇菜單欄Windows-->preferences(Mac 系統(tǒng)為 Eclipse-->偏好設(shè)置),彈出如下界面:

上圖中,點擊"add"的添加按鈕,彈出如下界面:

在選項中,我們選擇對應(yīng)的 Tomcat 版本,接著點擊 "Next",選擇 Tomcat 的安裝目錄,并選擇我們安裝的 Java 環(huán)境:

點擊 "Finish",完成配置。
創(chuàng)建實例
選擇 "File-->New-->Dynamic Web Project",創(chuàng)建 TomcatTest 項目:



注意如果已默認選擇了我們之前安裝的 Tomcat 和 JDK 則可跳過此步。
然后,單擊finish, 繼續(xù):



上圖中各個目錄解析:
deployment descriptor:部署的描述。
Web App Libraries:自己加的包可以放在里面。
build:放入編譯之后的文件。
WebContent:放進寫入的頁面。
在WebContent文件夾下新建一個test.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>Insert title here</title>
</head>
<body>
</body>
</html>
接著我們修改下test.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>南城的人</title>
</head>
<body>
<%
out.println("Hello World!");
%>
</body>
</html>

![Upload Paste_Image.png failed. Please try again.]
瀏覽器訪問 http://localhost:8080/TomcatTest/test.jsp, 即可輸出正常結(jié)果
Servlet 實例創(chuàng)建
我們也可以使用以上環(huán)境創(chuàng)建 Servlet 文件,選擇 "File-->New-->Servlet":


package com.runoob.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
//@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 使用GBK設(shè)置中文正常顯示
response.setCharacterEncoding("GBK");
response.getWriter().write("南城:http:、、www.zonzzz.com");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
創(chuàng)建 /TomcatTest/WebContent/WEB-INF/web.xml 文件(如果沒有),代碼如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 配置和映射servlet -->
<servlet>
<!-- servlet 注冊的名字 -->
<servlet-name>HelloServlet</servlet-name>
<!-- setvlet 的全類名 -->
<servlet-class>com.runoob.test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- 需要個某一個servlet子節(jié)點的servlet-name子節(jié)點的文本節(jié)點保持一致 -->
<servlet-name>HelloServlet</servlet-name>
<!-- 映射的具體訪問路徑:/代表當前WEB應(yīng)用的根目錄 -->
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

