please give me the code for opinion poll servlet

Hi i want to add some opinion poll application to my web site.
i.e in my index page i give one question and 4 choices, when the customer clicks vote button then the increment added to particular choice.

The structure of the application(I am using question,chice1 to 4 as images)

question?
choice1(radio button)
choice 2(r b)
choice 3(r b)
choice 4(r b)

  vote(button)

If the the user checks choice and say vote, then the percentage of choice 1 will be increased.
Imp Note: If the same user clicks with in 5 hour he got the message like sorry u r already voted.
In the code snippet i mentioned the code how can i do it, but i can't get exact results please send the code for this.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class OpenionServ extends HttpServlet
{
	int one_val;
	int two_val;
	int thri_val;
	int four_val;
	    
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		PrintWriter pw=response.getWriter();
		String res=request.getParameter("one");
		Cookie ck[]=request.getCookies();
		
		if(ck!=null)
		{
		  pw.println("<html><body leftmargin=0 topmargin=0><table background=images/tab_2_images/dd/banner.jpg border=0 width=1030 height=90><tr><td align=left><p><b><font style=font-family:Algerian;font-size:30px;>A2Z<img src=images/tab_2_images/dd/logo.gif align=middle>DETAILS.COM</font></b></p></td></tr> </table>");
		  pw.println("<br><br><br><BR><BR><BR>");
	 	  pw.println("<h3><center><font color=red>Sorry U already voted</font></center></h3>");
		  one_val=one_val+0;
		  two_val=two_val+0;
		  thri_val=thri_val+0;
		  four_val=four_val+0;
 
		  pw.println("<br><br>");
		  pw.println("<center>");
		  pw.println("<table><tr><td><b><font color=blue><u>Results</u>  :</font></b></td><tr><td><BR></td></tr></tr><tr><td><b>Mahesh Babu</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+one_val+"</td></tr><tr><td><b>Pavan Kalyan</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+two_val+"</td></tr><tr><td><b>Junior NTR</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+thri_val+"</td></tr><tr><td><b>Ram Charan Tej</b>&nbsp;&nbsp;&nbsp;"+four_val+"</td></tr></table>");
          pw.println("</center></body></html>");
 
		}
		else
		{
		  pw.println("<html><body leftmargin=0 topmargin=0><table background=images/tab_2_images/dd/banner.jpg border=0 width=1030 height=90><tr><td align=left><p><b><font style=font-family:Algerian;font-size:30px;>A2Z<img src=images/tab_2_images/dd/logo.gif align=middle>DETAILS.COM</font></b></p></td></tr> </table>");
          pw.println("<br><br><br><BR><BR><BR>");
		  pw.println("<h3><center><font color=green>Thanks For Voting...</font></center></h3>");
 
		  if(res.equals("one"))
		    {
               one_val=one_val+1;
		    }
	       if(res.equals("two"))
		    {
		      two_val=two_val+1;
		    }
		   if(res.equals("thri"))
		    {
		      thri_val=thri_val+1;
		    }
		    if(res.equals("four"))
		    {
		      four_val=four_val+1;
		    }
		   
		  
	       pw.println("<br><br>");
		   pw.println("<center>");
		   pw.println("<table><tr><td><b><font color=blue><u>Results</u>  :</font></b></td><tr><td><BR></td></tr></tr><tr><td><b>Mahesh Babu</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+one_val+"</td></tr><tr><td><b>Pavan Kalyan</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+two_val+"</td></tr><tr><td><b>Junior NTR</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+thri_val+"</td></tr><tr><td><b>Ram Charan Tej</b>&nbsp;&nbsp;&nbsp;"+four_val+"</td></tr></table>");
           pw.println("</center>");
		   pw.println("</body></html>");
	       
		   Cookie ck1=new Cookie("cc",res);
		   ck1.setMaxAge(43200);
		   response.addCookie(ck1);
		}
 
		
	}
}

Open in new window

haneefnbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gibu GeorgeChief Technology OfficerCommented:
First of all you need to show the options with radio button for voting. You can use a simple HTML page to do this or you can use the same servlet's doGet method to show this. Also the option butttons and the submit button should be inside a form and the forms taget should be your servlet, and the method should be POST. Add these to your existing code and then it should work fine.
0
Murali MurugesanFull stack Java developerCommented:
what do you mean by not getting the desired result?.. which is not working as expected?
Did ur code write the cookies or do u have problem in reading it?.

Moreover what would happen if the user deletes the cookie and try to vote in your poll ?

-Murali*
0
haneefnbAuthor Commented:
yes murali,u r wright
If  the user deletes the cookie, then it allows him to vote,
and more over i am facing another problem i.e when i am clicking vote ,it always gives the result like sorry u already voted, even the cookie expiry time reached.
0
Murali MurugesanFull stack Java developerCommented:
yes it works as per your code. You just take the cookies and check for != null and it will be true most of the time bcoz user might access other sites which might create cookies. Instead you read all the cookies and check for ur particular cookie that you created.

Try this change,

-Murali*
String cookieName = "cc";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
{
for (int i = 0; i < cookies.length; i++) 
{
if (cookies [i].getName().equals (cookieName))
{
myCookie = cookies[i];
break;
}
}
}
 
if (myCookie != null) {
        alert('You already voted');
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
haneefnbAuthor Commented:
Thank u very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.