Bootstrap

HttpServletRequest 和 HttpServletResponse

1. HttpServletRequest

1.1 HttpServletRequest 类的作用

每次只要请求进入Tomcat服务器,
Tomcat服务器会把请求的HTTP协议信息解析后的内容封装到Request对象中。
然后传递到service()方法(或者doGet() doPost()) 中给我们使用.
我们可以通过HttpServletRequest对象,获取到所有请求的信息.

1.2 HttpServletRequest 类的方法

getRequestURI() 获取请求的相对资源路径

getRequestURL() 获取请求的统一资源定位符(绝对路径)

getRemoteHost() 获取客户端的IP地址

getHeader() 获取请求头

getParameter() 获取请求的参数

getParameterValues() 获取多个请求参数

getMethond() 获取请求的方式 Get 或 Post

setAttribute(key, value) 设置域数据

getAttribute(key) 获取域数据

getRequestDispatcher() 获取请求转发对象

1.3 HttpServletRequest 获取请求参数

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
	//获取get请求  的参数 username  localhost:8080/test/testReq?username=lchh
	String username = req.getParameter("username);
}	

1.4 post请求中文乱码问题

  • 浏览器端页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div align="center">
		<form action="http://localhost:8080/servlet02/servlet01" method="post">
			
;