欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HttpServletRequest对象

程序员文章站 2022-03-10 13:04:37
...

HttpServletRequest:请求消息对象,一次请求信息
分为三部分:
1、请求行
a、请求方式
b、请求资源路径URL
c、协议版本号
2、请求头
3、请求实体

package com.zhouym.project;

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

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

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

	}
	private void getInfo(HttpServletRequest request, HttpServletResponse response) {
		// 请求行
		try {
			// 获取请求方式
			getRequestLine(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 获取请求资源
		getRequestURL(request, response);
		// 获取请求协议版本号
		getRequestProtocolVersion(request, response);
		//获取请求头信息
		getHeaders(request, response);
		//获取请求实体
		getRequestBody(request, response);
		//获取网络信息
		getRequestInternet(request, response);
	}
	private void getRequestLine(HttpServletRequest request, HttpServletResponse response) throws Exception {
			String method = request.getMethod();
			System.out.println(method);
	
		}
	private void getRequestURL(HttpServletRequest request, HttpServletResponse response) {
			String uri = request.getRequestURI();//统一资源标识符
			StringBuffer url = request.getRequestURL();//同一资源定位符
			System.out.println(uri);
			System.out.println(url);
			
			
		}

	private void getRequestProtocolVersion(HttpServletRequest request, HttpServletResponse response) {
		String protocol = request.getProtocol();//协议+版本
		System.out.println(protocol);
		String scheme = request.getScheme();
		System.out.println(scheme);//协议
	}

	private void getHeaders(HttpServletRequest request, HttpServletResponse response) {
		Enumeration<String> headerNames = request.getHeaderNames();//获取所有请求头信息的key值
		while (headerNames.hasMoreElements()) {
			String s = (String) headerNames.nextElement();
			System.out.println(s);
						
		}
		System.out.println("-------------分割线-------------");
		String header = request.getHeader("user-agent");
		System.out.println(header);
		String h = request.getHeader("accept-language");
		System.out.println(h);
		
	}

	private void getRequestBody(HttpServletRequest request, HttpServletResponse response) {
		String username = request.getParameter("username");
		System.out.println(username);
		String pwd = request.getParameter("pwd");
		System.out.println(pwd);
		
	}

	private void getRequestInternet(HttpServletRequest request, HttpServletResponse response) {
		String addr = request.getRemoteAddr();
		System.out.println(addr);
		int port = request.getRemotePort();
		System.out.println(port);
		
	}

响应消息分为三部分:
1、状态行
a、协议版本 b、状态码 c、状态描述信息
2、应答头
3、响应实体

状态行、响应实体与请求对象类似,不做介绍了

//应答头
private static void getResponseInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write("<p>你好</p>");
		
	}