dimanche 29 janvier 2017

How do we print a counter from a servlet to a jsp?

Hey I am trying to create a voting app.

I have several counters in my servlets and I want to be able to display the value of those counters in my jsp file. How do I do it? Please help me. I want to display the total number of votes in each section and the winner after the election. ...................................................................................................................................................... servlet file ......................................................................................................................................................

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/CastVote")
    public class CastVote extends HttpServlet {
        int javaCount, phpCount, pythonCount, othersCount;

        public CastVote() {
            super();

            javaCount = 0;
            phpCount = 0;
            pythonCount = 0;
            othersCount = 0;  
            // TODO Auto-generated constructor stub
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String choice = request.getParameter("language");

            if("java".equals(choice))
            {
                javaCount++;
            }

            else if("php".equals(choice))
            {
                phpCount++;
            }

            else if("python".equals(choice))
            {
                pythonCount++;
            }

            else if("others".equals(choice))
            {
                othersCount++;
            }

        }

    }

...................................................................................................................................................... Jsp File ......................................................................................................................................................

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Vote now.</title>
    </head>
    <body>

        <center>
            <h2>Welcome to voting portal. Cast your vote.</h2>
        </center>
        <form action="CastVote" method="post">
            Which of the following is the best language for backend programming? <br>
            <input type="radio" name="language" value="java">1) Java <br>
            <input type="radio" name="language" value="php">2) PHP <br>
            <input type="radio" name="language" value="python">3)        Python <br>
            <input type="radio" name="language" value="others">4) Others <br>
            <input type = "submit" value ="Vote">
        </form>


    </body>
    </html>




Aucun commentaire:

Enregistrer un commentaire