如何在html代码中使用cookie的值

How to use value of a cookie in html code?

本文关键字:cookie 的值 代码 html      更新时间:2023-09-26

我需要检索特定cookie的值,然后显示它,并在HTML代码中使用它。

我的代码如下,值显示在"value is:"消息之后,但我不知道如何将其作为myfunc函数的参数。

  <% Cookie cookies[] = request.getCookies();
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equalsIgnoreCase("MyCookie")) {
                    out.println("Value is:" + cookies[i].getValue());
                }
            }
        %>
        <form id="everything" onsubmit="myfunc(VALUE NEED TO BE HERE)">
             ......
<% Cookie cookies[] = request.getCookies();
   String myCookie = null;
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equalsIgnoreCase("MyCookie")) {
                myCookie = cookies[i].getValue();
                out.println("Value is:" + cookies[i].getValue());
            }
        }
    %>
    <form id="everything" onsubmit="myfunc('<%=myCookie%>')">

如果您在JSP文件中的Java代码中定义了一个变量:

<% int i = 4; %>

您可以使用以下标签使用它:

<%= i %>