我们如何从servlet打印计数器到jsp?

时间:2017-01-29 18:36:41

标签: java jsp servlets web

嘿我正在尝试创建一个投票应用程序。

我的servlet中有几个计数器,我希望能够在我的jsp文件中显示这些计数器的值。我该怎么做? 请帮我。 我想显示每个部分的总票数和选举后的获胜者。 .................................................. .................................................. .................................................. servlet文件 .................................................. .................................................. ..................................................

    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文件 .................................................. .................................................. ..................................................

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <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>

0 个答案:

没有答案