eclipse jsp/servlet 環(huán)境搭建
本文假定你已安裝了 jdk 環(huán)境,如未安裝,可參閱 java 開發(fā)環(huán)境配置 。
我們可以使用 eclipse 來搭建 jsp 開發(fā)環(huán)境,首先我們分別下載一下軟件包:
- eclipse j2ee:http://www.eclipse.org/downloads/
- tomcat:http://tomcat.apache.org/download-70.cgi
tomcat 下載安裝
你可以根據(jù)你的系統(tǒng)下載對應的包(以下以window系統(tǒng)為例):
下載之后,將壓縮包解壓到d盤(你可以自己選擇):
注意目錄名不能有中文和空格。目錄介紹如下:
- bin:二進制執(zhí)行文件。里面最常用的文件是startup.bat,如果是 linux 或 mac 系統(tǒng)啟動文件為 startup.sh。
- conf:配置目錄。里面最核心的文件是server.xml??梢栽诶锩娓亩丝谔柕取DJ端口號是8080,也就是說,此端口號不能被其他應用程序占用。
- lib:庫文件。tomcat運行時需要的jar包所在的目錄
- logs:日志
- temp:臨時產(chǎn)生的文件,即緩存
- webapps:web的應用程序。web應用放置到此目錄下瀏覽器可以直接訪問
- work:編譯以后的class文件。
接著我們可以雙擊 startup.bat 啟動 tomcat,彈出如下界面:

這個時候,本地的服務器就已經(jīng)搭建起來了。如果想關閉服務器,可以直接關閉上面的窗口,或者在里面輸入ctrl+c禁止服務。
接著我們在瀏覽器中輸入 http://localhost:8080/,如果彈出如下界面,表示tomcat安裝成功并且啟動起來了:

我們現(xiàn)在在瀏覽器上測試一下它吧:
首先在d:\apache-tomcat-8.0.14\webapps\root目錄中新建一個jsp文件:

test.jsp 文件代碼如下:
<%@ page contenttype="text/html;charset=utf-8" %>
<%
out.print("碩編程 : http://m.aalaour.cn");
%>
接著在瀏覽器中訪問地址 http://localhost:8080/test.jsp, 輸出結(jié)果如下:

將 tomcat 和 eclipse 相關聯(lián)
eclipse j2ee下載后,解壓即可使用,我們打開java ee ,選擇菜單欄windows-->preferences(mac 系統(tǒng)為 eclipse-->偏好設置),彈出如下界面:

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

在選項中,我們選擇對應的 tomcat 版本,接著點擊 "next",選擇 tomcat 的安裝目錄,并選擇我們安裝的 java 環(huán)境:

點擊 "finish",完成配置。
創(chuàng)建實例
選擇 "file-->new-->dynamic web project",創(chuàng)建 tomcattest 項目:


點開上圖中的紅框部分,彈出如下界面:

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


工程文件結(jié)構(gòu):

上圖中各個目錄解析:
- 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>
程序運行之前,我們先修改一下瀏覽器選項:

接著我們運行該項目:

運行時,彈出如下錯誤:(如果沒有此錯誤,請忽略)

原因是,我們之前點擊了tomcat安裝包中的?startup.bat,這樣一來就手動打開了tomcat服務器,這明顯是多余的,因為程序運行時,eclipse會自動開啟tomcat服務器。所以我們先手動關掉tomcat軟件,再次運行程序,就行了??刂婆_信息如下:

瀏覽器訪問 http://localhost:8080/tomcattest/test.jsp, 即可輸出正常結(jié)果:
servlet 實例創(chuàng)建
我們也可以使用以上環(huán)境創(chuàng)建 servlet 文件,選擇 "file-->new-->servlet":

位于 tomcattest項目的 /tomcattest/src 目錄下創(chuàng)建 "helloservlet" 類,包為 "com.yapf.test":

helloservlet.java 代碼如下所示:
package com.yapf.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 {
// 使用 gbk 設置中文正常顯示
response.setcharacterencoding("gbk");
response.getwriter().write("碩編程:http://m.aalaour.cn");
}
/**
* @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 version="2.5"
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">
<servlet>
<!-- 類名 -->
<servlet-name>helloservlet</servlet-name>
<!-- 所在的包 -->
<servlet-class>com.yapf.test.helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<!-- 訪問的網(wǎng)址 -->
<url-pattern>/tomcattest/helloservlet</url-pattern>
</servlet-mapping>
</web-app>
接著重啟 tomcat,瀏覽器訪問 http://localhost:8080/tomcattest/helloservlet:

參考文章:http://www.cnblogs.com/smyhvae/p/4046862.html


