Servlet处理请求报文9

处理请求报文起始行和报文首部字段都是来自于HttpServletRequest接口的方法
处理起始行的方法
getMethod         方法返回HTTP请求消息中的请求方式。
getRequestURI     方法返回请求行中的资源名部分。
getQueryString      方法返回请求行中的参数部分。
getProtocol     方法返回请求行中的协议名和版本。
处理首部字段的方法

package com.ls.javaees1;

import java.io.IOException;
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("/headerrequest")
public class RequestHeaderServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println(request.getMethod());
        System.out.println(request.getRequestURI());
        System.out.println(request.getQueryString());
        System.out.println(request.getProtocol());

        Enumeration<String> headerNames=request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String headerName=headerNames.nextElement();
            System.out.println(headerName+","+request.getHeader(headerName));
        }
        System.out.println(request.getContentType());//返回Cintent-Type首部字段的值
        System.out.println(request.getContentLength());//返回Cintent-Length首部字段的值
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
发布了48 篇原创文章 · 获赞 1 · 访问量 7490
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章