1、Request:设置请求数据
1.1、Request的继承系统
1.2、Request获取请求数据
获取请求行中的数据:
请求头和请求体:
POST获取请求体的方法如下:
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ///获取字符输入流 BufferedReader br = req.getReader(); ///读取数据 String s = br.readLine(); //输出读取的信息 System.out.println(s); }
输出信息:
username=17744&password=45616541
1.3、通用方法获取Request的请求参数
前端网页如下所示:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录</title></head><body><form action="/test/demo1" method="post"> 姓名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <!-- 复选框 --> <input type="checkbox" name="hobby" value="1"> 爬山 <input type="checkbox" name="hobby" value="2"> 游泳 <br> <input type="submit"></form></body></html>
Java代码如下所示:
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.Map;@WebServlet("/demo1")public class Demo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("get..."); //获取Map集合 Map<String, String[]> map = req.getParameterMap(); ///遍历Map集合 for (String key : map.keySet()) { System.out.print(key + ":"); ///通过键获得值 String[] values = map.get(key); for (String value : values) { System.out.print(value + " "); } System.out.println(); } ///根据Key获得参数值 String[] hobbies = req.getParameterValues("hobby"); for (String hobby : hobbies) { System.out.print(hobby + " "); } System.out.println(); //获取单个值 String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println("姓名为:" + username + "\t密码为:" + password ); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ///这里的dopost方法可以直接调用上述doget方法,因为获取请求参数的方法相同 this.doGet(req,resp); }}
1.4、中文乱码请求参数问题:
POST解决方案:
写在开头:request.setCharacterEncoding("utf-8");用于设置字符输入流的编码。
GET解决方案:==原因:编码和解码的方式不同!!!==
如下所示:
==解决方案:先将数据转换为字节数组,再通过字节数组转换为字符串!!==
import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*;import java.io.IOException;import java.nio.charset.StandardCharsets;@WebServlet("/demo2")public class Demo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); ///获得username的字节数组 byte[] bytes = username.getBytes(StandardCharsets.ISO_8859_1); //然后字节数组通过字节数组转换为字符串 username = new String(bytes,StandardCharsets.UTF_8); 上面的代码可以简写成以下形式 String username1 = new String(request.getParameter("username").getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8); ///输出信息 System.out.println(username); System.out.println(username1); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
==注:以上解决GET中文乱码的解决方案也适用于POST!!!==
1.5、request请求转发:服务器内部的资源跳转模式。
import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*;import java.io.IOException;@WebServlet("/demo3")public class Demo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo3..."); request.getRequestDispatcher("/demo4").forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
上述代码将输出:
demo3...demo4...
共享数据的操作方法:
2、Response:设置响应数据2.1、设置响应数据功能介绍
2.2、Response完成重定向:一种资源跳转的方式
使用如下所示:
import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*;import java.io.IOException;@WebServlet("/responsedemo1")public class Demo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("demo1..."); //设置响应状态码 response.setStatus(302); //参数是固定值,302 //设置响应头 response.setHeader("Location", "/test/responsedemo2"); //参数一是固定值,参数二是重定向资源路径,注意访问路径,添加项目名称 //简写为 response.sendRedirect("/test/responsedemo2"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
比较两种重定向和要求转发:
2.3、路径问题
谁使用明确的路径?
使用浏览器(发出):需要使用虚拟目录(项目访问路径)服务端(发出):不需要添加虚拟目录
常见例子如下:
2.4、虚拟目录的动态获取:
String contextPath = request.getContextPath();System.out.println(contextPath);
2.5、Response响应字符数据
@WebServlet("/responsedemo3")public class Demo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //让浏览器分析html标签并设置编码,能够正常识别中文 response.setContentType("text/html;charset=utf-8"); //获取字符输出流(此流不需要关闭,关闭可能有问题) PrintWriter writer = response.getWriter(); writer.write("aaa"); ///输出带html的字符串 writer.write("aaaaa"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}
2.6、Response响应字节数据
import org.apache.commons.io.IOUtils;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.annotation.*;import java.io.FileInputStream;import java.io.IOException;@WebServlet("/responsedemo4")public class Demo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ///阅读文件 FileInputStream fis = new FileInputStream("G:图片\\111.jpg"); //获得response字节输出流 ServletOutputStream os = response.getOutputStream(); Copy/完成流,从而在页面中显示图片(这里使用工具类,需要依赖xml) IOUtils.copy(fis, os); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }}