>백엔드 개발 >PHP 튜토리얼 >用Tomcat和Eclipse开发Servlet程序_PHP教程

用Tomcat和Eclipse开发Servlet程序_PHP教程

WBOY
WBOY원래의
2016-07-14 10:11:551042검색

1、 安装eclipse
1)、 在官网上直接下载Eclipse IDE for Java EE Developers,解压即可;
2、 eclipse安装tomcat插件:
1)、 在http://www.eclipsetotale.com/tomcatPlugin.html上下载tomcatPluginV33.zip
2)、 解压到eclipse目录下的plugins目录中
3)、 重新启动eclipse
4)、 如果你在Window  ->  Prefences中找不到Server选择的话,说明你的eclipse版本下错了,要下载Eclipse IDE for Java EE Developers
5)、 在Server一栏中选择Runtime Environment,在右边选择Add,选择好安装路径后download。下载完成后点击finished,看到选择版本的tomcatServer已经建立
6)、 在Window  ->  Prefences  ->  Tomcat配置tomcat。Tomcat home 选择刚才下载的目录
7)、 点击start tomcat按钮,可以发现大量打印信息,在浏览器中输入http://localhost:8080,出现tomcat的网页,证明已经安装好了。
3 新建Servlet测试程序
1)、 选择菜单File->New->Project... ,在新建项目向导中选择 Web->Dynamic Web Project,project name为MyFirstDynamicWebProject,一路next,最后选择生成web.xml;
2)、 new 一个class,package com.johnny.test,name为helloworld,super class 为HttpServlet;

3)、 code:


[java]  package com.johnny.test; 
 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{ 
       /* (non-Java-doc)
         * @see javax.servlet.http.HttpServlet#HttpServlet()
         */ 
        public HelloWorld() { 
            super(); 
        }        
         
        /* (non-Java-doc)
         * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, 
             HttpServletResponse response)
         */ 
        protected void doGet(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException { 
            response.getWriter().write("Hello, world 1112!"); 
        }    
         
        /* (non-Java-doc)
         * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, 
             HttpServletResponse response)
         */ 
        protected void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws ServletException, IOException { 
            // TODO Auto-generated method stub  
        }  

package com.johnny.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet implements javax.servlet.Servlet{
    /* (non-Java-doc)
   * @see javax.servlet.http.HttpServlet#HttpServlet()
   */
  public HelloWorld() {
   super();
  }    
  
  /* (non-Java-doc)
   * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
       HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
   response.getWriter().write("Hello, world 1112!");
  }   
  
  /* (non-Java-doc)
   * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
       HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
   // TODO Auto-generated method stub
  }
}
web.xml


[html]   
 
   
    HelloWorld 
    com.johnny.test.HelloWorld 
 
 
   
    HelloWorld 
    /helloworld 
 
 
   
 



 
   HelloWorld
   com.johnny.test.HelloWorld
 

 
   HelloWorld
   /helloworld
 

 

4)、 右键点击HelloWorld.java,选择rus as :run on server,在浏览器上会显示:Hello, world 1112!
5)、 或者右键点击工程,export MyFirstDynamicWebProject.war,然后放到ubuntu的tomcat的webapps目录下

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477266.htmlTechArticle1、 安装eclipse 1)、 在官网上直接下载Eclipse IDE for Java EE Developers,解压即可; 2、 eclipse安装tomcat插件: 1)、 在http://www.eclipsetotale.com/tomcat...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.