HttpServletRequest和HttpServletResponse

HttpServletRequest:一次来自客户端的请求的相关信息

		String method = request.getMethod();
		System.out.println(method);
		String requestURI = request.getRequestURI();
		String string = request.getRequestURL().toString();
		System.out.println(requestURI);
		System.out.println(string);
		String protocol = request.getProtocol();
		System.out.println(protocol);
		String scheme = request.getScheme();
		System.out.println(scheme);
		String queryString = request.getQueryString();
		System.out.println(queryString);
		
		
		//请求头  获取所有的key,通过key获取value
		Enumeration<String> headerNames = request.getHeaderNames();
		while(headerNames.hasMoreElements()){
			System.out.println(headerNames.nextElement());
		}
		String header = request.getHeader("user-agent");
		System.out.println(header);
		
		//请求体
		//对于1,3编码方式  (application/x-www-form-urlencoded)(text/plain)
		String username = request.getParameter("username");
		System.out.println(username);
		String password = request.getParameter("password");
		System.out.println(password);
		
		//对于2编码方式(.multipart/form-data)
		ServletInputStream in = request.getInputStream();
		FileOutputStream out = new FileOutputStream("C:\\Users\\jia\\Desktop\\222.txt");
		
		int len =0;
		byte[] flush= new byte[1024];
		while( (len =in.read(flush)) !=-1){
			out.write(flush, 0, len);
		}
		out.close();
		in.close();

HttpServletResponse:一次发送给客户端的响应的相关信息 

	//HttpServletResponse :一次性发送给客户端的响应信息
		//设置应答头
		response.setHeader("content-type", "image/jpeg;charset=utf-8");
		//设置响应内容及字符集响应实体
		//response.setContentType("text/html; charset=utf-8") ;
		//获取打印流
		PrintWriter out = response.getWriter();
		out.write("<h1>你好呀</h1>");
		out.flush();
		out.close();
		//获取字节输出流
		FileInputStream in = new FileInputStream("C:\\Users\\jia\\Desktop\\111.jpg");
		ServletOutputStream out = response.getOutputStream();
		
		int len=0;
		byte[] flush= new byte[1024*1024];
		while( (len = in.read(flush)) !=-1){
			out.write(flush,0,len);
		}
		out.flush();
		out.close();
		in.close();

  

posted @ 2019-05-10 20:47  无声凉薄  阅读(193)  评论(0编辑  收藏  举报