java web開發--Servlet學習之HelloWorld 從部署到開發全過程
今天終於搞出來來了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原始碼為:
- //這是我的第一個servlet,使用實現servlet介面的方式來開發
- package yanguoqi;
- import javax.servlet.*;
- import java.io.*;
- import javax.servlet.Servlet;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import java.io.IOException;
- public class Hello implements Servlet{
- /**
- * Method init
- * @param parm1
- *
- @throws ServletException
- *
- */
- //初始化servlet,類似於建構函式
- //只第一次訪問survlet時被呼叫
- public void init(ServletConfig parm1) throws ServletException {
- // TODO: 在這新增你的程式碼
- System.out.println("init ");
- }
- /**
- * Method getServletConfig
- * @return
- */
- //得到servlet配置檔案,不太重要
- public ServletConfig getServletConfig() {
- // TODO: 在這新增你的程式碼
- return null;
- }
- /**
- * Method service
- * @param parm1
- * @param parm2
- *
- @throws ServletException
- @throws IOException
- */
- //用於處理業務邏輯
- public void service(ServletRequest parm1, ServletResponse res) throws ServletException, IOException {
- // TODO: 在這新增你的程式碼
- System.out.println("service it");
- PrintWriter pw = res.getWriter();
- pw.println("Hello World!");
- }
- /**
- * Method getServletInfo
- * @return
- */
- public String getServletInfo() {
- // TODO: 在這新增你的程式碼
- return "";
- }
- /**
- * Method destroy
- */
- public void destroy() {
- // TODO: 在這新增你的程式碼
- System.out.println("destroy!");
- }
- }
注意這個程式寫好後要編譯下!每次修改後也應該編譯。
第三步:部署web.xml
開啟web.xml,坑爹的是JCreator貌似打不開,一開啟就報錯要求關閉。不得已,用eclipse開啟來編輯。
- <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
- <!-- ISO-8859-1 -->
- <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
- license agreements. See the NOTICE file distributed with this work for additional
- information regarding copyright ownership. The ASF licenses this file to
- You under the Apache License, Version 2.0 (the "License"); you may not use
- this file except in compliance with the License. You may obtain a copy of
- the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
- by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
- OF ANY KIND, either express or implied. See the License for the specific
- language governing permissions and limitations under the License. -->
- <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_2_5.xsd"
- version="2.5">
- <display-name>Welcome to Tomcat</display-name>
- <description>
- Welcome to Tomcat
- </description>
- <servlet>
- <!--給survlet起個名字,可以是任意的 -->
- <servlet-name>hello</servlet-name>
- <!--servlet的路徑(包名+類名)-->
- <servlet-class>yanguoqi.Hello</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>hello</servlet-name>
- <!-- 這是在瀏覽器中輸入的訪問該survlet的url,任意的 -->
- <url-pattern>/yanguoqi</url-pattern>
- </servlet-mapping>
- </web-app>
- </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"/>
至此完畢!明日再戰。
相關文章
- Flask Web開發學習之“HelloWorld”FlaskWeb
- 初學 Java Web 開發,請遠離各種框架,從 Servlet 開發JavaWeb框架Servlet
- Java程式從開發到執行經歷過程Java
- JavaWeb開發必過關-Servlet學習(一)JavaWebServlet
- 如何學習Java Web開發JavaWeb
- web開發1——servletWebServlet
- 從今天開始學習iOS開發(iOS 7版)–構建一款App之App開發過程iOSAPP
- apache模組開發之helloworldApache
- Web開發學習Web
- 微信公眾號支付開發全過程(Java 版)Java
- 從零開始學習Linux部署Java web專案LinuxJavaWeb
- 從 Java 應用部署方式看 IT 思潮——從開發和運維到開發自運維Java運維
- Java學習過程中實戰開發經驗重要嗎?Java
- web開發學習之旅Web
- Java初學者:Jsp開發環境配置全過程JavaJS開發環境
- Web開發從學些JavaScript開始WebJavaScript
- MDN之學習 Web 開發(一)【Web API簡介】WebAPI
- ODAC (odp.net) 從開發到部署
- 《Angular2 從開發到部署系列》之「環境搭建」Angular
- django學習之pythonbrew部署開發環境DjangoPython開發環境
- Flutter Web 開發部署FlutterWeb
- WEB應用開發中的ServletWebServlet
- pydev部署開發全解析dev
- java EE開發之Servlet第一課:servlet的建立方式JavaServlet
- java EE開發之Servlet第二課:Servlet的三個hashMapJavaServletHashMap
- java EE開發之Servlet第五課:xml解析JavaServletXML
- Java開發學習(九)----IOC之核心容器Java
- 遊戲開發雜記(三) 開發及學習過程中的體會遊戲開發
- 從web開發看.NET和Java。薦WebJava
- 全全全棧測試開發學習路線全棧
- FEer到全棧開發全棧
- 學習web前端開發的原因Web前端
- 從“黑掉Github”學Web安全開發GithubWeb
- 使用next.js完成從開發到部署JS
- 敏捷開發過程敏捷
- 《地圖氣球》小程式從產品到運維的個人全棧開發過程分享(長文)地圖運維全棧
- 全端開發之“兩端開發策略”
- go web 專案開發部署GoWeb