1、HttpServletRequest概述:
我们在创建Servlet时,会重写service()方法、doGet()\、doPost()方法,这些方法都有两个参数request(请求)和response(响应),service中的request是ServletRequset,而
doGet()和doPost()方法中的是:HttpServletRequest,HttpServletRequest是ServletRequest的子接口。
2、request的运行流程:
3、通过request获得请求行
(1)获得客户端的请求方式:
String method=request。getMethod()
(2)获得请求资源:
String URI=request。getRequestURI();
String URL=request.getRequestURL();
(3)获得WEB应用的名称
String webname=request.getContextPath();
(4)get提交url地址后的参数字符串
String queryString=request.getQueryString();
(5)获得访问的客户端IP地址
String ip=request.getRemoteAddr();
4.通过request获得请求头
(1)获取请求头
String agent=request.getHeader("User-Agent");
(2)
获取所有请求头名称
Enumeration
while(headerNames.hasMoreElements()){
//获取每个请求头名称
String headerName=headerNames.nextElement();
//跟距请求头获取请求值
String value=request.getHeader(headerName);
System.out.println(headerName);
System.out.println(value);
referer头的作用:执行该此访问的的来源
做防盗链跳转
package servlet;
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 ReferServlet extends HttpServlet {
//防盗链
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头中的referer
String referer=request.getHeader("referer");
//不是以"http://localhosr:8080"开头的不能访问
if(referer!=null&&referer.startsWith("http://localhosr:8080")){
//认为可以正常访问
//结觉中文乱码
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("啊啊啊啊啊啊啊啊");
}else{
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("不不不不不不不不不不不不不不不不不不不不不不不不");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.通过request获得请求体解决中文乱码等问题
获取表单元素的值:String username=request.getParameter("username");
获取表单所有元素的值:Map<String,String[]> map=request.getParameterMap();
package servlet;
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");
//获取表单元素的值、
//get方式结觉中文乱码问题
String username=request.getParameter("username");
//username=new String
//(username.getBytes("iso8859-1"),"utf-8");
String password=request.getParameter("password");
System.out.println(username+"..."+password);
//获取表单所有元素的值
Map<String,String[]> map=request.getParameterMap();
Set<String> set=map.keySet();
for(String s:set){
String key=s;
String[] value=map.get(s);
//遍历数组
for(String s1:value){
System.out.println(s+":"+s1);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
6、request是一个域对象
request对象也是一个存储数据的区域对象,所以也具有如下方法:
setAttribute(xxxx, xxxxxxxxxxx)
getAttribute(String name)
removeAttribute(String name)
注意:request域的作用范围:一次请求中
7、request完成请求转发
获得请求转发器----path是转发的地址
RequestDispatcher getRequestDispatcher(String path)
通过转发器对象转发
**requestDispathcer.**forward(ServletRequest request, ServletResponse response)
转发:
package servlet;
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 Servlet01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向request中内放值
request.setAttribute("name","小红帽");
//请求转发
request.getRequestDispatcher("/Servlet02").forward(request, response);
response.getWriter().write("servlet01");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package servlet;
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 Servlet02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=(String) request.getAttribute("name");
//解决中文乱码
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("servlet02"+name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}