將 Tomcat 和 Eclipse 相關(guān)聯(lián)
Eclipse J2EE下載后拴孤,解壓即可使用弊决,我們打開Java EE 周伦,選擇菜單欄Windows-->preferences(Mac 系統(tǒng)為 Eclipse-->偏好設(shè)置)宫莱,彈出如下界面:
上圖中只酥,點(diǎn)擊"add"的添加按鈕娃胆,彈出如下界面:
在選項(xiàng)中遍希,我們選擇對(duì)應(yīng)的 Tomcat 版本,接著點(diǎn)擊 "Next"里烦,選擇 Tomcat 的安裝目錄凿蒜,并選擇我們安裝的 Java 環(huán)境:
點(diǎn)擊 "Finish"禁谦,完成配置。
創(chuàng)建實(shí)例
選擇 "File-->New-->Dynamic Web Project"废封,創(chuàng)建 TomcatTest 項(xiàng)目:
注意如果已默認(rèn)選擇了我們之前安裝的 Tomcat 和 JDK 則可跳過(guò)此步州泊。
然后,單擊finish, 繼續(xù):
上圖中各個(gè)目錄解析:
deployment descriptor:部署的描述漂洋。
Web App Libraries:自己加的包可以放在里面遥皂。
build:放入編譯之后的文件。
WebContent:放進(jìn)寫入的頁(yè)面刽漂。
在WebContent文件夾下新建一個(gè)test.jsp文件演训。在下圖中可以看到它的默認(rèn)代碼:
<%@ 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.]
瀏覽器訪問(wèn) http://localhost:8080/TomcatTest/test.jsp, 即可輸出正常結(jié)果
Servlet 實(shí)例創(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 文件(如果沒(méi)有),代碼如下所示:
<?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 注冊(cè)的名字 -->
<servlet-name>HelloServlet</servlet-name>
<!-- setvlet 的全類名 -->
<servlet-class>com.runoob.test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- 需要個(gè)某一個(gè)servlet子節(jié)點(diǎn)的servlet-name子節(jié)點(diǎn)的文本節(jié)點(diǎn)保持一致 -->
<servlet-name>HelloServlet</servlet-name>
<!-- 映射的具體訪問(wèn)路徑:/代表當(dāng)前WEB應(yīng)用的根目錄 -->
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>