JavaWeb零碎

小七已经被JavaWeb折磨疯魔

JavaWeb零零碎碎的知识点实在是太多了,建了三四个项目,写了一堆demo,懒得整合了,打算重新写个新的,之前的就当做笔记挤在这里叭~

目前记录了:cookie, session, 打开新网页,转发,输出流,画图,重定向,和一个登录后端代码qwq

添加cookie

public class demo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie = new Cookie("msg", "hello");
        response.addCookie(cookie);
    }
}

获取cookie

public class demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cs = request.getCookies();
        if (cs != null) {
            for (Cookie c : cs) {
                String name = c.getName();
                String value = c.getValue();
                System.out.println(name + ":" + value);
            }
        }
    }
}

Session

public class demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("msg", "hello");
    }
}
public class demo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Object msg = session.getAttribute("msg");
        System.out.println(msg);
    }
}

乱七八糟小Demo

打开新路径

@WebServlet("/demo1")
public class demo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("Demo1~");
        String contextPath = request.getContextPath();//根路径
        response.sendRedirect(contextPath + "/demo2");//打开demo2
    }
}

转发

@WebServlet("/demo2")
public class demo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("Demo2222~");
        request.getRequestDispatcher("/demo3").forward(request, response);//转发
    }
}

输出流

@WebServlet("/demo3")
public class demo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //System.out.println("Demo3~");
        response.setContentType("text/html;charset=utf-8");

        //获取字节输出流

        ServletOutputStream sos = response.getOutputStream();
        sos.write("啊啊啊".getBytes("utf-8"));
        ServletContext context = this.getServletContext();
        Object msg = context.getAttribute("msg");
        System.out.println(msg);


        //获取字符输出流
        response.setCharacterEncoding("utf-8");
        //response.setHeader("content-type", "text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write("<h1>咕咕咕啊啊啊aaaa</h1>");
    }
}

画图

@WebServlet("/demo4")
public class demo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int width = 100;
        int height = 50;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();
        g.setColor(Color.PINK);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width - 1, height - 1);

        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklzxcvbnm1234567890";
        Random random = new Random();
        for (int j = 1; j <= 4; ++j) {
            int i = random.nextInt(str.length());
            char ch = str.charAt(i);
            g.drawString(ch + "", width / 5 * j, height / 2);
        }
        g.setColor(Color.GREEN);
        for (int i = 1; i <= 10; ++i) {
            int x1 = random.nextInt(width - 1);
            int x2 = random.nextInt(width - 1);
            int y1 = random.nextInt(height - 1);
            int y2 = random.nextInt(height - 1);
            g.drawLine(x1, y1, x2, y2);
        }

        ImageIO.write(image, "jpg", response.getOutputStream());
    }
}

登录验证码

<head>
    <meta charset="UTF-8">
    <title>登录</title>

    <script>
        window.onload = function () {
            var img = document.getElementById("image");
            img.onclick = function () {
                var date = new Date().getTime();
                img.src = "/aaa/demo4?" + date;
            }
        }
    </script>
</head>
<body>
        <img id = "image" src="/aaa/demo4"/>

        <a id = "check" href="">看不清,换一张</a>
</body>

重定向

@WebServlet("/resDemo")
public class resDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Demo1~");
        response.setStatus(302);
        response.setHeader("location", "/reaDemo2");
    }
}
@WebServlet("/resDemo")
public class resDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Demo2~");
    }
}

登录

public class login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws SecurityException, IOException {
        req.setCharacterEncoding("utf-8");

        String name = req.getParameter("username");
        String password = req.getParameter("password");
        staff staff = new staff();
        staff.setName(name);
        staff.setPassword(password);

        staffDao dao = new staffDao();
        try {
            staff loginStaff = dao.login(staff);
            if (loginStaff == null) {
                req.getRequestDispatcher("/failLogin").forward(req, resp);
            }
            else {
                req.setAttribute("staff", staff);
                req.getRequestDispatcher("/successLogin").forward(req, resp);
            }
        } catch (SQLException | ServletException throwables) {
            throwables.printStackTrace();
        }
        return;
    }
}
上一篇:第二十章 责任链模式 Chain of Responsibility


下一篇:C#多线程3种创建Thread、Delegate.BeginInvoke、线程池