關(guān)于servlet解析HTTP請求的一點筆記

0. 說明

筆記內(nèi)容關(guān)于以下方法:

  • String getContextPath()
  • String getQueryString()
  • String getRequestURI()

菜鳥教程的描述如下:

下面的方法可用在 Servlet 程序中讀取 HTTP 頭。這些方法通過 HttpServletRequest 對象可用。

序號 方法 & 描述
13 String getContentType()
返回請求主體的 MIME 類型,如果不知道類型則返回 null。
19 String getQueryString()
返回包含在路徑后的請求 URL 中的查詢字符串。
23 String getRequestURI()
從協(xié)議名稱直到 HTTP 請求的第一行的查詢字符串中,返回該請求的 URL 的一部分。

1. 示例代碼

import java.io.IOException;
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("/NewServlet/*")
public class NewServlet extends HttpServlet {

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String contextPath = request.getContextPath();
        String queryString = request.getQueryString();
        String requestURI = request.getRequestURI();
        
        response.getWriter().println(
                "ContextPath: " + contextPath + "\n" +
                "QueryString: " + queryString + "\n" +
                "RequestURI: " + requestURI
        );
    }

}

2. 請求結(jié)果

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet
ContextPath: /WebApplication1
QueryString: null
RequestURI: /WebApplication1/NewServlet

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet/
ContextPath: /WebApplication1
QueryString: null
RequestURI: /WebApplication1/NewServlet/

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet/123
ContextPath: /WebApplication1
QueryString: null
RequestURI: /WebApplication1/NewServlet/123

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet?id=123
ContextPath: /WebApplication1
QueryString: id=123
RequestURI: /WebApplication1/NewServlet

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet?id=123?num=123
ContextPath: /WebApplication1
QueryString: id=123?num=123
RequestURI: /WebApplication1/NewServlet

C:\Users\asus>curl http://localhost:8080/WebApplication1/NewServlet?id=123/123
ContextPath: /WebApplication1
QueryString: id=123/123
RequestURI: /WebApplication1/NewServlet

C:\Users\asus>curl -G -d 'id=123' -d 'tag=tag' http://localhost:8080/WebApplication1/NewServlet
ContextPath: /WebApplication1
QueryString: 'id=123'&'tag=tag'
RequestURI: /WebApplication1/NewServlet

C:\Users\asus>curl -G -d 'id=123' -d 'tag=tag' http://localhost:8080/WebApplication1/NewServlet/
ContextPath: /WebApplication1
QueryString: 'id=123'&'tag=tag'
RequestURI: /WebApplication1/NewServlet/
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容