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!");
        }
    }
  • 相关阅读:
    VC++用Recordset MSPersist载入C#DataSet Save出来的xml失败,但载入VC Recordset Save出来的xml则没问题,怎么xml不通用呢?
    观察力、分析问题的能力、与人沟通的能力和资源整合能力
    [导入]有感于神童之神源
    军训系列二:两类人创业不容易成功
    运行微软的SOAP3.0的VC样例Samples30_ADOTM_Client报错,m_pSoapClient>Invoke时直接失败
    About IM software
    [导入][转]好企业是什么样?
    动网论坛v7.0.0SQL版竟然帯病毒!
    CZoneSoft出品: 音频视频在线录制系列之 AV留言本 简介
    递归算法在生成树型结构中,几乎完全属于无稽的算法
  • 原文地址:https://www.cnblogs.com/leisureeen/p/12403081.html
Copyright © 2011-2022 走看看