程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 用Tomcat和Eclipse開發Servlet程序

用Tomcat和Eclipse開發Servlet程序

編輯:關於PHP編程

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]  <?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
  <servlet> 
    <servlet-name>HelloWorld</servlet-name> 
    <servlet-class>com.johnny.test.HelloWorld</servlet-class> 
  </servlet> 
  <servlet-mapping> 
    <servlet-name>HelloWorld</servlet-name> 
    <url-pattern>/helloworld</url-pattern> 
  </servlet-mapping> 
   
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
   <servlet-name>HelloWorld</servlet-name>
   <servlet-class>com.johnny.test.HelloWorld</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>HelloWorld</servlet-name>
   <url-pattern>/helloworld</url-pattern>
  </servlet-mapping>
 
</web-app>
4)、 右鍵點擊HelloWorld.java,選擇rus as :run on server,在浏覽器上會顯示:Hello, world 1112!
5)、 或者右鍵點擊工程,export MyFirstDynamicWebProject.war,然後放到ubuntu的tomcat的webapps目錄下

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved