天天看點

Eclipse中Tomcat實作表單Get和Post響應

前言:

想了解 HTTP協定内容的可以參考:

https://blog.csdn.net/qq_38409944/article/details/80628723
URL:  統一資源定位符。http://localhost:8080/day09/testImg.html。隻能定位網際網路資源。是URI的子集。
URI: 統一資源标記符。/day09/hello。用于标記任何資源。可以是本地檔案系統,區域網路的資源(//192.168.14.10/myweb/index.html),可以是網際網路。           

首先看一下Get和Post之間的差異:

注意:當頁面重新整理的時候,資料會自動重新送出,但是沒有提醒

Get:

GET送出參數資料有限制,不超過1KB。
GET方式不适合送出敏感密碼。
浏覽器直接通路的請求,預設送出方式是GET方式
位址欄(URI)會跟上參數資料。以?開頭,多個參數之間以&分割。
           

POST:

POST送出的參數資料沒有限制。
POST方式送出敏感資料。
參數不會跟着URI後面。參數而是跟在請求的實體内容中。  
沒有?開頭,多個參數之間以&分割。           

表單的話 一般都是POST送出。

GET送出的話 賬号密碼都會顯示在URL中,如:

http://localhost:8080/haha/zz?name=11&password=d           

而POST送出的話,賬号密碼不會顯示,而是放在HTTP協定中的實體内容中

看一下前背景互動的原理圖:

核心的API:
    請求行: 
        request.getMethod();   請求方式 GET 還是POST
        request.getRequetURI()      請求資源的URI位置
        // request.getRequetURL()   請求資源的URL位置
        request.getProtocol()   請求http協定版本
                    
    請求頭:
        request.getHeader("名稱")   根據請求頭擷取請求值
        request.getHeaderNames()    擷取所有的請求頭名稱

    實體内容:
        request.getInputStream()     
        擷取實體内容資料   主要是POST擷取賬号密碼。           

首先建立表單:

<body>
<form action="/haha/zz" method="get">
<input type="text" name="name"/>
<input type="password" name="password" />
<input type="submit" value="tijiao" />
</form>
<form action="/haha/zz" method="post">
<input type="text" name="name"/>
<input type="password" name="password" />
<input type="submit" value="tijiao" />
</form>
</body>           

注意:

action="/haha/zz"中

haha

是項目名字

zz

是在src中建立的servlet檔案

其實action填的是你要傳遞的URI位置。

如:

http://localhost:8080/haha/zz           

/haha/zz取得就是8080後面的URI位置。

Servlet:

HttpServletRequest對象接受前台 Http請求,包括請求行,請求頭(多個key-value對象),可選)實體内容。

Http請求資料被包裝在 HttpServletRequest對象中。

package a;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/zz")
public class zz extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        t1(request);
        t2(request);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {

        t1(request);
        t2(request);
        t3(request);
    }

    private void t3(HttpServletRequest request) throws IOException {
        InputStream inputStream=request.getInputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=inputStream.read(buffer))!=-1) {
            String string=new String(buffer, 0, len);
            System.out.println(string);
        }
    }

    private void t2(HttpServletRequest request) {
        Enumeration<String> enumeration=request.getHeaderNames();
        while(enumeration.hasMoreElements()) {
            String name=enumeration.nextElement();
            String content=request.getHeader(name);
            System.out.println(name+" "+content);
        }
    }

    private void t1(HttpServletRequest request) {
        System.out.println(request.getMethod());
        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL());
        System.out.println(request.getProtocol());
    }

}
           

輸出請求行函數:

private void t1(HttpServletRequest request) {
        System.out.println(request.getMethod());
        System.out.println(request.getRequestURI());
        System.out.println(request.getRequestURL());
        System.out.println(request.getProtocol());
    }           

請求頭:

private void t2(HttpServletRequest request) {
Enumeration<String> enumeration=request.getHeaderNames();
        while(enumeration.hasMoreElements()) {
            String name=enumeration.nextElement();
            String content=request.getHeader(name);
            System.out.println(name+" "+content);
        }
    }           

實體内容函數:

private void t3(HttpServletRequest request)   
throws IOException {
        InputStream inputStream=request.getInputStream();
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=inputStream.read(buffer))!=-1) {
            String string=new String(buffer, 0, len);
            System.out.println(string);
        }
    }           

總結:

1. POST實體内容即t3得到的是輸入的賬号和密碼。
而GET實體内容一般無内容 因為賬号和密碼會顯示在URL框内了           
2. 如果修改了表單或者Servlet,隻要直接ctrl+S儲存就行,  
   然後等待Tomcat自動更新下即可。           
3. doGet(HttpServletRequest request,   
HttpServletResponse response)是GET格式,調用伺服器的方法。
 doPost(HttpServletRequest request,   
 HttpServletResponse resp)是POST格式,調用伺服器的方法           
4. 浏覽器預設是GET格式,是以當你啟動servlet的時候,  
   預設是調用doGet方法           
5. 當你點選送出按鈕的時候,頁面會跳轉到servlet頁面,  
   然後根據Get還是Post來選擇調用doGet還是doPost方法。           

servlet中的service()方法重寫與不重寫:

在servlet中預設情況下,無論你是get還是post 送出過來 都會經過service()方法來處理,然後轉向到doGet或是doPost方法

從上面可以看出 這裡的service是用來轉向的,但是如果你在自己的servlet類中覆寫了service方法,比如說你的service是這樣的:

public void service(ServletRequest req, ServletResponse res)
                   throws ServletException, IOException {
        res.getOutputStream().print(
         "image is <img src='images/downcoin.gif'></img><br>");
    }           

那麼這時service就不是用來轉向的,而是用來處理業務的,現在不論你的用戶端是用pos還是get來請求此servlet

都會執行service方法也隻能執行servlet方法,不會去執行doPost或是doGet方法。

要是你以後寫servlet,最好不要重寫service方法啊

為什麼不應該重寫service方法,似乎是為了保留HttpServlet預設實作的緩存協商的機制;其實還有另外一個原因:就是禁用你沒有在servlet中重寫的方法,例如post、head等,這樣就從一定程度上提高了安全性。

參考:

https://my.oschina.net/dtkking/blog/89443

繼續閱讀