005_HttpServlet
生活随笔
收集整理的這篇文章主要介紹了
005_HttpServlet
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. Servlet、GenericServlet和HttpServlet的繼承關系:
Servlet (接口)
|
|
GenericServlet
|
|
HttpServlet (用于處理http的請求)
2. 在Eclipse使用Ctrl+T快捷鍵查看Servlet、GenericServlet和HttpServlet的繼承關系:
3. 新建一個HelloWorldHttpServlet的Web工程
4. 在WebContent下新建一個index.html
5. 編寫index.html
<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Hello World HttpServlet</title></head><body><h1>Hello World HttpServlet</h1></body> </html>6. 新建一個HelloWorldHttpServlet.java類
7. 編寫HelloWorldHttpServlet.java, 這里我們不再實行Servlet接口, 而是繼承HttpServlet這個抽象類
package com.lywgames.myservlet;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 HelloWorldHttpServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("doGet");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);} }8. 在web.xml里配置servlet
9. 運行項目, 在瀏覽器的地址欄中輸入:
http://localhost:8080/HelloWorldHttpServlet/HWHS.action訪問幾次
10. doGet和doPost調用流程
總結
以上是生活随笔為你收集整理的005_HttpServlet的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 004_LoadOnStartup
- 下一篇: 006_url-pattern配置