javaweb开发(五):HttpServletRequest与HttpServletResponse详解
-
构建1个web项目,导入依赖
-
查看HttpServlet类
-
下载源码包,下载链接
-
解压并导入
-
新建RequsetServlet
@WebServlet("/request")
public class RequsetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
//客户端请求信息
System.out.println("应用上下文路径 getContextPath="+request.getContextPath()) ;
System.out.println("客户端发出请求时的完整URL getRequestURL="+request.getRequestURL()) ;
System.out.println("请求行中的资源名部分 getRequestURI="+request.getRequestURI()) ;
System.out.println("请求行中的参数部分 getQueryString="+request.getQueryString()) ;
System.out.println("发出请求的客户机的IP地址 getRemoteAddr="+request.getRemoteAddr()) ;
System.out.println("客户机发请求使用的网络端口号 getRemotePort="+request.getRemotePort()) ;
//获取请求头
System.out.println("获取请求头 getHeader(Accept)="+request.getHeader("Accept")) ;
//获取请求参数
System.out.println("客户端请求参数 getParameter="+request.getParameter("userName"));
String[] sport = request.getParameterValues("sport");
System.out.println("客户端请求参数列表,多个值 getParameterValues="+sport.toString());
Map<String,String[]> map = request.getParameterMap();
System.out.println("客户端请求参数封装成的map类型 getParameterMap="+map.toString());
//存储到reqeust作用域
request.setAttribute("name","jack");
}
}
-
tomcat配置如下
-
测试
http://127.0.0.1:8080/web03/request?userName=jack&age=11&sport=ball&sport=sleep
- 控制台输出如下
12-Apr-2022 22:15:39.484 淇℃伅 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory 鎶妛eb 搴旂敤绋嬪簭閮ㄧ讲鍒扮洰褰� [C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]
12-Apr-2022 22:15:39.531 淇℃伅 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory Web搴旂敤绋嬪簭鐩綍[C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]鐨勯儴缃插凡鍦╗47]姣鍐呭畬鎴�
应用上下文路径 getContextPath=/web03
客户端发出请求时的完整URL getRequestURL=http://127.0.0.1:8080/web03/request
请求行中的资源名部分 getRequestURI=/web03/request
请求行中的参数部分 getQueryString=userName=jack&age=11&sport=ball&sport=sleep
发出请求的客户机的IP地址 getRemoteAddr=127.0.0.1
客户机发请求使用的网络端口号 getRemotePort=54357
获取请求头 getHeader(Accept)=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
客户端请求参数 getParameter=jack
客户端请求参数列表,多个值 getParameterValues=[Ljava.lang.String;@345e0725
客户端请求参数封装成的map类型 getParameterMap=org.apache.catalina.util.ParameterMap@6b83c953
实现登录提交
- 新建LoginServlet
@WebServlet(name = "loginServlet",urlPatterns = {"/user/loin"})
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String userName = req.getParameter("userName");
String pwd = req.getParameter("pwd");
if(userName.equals("user") && pwd.equals("12345")){
resp.getWriter().write("登录成功");
}else {
resp.getWriter().write("账号密码错误");
}
}
}
- 编写index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="/web03/user/loin" method="post">
<div>用户名: <input type="text" name="userName" /> </div>
<div>密码: <input type="password" name="pwd"/> </div>
<div> <input type="submit" value="登录"> </div>
</form>
</body>
</html>
-
启动测试
-
解决乱码
//POST⽅式遇到中⽂乱码,如果客户端是以UTF-8字符编码,需要服务器以UTF-8的编码接收数据,
req.setCharacterEncoding("UTF-8");
//对于以get⽅式传输的中⽂数据乱码需要另外⼀种⽅式,默认的还是使⽤ISO8859-1这个字符编码来接
收数据
//办法:对接收到的原始数据的字节数组,然后通过字节数组以指定的编码构建字符串,解决乱码问题
String name = request.getParameter("name");//接收数据
name =new String(name.getBytes("ISO8859-1"), "UTF-8")
HttpServletResponse响应对象讲解
- 编写ResponseServlet
@WebServlet("/response")
public class ResponseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
// 设置编码
response.setCharacterEncoding("UTF-8");
response.addHeader("name", "java");
response.addHeader("name", "spring");
}
}
-
测试
-
设置只有⼀个值的http响应头,参数name表示响应头名称,参数value表示响应头的值
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("age","23");
}
- 设置状态码
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
}
- 字节输出流对象
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type","text/html;charset=utf-8");
String data = "hello world";
OutputStream outputStream = response.getOutputStream();
byte[] bytes = data.getBytes("UTF-8");
outputStream.write(bytes);
}
- 字符输出流对象
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type","text/html;charset=utf-8");
String data = "hello world";
PrintWriter printWriter = response.getWriter();
printWriter.write(data);
}