本文前提是你安装好了JDK。
1. 下载eclipse3.6,下载Eclipse IDE for Java Developers或者
下载地址:http://www.eclipse.org/downloads/
2. 下载tomcat7
下载地址:http://tomcat.apache.org/
3. 下载tomcatPlugin33
http://www.eclipsetotale.com/tomcatPlugin.html
4. 解压
解压eclipse,tomcat7到自己定义的目录,这两个不需要安装
解压 tomcatPlugin33到eclipse安装目录下的dropins目录下,如:D:/eclipse3.6.2/dropins
5. 配置
添加几个环境变量:
CATALINE_HOME:指向tomcat主目录
CATALINE_BASE: 指向tomcat主目录
TOMCAT_HOME: 指向tomcat主目录
添加系统classpath: %CATALINE_BASE%/lib/servlet-api.jar;
6. 启动eclipse
7. 配置tomcatPlugin
Window -> Preferences -> Tomcat,Tomcat version 选 Version 7.x,Tomcat home 选择你解压后的
tomcat的根目录,其它设置默认。
8.新建测试项目
File -> New -> Java -> Tomcat Project
新建一个 HelloWorld的java文件
内容
package com.sin;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloWorld implements Servlet {
@Override
public void destroy() {
System.out.println(“destroy,hoho!”);
}
@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println(“init it,ok”);
}
@Override
public void service(ServletRequest reg, ServletResponse res)
throws ServletException, IOException {
// TODO: Add your code here
System.out.println(“service it”);
//从res中得到PrintWriter
PrintWriter pw = res.getWriter();
pw.println(“hello,world,My first Eclipse Servlet!”);
}
}
如果WEB-INF下面没有web.xml,新建一个web.xml,内容:
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<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_3_0.xsd”
version=”3.0″
metadata-complete=”true”>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<!–给你的servlet取名–>
<servlet-name>hello</servlet-name>
<!–指明servlet的路径(包名+类名)–>
<servlet-class>com.sin.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<!–这是在浏览器中输入的访问该servlet的url,任意的–>
<url-pattern>/sp</url-pattern>
</servlet-mapping>
</web-app>
测试:http://localhost:8080/myweb/sp
输出:hello,world,My first Eclipse Servlet!
恭喜你!