Link to home
Start Free TrialLog in
Avatar of haneefnb
haneefnb

asked on

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

Avatar of Gibu George
Gibu George
Flag of India image

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.
Avatar of Murali Murugesan
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*
Avatar of haneefnb
haneefnb

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Murali Murugesan
Murali Murugesan
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u very much