RequestInfoServlet接口: 提取HTTP請求報頭信息

代碼塊
```package com.foreknow.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

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

/**
 *RequestInfoServlet接口: 提取HTTP請求報頭信息
 * @author Administrator
 *
 */
public class RequestInfoServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //服務(wù)器端先客戶端響應(yīng)的內(nèi)容的類型
        resp.setContentType("text/html;charset=utf-8");
        
        //req.getContextPath();獲取上下文的路徑(當(dāng)前項目的路徑)
        String contextPath=req.getContextPath();
        
        //PrintWriter可以在網(wǎng)頁中輸出內(nèi)容
        PrintWriter out=resp.getWriter();
        out.println("獲取工程路徑:"+contextPath+"<br>");
        out.println("獲取客戶端向服務(wù)器端請求的方法:"+req.getMethod()+"<br>");
        out.println("servlet的映射地址:"+req.getRequestURI());
        out.flush();
        out.close();
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    }

}

?著作權(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)容