javaWeb-Request请求

javaWeb-Request请求

1.请求行

2.请求头

3.请求体

代码示例:

1.表单使用get请求的方式提交的时候:

前端代码:





Insert title here
	





抽烟 喝酒 烫头

后台servlet:

package com.helong.request;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

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("/RequestServlet")
public class RequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("RequestServlet");
		/*在前端使用get方式提交表单的时候获取的数据是ISO8859-1编码,要使用下面的方式将数据进行回炉操作,才能使得数据正常显示*/
		/** 通用方式解决乱码
	 	String parameter = request.getParameter("username");
		System.out.println(parameter);
		byte[] bytes = parameter.getBytes("ISO8859-1");
		String str = new String(bytes,"UTF-8");
		System.out.println(str);
	 	*/
		
		
		/***********1.请求行***********************/
		//1.1获取请求方式  get post
		String method = request.getMethod();
		System.out.println(method);
		
		//1.2请求资源 URL URI (URL:http://localhost:8080/27-Request/RequestServlet;URI=/27-Request/RequestServlet)
		StringBuffer requestURL = request.getRequestURL();
		String requestURI = request.getRequestURI();
		System.out.println("getRequestURL = "+requestURL);
		System.out.println("getRequestURI ="+requestURI);
		
		//1.3获取get请求参数
		String queryString = request.getQueryString();
		System.out.println("getQueryString  = "+queryString);
		
		//1.4获取当前web应用名称 (工程名)
		String contextPath = request.getContextPath();
		System.out.println("getContextPath = " +contextPath);
		
		
		/***********2.请求头***********************/
		//1.获取所有的请求头名称
		Enumeration headerNames = request.getHeaderNames();
		while(headerNames.hasMoreElements()) {
			System.out.println("name = "+headerNames.nextElement());
		}
		
		//2.根据名称获取指定的头信息
		String header = request.getHeader("referer");
		System.out.println("referer  = "+ header);
				
		//3.打印所有的请求头和请求头内容
		Enumeration headerNames2 = request.getHeaderNames();
		while(headerNames2.hasMoreElements()) {
			//获取请求名称
			String name = headerNames2.nextElement();
			String value = request.getHeader(name);
			System.out.println(name +":"+value);
		}	
		
		/***********3.请求体***********************/
		//1.获取一个值
		String name =  request.getParameter("username");
		/*将获取的数据转回二进制位,再使用UTF-8进行编码*/
		byte[] bytes = name.getBytes("ISO8859-1");
		name = new String(bytes,"UTF-8");
		System.out.println(name);
		
		//2.获取多个值
		String[] parameterValues = request.getParameterValues("hobby");
		for(int i = 0;i parameterNames = request.getParameterNames();
		while(parameterNames.hasMoreElements()) {
			System.out.println("name="+parameterNames.nextElement());
		}	
		
		//4.获取所有请求参数
		Map parameterMap = request.getParameterMap();
		//遍历所有请求参数名称与值
		for (Map.Entry entry: parameterMap.entrySet()) {
			/*System.out.println(entry.getKey());
			System.out.println(Arrays.toString(entry.getValue()));*/
			String[] value = entry.getValue();
			for(int i = 0;i

2.表单使用post请求的时候:

前端界面代码:






Insert title here






抽烟 喝酒 烫头

后台servlet代码:

package com.helong.request;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

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

import org.apache.commons.beanutils.BeanUtils;


@WebServlet("/RequestServlet2")
public class RequestServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("RequestServlet2");		
		
		
		//表单提交使用的提交方式为:post的时候可以使用这种方式来修改编码方式,仅仅只是适用于POST请求,不适用于GET
		request.setCharacterEncoding("UTF-8");
		//获取所有的请求参数
		Map allP = request.getParameterMap();
		for (Map.Entry entry : allP.entrySet()) {
			System.out.println("-------");
			System.out.println(entry.getKey() +":"+Arrays.toString(entry.getValue()));
		}
		
		/**  把获取的请求参数封装成对象    对象可以直接存到数据库*/
        /*使用BeanUtils封装对象之前一定要引入jar包BeanUtils.jar才能操作*/
		User u = new User();
		try {
			BeanUtils.populate(u, allP);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(u);
	}
}

将获获得的数据封装到User类中,创建一个javaBean类:

package com.helong.request;

import java.util.Arrays;

public class User {
	private String username;
	private String pwd;
	private String gender;
	private String[] hobby;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String[] getHobby() {
		return hobby;
	}
	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", pwd=" + pwd + ", gender=" + gender + ", hobby="
				+ Arrays.toString(hobby) + "]";
	}
	
}

 

你可能感兴趣的:(java,web,Request)