检查客户端计算机上是否有cookie,否则您执行某些操作并退出

问题描述 投票:1回答:1

该练习的内容是:“编写一个Web应用程序,创建一个电子调查页面,用于选择市长的五个候选人之一。该应用程序必须包括设置cookie以在已经表达的客户机器上注册投票如果客户端连接的机器不包含投票注册cookie,则允许用户进入投票页面,否则用户必须自动重定向到显示五个候选人获得的投票百分比的统计页面。 “。

如何检查客户端计算机上是否已存在cookie?我试过但是正确地从html文件中我做了第一次投票,输入if然后停止,但它不好,因为它将是第一次投票而不是第二次。救命 !!

public class SerElection extends HttpServlet{
  int cGhezzi=0, cPelliccia=0, cValente=0, cBussi=0, cFerri=0;

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException{
    String candid = request.getParameter("candidato");
    Cookie cookie = new Cookie(candid,candid);
    response.addCookie(cookie);
    String c = cookie.getName();
    if(c.equals("Ghezzi") || c.equals("Pelliccia") || c.equals("Valente") || c.equals("Bussi") || c.equals("Ferri")){
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head>");
      out.println("<title></title>");
      out.println("</head>");
      out.println("<body>");
      out.println("<h3>I'm sorry but you've already voted</h3>");
      out.println("</body>");
      out.println("</html>");
      out.close();
    }
else{
      //ACCORDING TO THE CHOICE OF CHOICE INCREMENT ITS VARIABLE FOR STATISTICS
      if(candid.equals("Ghezzi")) cGhezzi++;
      if(candid.equals("Pelliccia")) cPelliccia++;
      if(candid.equals("Valente")) cValente++;
      if(candid.equals("Bussi")) cBussi++;
      if(candid.equals("Ferri")) cFerri++;
      //response.sendRedirect("/Elezioni/index.html");
  } //END ELSE
.
.
.
java tomcat servlets
1个回答
0
投票

我建议使用链接并获取cookie

Cookie[] cookies = request.getCookies();

response.addCookie(cookie);

用于设置cookie,然后检查是否不是null,所以你知道选民是否投票。如果not null和getName设置为votedName,则可以将用户重定向。

但是要知道这些cookie可以被大多数浏览器修改或清除,或者当浏览器不支持cookie时可能会出现问题。

这只是一个粗略的答案,所以随时编辑

Cookie[] cookies = request.getCookies();
Boolean notVoted = true; //flag to see if user voted
if (cookies != null) { //check if cookie exist
    for (Cookie cookie : cookies) {
        String c = cookie.getName();
        if(c.equals("Ghezzi") || c.equals("Pelliccia") || c.equals("Valente") || c.equals("Bussi") || c.equals("Ferri")){
          response.setContentType("text/html");
          notVoted = false; //if user voted and cookie verifies then set flag
          PrintWriter out = response.getWriter();
          out.println("<html>");
          out.println("<head>");
          out.println("<title></title>");
          out.println("</head>");
          out.println("<body>");
          out.println("<h3>I'm sorry but you've already voted</h3>");
          out.println("</body>");
          out.println("</html>");
          out.close();
        }
    }
}
else if(notVoted){ //in case no cookies and user has not voted
    // do the voting stuff here
    String candid = request.getParameter("candidato");// set the cookie after user voted
    Cookie cookie = new Cookie(candid,candid);
    response.addCookie(cookie);
}

希望能帮助到你

© www.soinside.com 2019 - 2024. All rights reserved.