zoukankan      html  css  js  c++  java
  • TestCookie

    设计要求:

    默认首页为“pages/index.html”,有生成32位大写字母(A~F)和数字(0~9)混合的随机字符串功能,有设置Cookie,读取Cookie的功能。

    前端HTML页面(index.html):

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <title>Powered By Leisureeen</title>
    <script src="./js/req.js"></script>
    <script type="text/javascript">
        function b_req(index) {
            var idV = document.getElementById("id").value;
            var result = document.getElementById("res");
            if (index == 1)
                Request("action.do", "post", "index=" + index + "&id=" + idV,
                        result);
            else
                Request("action.do", "post", "index=" + index, result);
        }
    </script>
    </head>
    <body bgcolor="AADDFF">
        <div align="center" style="line-height: 30px;">
            <input type="text" id="id" maxlength="8" value="">
            <br>
            <input type="submit" value="Load Cookie" onclick="b_req(0)">
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="submit" value="Store Cookie" onclick="b_req(1)">
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="submit" value="UUID" onclick="b_req(2)">
            <br>
            <div id="res" style="color: red"></div>
        </div>
    </body>
    </html>

    req.js文件:

    function Request(url, action, json, result) {
        var httpRequest = new XMLHttpRequest();
        httpRequest.open(action, url, true);
        httpRequest.setRequestHeader("Content-type",
                "application/x-www-form-urlencoded");
        httpRequest.send(json);
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200)
                result.innerHTML = httpRequest.responseText;
        };
    }

    web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>TestCookie</display-name>
    <!--
      ALL functions
      1 welcome-file
      2 UUID
      3 Cookie
    -->
        <welcome-file-list>
            <welcome-file>pages/index.html</welcome-file>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>servlet</servlet-name>
            <servlet-class>controller.Servlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>servlet</servlet-name>
            <url-pattern>/action.do</url-pattern>
        </servlet-mapping>
    </web-app>

    后端收发数据Java类(Servlet.java):

    package controller;
    
    import java.io.IOException;
    import java.util.UUID;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Servlet extends HttpServlet{
    
        protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException{
            req.setCharacterEncoding("utf-8");
            String index=req.getParameter("index");
            // resp.setContentType("application/json; charset=utf-8");
            resp.setContentType("text/html;charset=utf-8");
            if(index.equals("0")){
                Cookie[] cookies=req.getCookies();
                if(cookies!=null)
                    for(Cookie cookie: cookies)
                        if(cookie.getName().equals("id"))
                            resp.getWriter().print("id is "+cookie.getValue());
            }else if(index.equals("1")){
                String id=req.getParameter("id");
                Cookie cookie=new Cookie("id",id);
                resp.addCookie(cookie);
                resp.getWriter().print("AddCookie Success!");
            }else if(index.equals("2")){
                String uuid=UUID.randomUUID().toString().replace("-","").toUpperCase();
                resp.getWriter().print(uuid);
            }else
                resp.getWriter().print("index error!");
        }
    }
  • 相关阅读:
    【安全运维】在Windows平台利用sysmon进行安全分析
    【渗透测试】利用分块传输绕安全狗
    【渗透测试】如何获取目标网站真实IP
    【渗透测试】渗透测试常用在线工具
    【读书笔记】《互联网企业安全建设高级指南》6-17
    【安全运维】linux安全加固项目
    【安全运维】初识osquery
    【渗透测试】使用隧道模式访问目标数据库
    【企业安全】使用文件hash进行威胁分析
    【编程开发】python学习-判断是否是私网IP地址
  • 原文地址:https://www.cnblogs.com/leisureeen/p/12403081.html
Copyright © 2011-2022 走看看