Servlet

一、介紹

  • 什麼是Servlet?

是運行在服務器段的程序,用來接受和響應來自客戶端基於HTTP協議的請求
imagehtml

二、執行流程

2.一、基於配置文件的開發()

  • 須要在web.xml中配置好Servlet
  • 每刷新一次瀏覽器都會執行一次service方法
    image
  • 經常使用的是繼承HttpServlet
    image
    image
    image

2.二、註解式開發(基本上都是用這種方式)

在servlet的java文件裏面添加下面的註解便可,就能夠代替上面的配置java

@WebServlet("/路徑名")

三、只用寫doGet和doPost其中之一的寫法

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Servlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello world");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//調用doGet便可!
        super.doGet(req, resp);
    }
}

其它

  • Application context
    虛擬目錄
  • servlet入口的url
    寫在地址欄Application context後面
    image

html的表單屬性怎麼寫

form action = "/虛擬目錄/servlet訪問目錄"
method = "get或者post"

servlet獲取數據代碼

req.getParameter("屬性名")web

相關文章
相關標籤/搜索