HttpServletRequest概述

獲取請求行相關信息的相關方法:html

一、getMethod()方法:得到客戶端請求方式java

二、getRequestURI()方法:返回URI(URI是URL從主機和端口以後到表單數據以前的那一部分)。web

三、getRemoteAddr()方法:用於獲取請求客戶端的IP地址數組

四、getRemotePort()方法:用於獲取請求客戶端的端口號服務器

五、getLocalAddr()方法:用於獲取服務器當前接收請求的IP地址oracle

六、getContextPath()方法:該方法用於獲取URL中屬於web應用程序的路徑post

七、getProtocol()方法:該方法用於獲取請求中的協議名和版本ui

8.   getQueryString():get提交url地址後的參數字符串url

package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LineServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//獲取請求的方式
		String method=request.getMethod();
		System.out.println("請求方式爲:"+method);
		//獲取請求的URL
		String URI=request.getRequestURI();
		System.out.println("URI爲:"+URI);
		//獲取請求的URL
		StringBuffer URL=request.getRequestURL();
		System.out.println("URL爲:"+URL);
		//獲取WEB應用名稱
		String name=request.getContextPath();
		System.out.println("WEB應用名稱爲:"+name);
		//獲取請求get請求後的URL後的字符串
		String query=request.getQueryString();
		System.out.println("get請求參數爲:"+query);
		//獲取客戶端的IP地址
		String ip=request.getRemoteAddr();
		System.out.println("ip地址爲:"+ip);
	}

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

 

獲取請求頭的相關方法:spa

一、getHeader(String name):該方法用於獲取一個指定頭字段的值,若是請求頭中不包含該字段則返回null,若是包含多個該字段的值則獲取第一個值。

獲取請求參數:獲取單個參數

一、getParameter(String name):用於獲取某個指定名稱的參數值,若是請求中沒有包含指定名稱的參數,則返回null,若是有指定參數可是沒有給設置值,則返回空串,若是包含多個該參數的值則返回第一個出現的參數值

二、getParameterNames():該方法用於返回一個包含請求消息中全部參數名的枚舉集合

三、getParameterMap():將請求中全部參數和值裝入一個map對象而後返回

4  .getParametervalues(String name):獲取多個值的參數,以數組的String數組的方式

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <form action="/WEB06/BodyServlet" method="get">
       <input type="text" name="username"><br>
       <input type="radio" name="sex" value="man">男
       <input type="radio" name="sex" value="woman">女
       <br>
       <input type="checkbox" name="hobby" value="ppq">
                   乒乓球
       <input type="checkbox" name="hobby" value="pq">
                   排球
       <input type="checkbox" name="hobby" value="wq">
                   網球
       <br>
       <input type="submit" value="提交">
   </form>
</body>
</html>

 

  

package com.oracle.demo01;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

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

public class BodyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//post請求解決中文亂碼問題
		//request.setCharacterEncoding("utf-8");
		//獲取請求參數
		//1.獲取單個值的參數
		String name=request.getParameter("username");
		name=new String(name.getBytes("ISO-8859-1"),"UTF-8");
		String sex=request.getParameter("sex");
		System.out.println(name+"...."+sex);
		//獲取多個值的參數,以數組的String數組的方式
		String[] hobbys=request.getParameterValues("hobby");
		//遍歷
		for(String s:hobbys){
			System.out.println(s);
		}
		//獲取全部值的請求參數Map
		Map<String,String[]> map=request.getParameterMap();
		//遍歷
		Set<String> set=map.keySet();
		for(String str:set){
			String[] value=map.get(str);
			for(String v:value){
				System.out.println(str+"....."+v);
			}
		}
	}

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

 注意:

      解決post提交方式的亂碼:request.setCharacterEncoding("UTF-8");

      解決get提交的方式的亂碼:

            parameter = new String(parameter.getbytes("iso8859-1"),"utf-8");

request:

     只請求一次

     url地址不會改變

相關文章
相關標籤/搜索