java web開發--Servlet學習之HelloWorld 從部署到開發全過程

工程師WWW發表於2015-10-30

今天終於搞出來來了Servlet的HelloWorld,記錄下過程。

下圖是個整體框架圖:

 

開發流程圖:

第一步:配置TOMCAT

      在tomcat的安裝目錄下,找到webapps資料夾,新建資料夾myWebSite(這個名字可以任意,代表自己的工程名字),在myWebSite資料夾下新建WEB-INF(這個名字不能錯),在WEB-INF資料夾下新建兩個資料夾分別是classes、lib資料夾,並新建一個檔案web.xml。這裡可以找到webapps資料夾下的ROOT資料夾,將裡面的WEB-INF資料夾拷到myWebSite就可以了。沒有的資料夾要新建。

第二步:開發Servlet(引入servlet-api.jar)

     這裡使用的工具是JCreator,其實Eclipse也可以的。這一步要做的事情就是在上面的classes資料夾裡新建一個Hello類。先下載並安裝JCreator,新建一個JavaFile,名字Hello,路徑選到classes資料夾下。然後要引入servlet-api.jar這個包。點選JCreator的配置---選項---JDK配置檔案,選中右邊的JDK版本,如上圖所示,點編輯,點新增--新增存檔,然後將TOMCAT安裝目錄下得lib資料夾下得servlet-api.jar選中,確定即可。

編寫程式碼:

package yanguoqi;
import javax.servlet.*;
import java.io.*;

public class Hello{

} 然後點工具---實現介面---找到javax---servlet---servlet,選中即可。

最終的Hello.java原始碼為:

  1. //這是我的第一個servlet,使用實現servlet介面的方式來開發  
  2. package yanguoqi;  
  3. import javax.servlet.*;  
  4. import java.io.*;  
  5. import javax.servlet.Servlet;  
  6. import javax.servlet.ServletConfig;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10. import java.io.IOException;  
  11.   
  12. public class Hello implements Servlet{  
  13.     /** 
  14.      * Method init 
  15.      * @param parm1 
  16.      * 
  17.      @throws ServletException 
  18.      * 
  19.      */  
  20.      //初始化servlet,類似於建構函式  
  21.      //只第一次訪問survlet時被呼叫  
  22.     public void init(ServletConfig parm1) throws ServletException {  
  23.         // TODO: 在這新增你的程式碼  
  24.         System.out.println("init ");  
  25.     }  
  26.   
  27.     /** 
  28.      * Method getServletConfig 
  29.      * @return 
  30.      */  
  31.      //得到servlet配置檔案,不太重要  
  32.     public ServletConfig getServletConfig() {  
  33.         // TODO: 在這新增你的程式碼  
  34.         return null;  
  35.     }  
  36.   
  37.     /** 
  38.      * Method service 
  39.      * @param parm1 
  40.      * @param parm2 
  41.      * 
  42.      @throws ServletException 
  43.      @throws IOException 
  44.      */  
  45.      //用於處理業務邏輯  
  46.     public void service(ServletRequest parm1, ServletResponse res) throws ServletException, IOException {  
  47.         // TODO: 在這新增你的程式碼  
  48.         System.out.println("service it");  
  49.         PrintWriter pw = res.getWriter();     
  50.         pw.println("Hello World!");    
  51.     }  
  52.   
  53.     /** 
  54.      * Method getServletInfo 
  55.      * @return 
  56.      */  
  57.     public String getServletInfo() {  
  58.         // TODO: 在這新增你的程式碼  
  59.         return "";  
  60.     }  
  61.   
  62.     /** 
  63.      * Method destroy 
  64.      */  
  65.     public void destroy() {  
  66.         // TODO: 在這新增你的程式碼  
  67.         System.out.println("destroy!");  
  68.     }  
  69.       
  70. }  

注意這個程式寫好後要編譯下!每次修改後也應該編譯。

第三步:部署web.xml

開啟web.xml,坑爹的是JCreator貌似打不開,一開啟就報錯要求關閉。不得已,用eclipse開啟來編輯。

  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>   
  2. <!-- ISO-8859-1 -->  
  3. <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor   
  4.     license agreements. See the NOTICE file distributed with this work for additional   
  5.     information regarding copyright ownership. The ASF licenses this file to   
  6.     You under the Apache License, Version 2.0 (the "License"); you may not use   
  7.     this file except in compliance with the License. You may obtain a copy of   
  8.     the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required   
  9.     by applicable law or agreed to in writing, software distributed under the   
  10.     License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS   
  11.     OF ANY KIND, either express or implied. See the License for the specific   
  12.     language governing permissions and limitations under the License. -->  
  13.   
  14. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  15.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  16.     version="2.5">  
  17.   
  18.     <display-name>Welcome to Tomcat</display-name>  
  19.     <description>  
  20.      Welcome to Tomcat  
  21.   </description>  
  22.   
  23.     <servlet>  
  24.         <!--給survlet起個名字,可以是任意的 -->  
  25.         <servlet-name>hello</servlet-name>  
  26.         <!--servlet的路徑(包名+類名)-->  
  27.         <servlet-class>yanguoqi.Hello</servlet-class>  
  28.     </servlet>  
  29.   
  30.     <servlet-mapping>  
  31.         <servlet-name>hello</servlet-name>  
  32.         <!-- 這是在瀏覽器中輸入的訪問該survlet的url,任意的 -->  
  33.         <url-pattern>/yanguoqi</url-pattern>  
  34.     </servlet-mapping>  
  35.   
  36. </web-app>  
  37. </span>  


第四步:輸入網址訪問

         到tomcat資料夾下,找到bin下得startup開啟,然後輸入:http://localhost:8080/myWebSite/yanguoqi進行訪。

注:reload一個servlet的方法:輸入網址:http://localhost:8080/,點Tomcat Manager。會要求輸入使用者名稱和密碼。我的是解壓縮版本的,因此要自己設定下。如果是安裝版的會提示設定密碼。找到conf資料夾下的tomcat-users,新增使用者及密碼:

<role rolename="manager-gui"/>
<user username="yan" password="yan" roles="manager-gui"/>

 

至此完畢!明日再戰。


相關文章