首先下载tomcat插件: ,下载最新的3.3版本;
由于我的eclipse是通过yum自动安装的,因此eclipse在默认的路径下/usr/lib/eclipse/,将tomcat插件解压后,把com.sysdeo.eclipse.tomcat_3.3.0文件夹放在eclipse默认路径下的/plugins路径下;
然后重启eclipse,即可看到有tomcat图标了。
配置tomcat的文章,参考:,虽然是windows的,但没有本质差别;
创建servlet,参考:;
使用命令行,也可以编译servlet.
首先,将编译用的api文件拷贝到java文件同一目录下,我用的是/usr/share/java/servlet_2_4_api.jar;
然后使用javac -classpath servlet_2_4_api.jar HelloWorldExample11.java进行编译;即可生成class文件;
HelloWorldExample11.java代码如下:
import java.io.*;
import java.util.*;import javax.servlet.*;import javax.servlet.http.*;/**
* The simplest possible servlet. * * @author James Duncan Davidson */public class HelloWorldExample11 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings",request.getLocale()); response.setContentType("text/html"); PrintWriter out = response.getWriter();out.println("<html>");
out.println("<head>");String title = rb.getString("helloworld.title");
out.println("<title>" + title + "</title>");
out.println("</head>"); out.println("<body bgcolor=\"white\">");// note that all links are created to be relative. this
// ensures that we can move the web application that this // servlet belongs to to a different place in the url // tree and not have any harmful side effects.// XXX
// making these absolute till we work out the // addition of a PathInfo issueout.println("<a href=\"../helloworld.html\">");
out.println("<img src=\"../images/code.gif\" height=24 " + "width=24 align=right border=0 alt=\"view code\"></a>"); out.println("<a href=\"../index.html\">"); out.println("<img src=\"../images/return.gif\" height=24 " + "width=24 align=right border=0 alt=\"return\"></a>"); out.println("<h1>" + title + "</h1>"); out.println("</body>"); out.println("</html>"); }}