如何从表单输入传递cookie中的值?

时间:2013-12-02 19:59:37

标签: java java-ee servlets

我正试图从文本字段输入中获取值

在我的testservlet.java中

        out.println("<form id='name' action='' method='post'>");
        out.println("<input type="text" name='hostname'>");
        out.println("<input type="submit" value="Submit">");
        out.println("</form>");
        String name = request.getParameter("hostname");

并将其放入cookie中

        boolean flag = false;
        Cookie[] ck = request.getCookies();
        for(Cookie c : ck){
            if(c.getName() != null){
                out.println(c.getName());
                flag = true;
                break;
            }
        }


        if(!flag){
              Cookie c = new Cookie("name",name);
              c.setMaxAge(25);
              response.addCookie(c);
              out.println("user was add " +c.getName() + ", " + c.getValue());
        }

但它不起作用,出了什么问题?

0 个答案:

没有答案