0310 HttpServletRequest

回顾一下 servlet运行流程

 通过request获得请求行

获得客户端的请求方式:String getMethod()

获得请求的资源:

获取请求地址uri: String getRequestURI()

获取请求地址url:StringBuffer getRequestURL()

获取去web应用名称:String getContextPath()

获取get请求提交的参数:String getQueryString()

获取客户端的IP地址:getRemoteAddr()

代码展示

register.jsp

<%@ 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="/WEB04/LineServlet" method="get">
	用户名:<input type="text" name="uname"><br>
	密码:<input type="password" name="pwd"><br>
	爱好:
	足球:<input type="checkbox" name="hobby" value="zq">
	篮球:<input type="checkbox" name="hobby" value="lq">
	乒乓球:<input type="checkbox" name="hobby" value="ppq"><br>
	<input type="submit" value="注册">
</form>
</body>
</html>

  LineServlet

public class LineServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取哦请求方式
		String method=request.getMethod();
		System.out.println("请求方式"+method);
		//获取请求地址uri
		String uri=request.getRequestURI();
		System.out.println("请求地址uri:"+uri);
		//获取请求地址
		StringBuffer url=request.getRequestURL();
		System.out.println("请求地址url是:"+url);
		//获取去web应用名称
		String name=request.getContextPath();
		System.out.println("web应用名称是:"+name);
		//获取get请求提交的参数
		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);
	}
}

 

 

 如果jsp文件的提交方式改成post那么这个get请求提交参数就是null

通过request获得请求头

String getHeader();

referer头的作用:获取该访问的的来源

代码展示

public class HeadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取refer头
		String refer=request.getHeader("referer");
		System.out.println(refer);
	}

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

  还是使用register.jsp的代码将action中的路径改成/WEB04/HeadServlet

 

 referer头可以做一个防盗链的作用 就是可以判断这个访问来自哪里 如果不是我指定的访问路径 就不让他链接

代码展示

referer.jsp

<%@ 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>
<a href="/WEB04/RefererServlet">14天交易日A股暴跌30%</a>
</body>
</html>

  

public class RefererServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取referer头
		String refer=request.getHeader("referer");
		//解决响应乱码
		response.setContentType("text/html;charset=utf-8");
		if(refer.startsWith("http://localhost")){
			response.getWriter().write("这是一跳爆炸新闻");
		}else{
			response.getWriter().write("到连着 可耻");
		}
	}

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

  

 通过request获得请求体

获取请求体:

String getParameter(String key);

String[] getParameterValues(String name)

Map<String, String[]> getParameterMap()

解决post请求乱码

setCharacterEncoding("UTF-8") 指定码表

解决get 请求乱码

当客户端提交中文,那么先将数据提交,请求行会按照ISO8859-1码表进行解析 存到 request对象中 然后再传到服务器进行解析,因为服务器是utf-8码表所以解析不出来 ,所以就会响应给客户端一个乱码,这个解决这个乱码,我们可以再按照这个编码给他编译回去。

运用到

getbytes(“码表”)可以指定码表

new String(bytes,"码表")也可以指定码表

例:new String(parameter.getbytes("iso8859-1"),"utf-8")

代码展示

register.jsp post请求

<%@ 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="/WEB04/BodyServlet" method="post">
	用户名:<input type="text" name="uname"><br>
	密码:<input type="password" name="pwd"><br>
	爱好:
	足球:<input type="checkbox" name="hobby" value="zq">
	篮球:<input type="checkbox" name="hobby" value="lq">
	乒乓球:<input type="checkbox" name="hobby" value="ppq"><br>
	<input type="submit" value="注册">
</form>
</body>
</html>

  

public class BodyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决post请求乱码
		request.setCharacterEncoding("UTF-8");
		//获取请求体
		String uname=request.getParameter("uname");
		//解决get请求乱码
		//uname=new String(uname.getBytes("ISO8859-1"),"UTF-8");
		System.out.println("用户名"+uname);
		String pwd=request.getParameter("pwd");
		System.out.println("密码"+pwd);
		String[] hobbys=request.getParameterValues("hobby");
		for(String s:hobbys){
			System.out.println(s);
		}
		//获取所有参数所在的map集合
		Map<String, String[]> map=request.getParameterMap();
		//获取所有key躲在的set集合
		Set<String> keys=map.keySet();
		for(String key:keys){
			System.out.print(key+":");
			String[] values=map.get(key);
			for(String value:values){
				System.out.print(value+" ");
			}
			System.out.println();
		}
	}

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

  

 

 

 register.jsp get请求 将register文件中的post改成get,将bodyservlet文件中的 将解决post乱码的代码行注释掉,将解决get乱码代码行放开

 因为在上述代码中只解决了第一个name的乱码 并没有解决下边的那个map集合中的中文乱码。

 request其他功能

request是一个域对象

request对象也是一个存储数据的区域对象、

setAttribute(String name,Object j)

getAttribute(String name)

removeAttribute(String name)

注意:request域的作用范围:一次请求中

request完成请求转发

getRequestDispatcher(String path)方法获得一个RequestDispatcher转发器对象,通过调用RequestDispatcher对象的forward(ServletRequest request, ServletResponse response)方法完成请求转发

代码展示

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);;
		
	}

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

  

public class Servlet02 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		//取值
		String name=(String)request.getAttribute("name");
		response.getWriter().write("hello"+name);
	}

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

  

request域与ServletContext域的区别

request作用域是一次请求中,请求时创建,请求结束销毁

ServletContext作用域是整个项目文件,服务器开启状态部署文件时创建,服务器关闭销毁

重定向与请求转发区别

重定向是请求两次,地址栏改变,不能够传递数据,可以用于访问外部资源。

请求转发是请求一次,地址栏不改变,可以数据传递,只能访问内部资源

图解

 

posted @ 2021-03-10 11:26  公雪  阅读(114)  评论(0编辑  收藏  举报