HttpServletResponse和HttpServletRequest的简单实用

# 1、HttpServletResponse

web服务器接收到客户端的http请求,针对这个请求,分别创建一一个代表请求的HttpServletRequest
对象,代表响应的- -个HttpServletResponse;

  • 如果要获取客户端请求过来的参数:找HttpServletRequest
  • 如果要给客户端响应一些信息: 找HttpServletResponse

响应状态码

200:请求响应成功200
3xx:请求重定向 重定向:你重新到我给你新位置去;

4xx:找不到资源404 资源不存在;
5xx:服务器代码错误500 502:网关错误

response下载文件

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realPath = "D:\\IDEA\\javaweb-01-maven\\javaweb-01-maven\\javaWeb-servlet\\lession2\\target\\classes\\1.png";
        System.out.println("下载文件的路径"+realPath);
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
        resp.setHeader("Content-disposition","attachment;filename="+java.net.URLEncoder.encode(fileName, "UTF-8"));
        FileInputStream in = new FileInputStream(realPath);
        int len=0;
        byte[] buffer = new byte[1024];
        ServletOutputStream out = resp.getOutputStream();
        while ((len=in.read(buffer))>0){
            out.write(buffer,0,len);
        }
        in.close();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

验证码实例

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("refresh","3");
        BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        g.setColor(Color.blue);
        g.setFont(new Font(null,Font.ITALIC,20));
        g.drawString(makeNum(),0,20);
        resp.setContentType("image/jpeg");
        resp.setDateHeader("expires",-1);
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("pragma","no-cache");
        boolean write = ImageIO.write(image, "jpg", resp.getOutputStream());
    }

    private String makeNum(){
        Random ran=new Random();
        String num=ran.nextInt(9999999)+"";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <7-num.length() ; i++) {
            sb.append("0");
        }
        num=sb.toString()+num;
        return num;
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

重定向 response.sendRedirect()

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Location","/lession2_war/image");
        resp.setStatus(302);
        resp.sendRedirect("/lession2_war/image");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

请求转发与重定向的区别:

  • 相同点:都能实现页面跳转

  • 不同点:请求转发不改变URL的地址 307

    ​ 重定向改变URL的地址 302

    image-20200517232118239

2、HttpServletRequest

jsp编写form表单乱码问题解决办法

<%@ page contentType="text/html; charset=UTF-8"%>

request处理表单

<html>
<body>
<%--${pageContext.request.contextPath}代表当前的项目--%>
 <%@ page contentType="text/html; charset=UTF-8"%>
<form action="${pageContext.request.contextPath}/login" method="get">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" name="submit" value="提交">
    <input type="reset" name="reset" value="重置">
</form>
</body>
</html>
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("进入请求");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        resp.sendRedirect("/lession2_war/test.jsp");//重定向到成功页面
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

req.getParameter(获取单个值) req.getParameterValues(获取集合)获取前端参数

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        String[] hobbies = req.getParameterValues("hobby");
        System.out.println("============================");
        System.out.println(username);
        System.out.println(pwd);
        System.out.println(Arrays.toString(hobbies));
        System.out.println("============================");

        resp.setCharacterEncoding("UTF-8");
        req.getRequestDispatcher("success.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
posted @ 2020-05-18 14:57  我不爱吃鱼  阅读(450)  评论(0编辑  收藏  举报
Live2D