JSP和Servlet(测试计数器)

ps:application是在整个服务器中起作用的,所以只要服务器不中断,数据还在保留。
commit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>My JSP 'conmit.jsp' starting page</title>
  </head>
  <body>
    <form action="commitServlet">
        一、你觉得JavaWeb的课程好不好?<br>
        <input type = "radio" name = "java" value = "hao">A、好<br>
        <input type = "radio" name = "java" value = "buhao">B、不好<br>
        <input type = "radio" name = "java" value = "haihaola">C、还好啦<br>
        二、你的爱好是?<br>
        <input type = "checkbox" name = "hobby" value = "basketball">A、basketball<br>
        <input type = "checkbox" name = "hobby" value = "football">B、football<br>
        <input type = "checkbox" name = "hobby" value = "volleyball">B、volleyball<br>
        <input type = "submit" value = "提交">
    </form>
  </body>
</html>

commitServlet.java

package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class commitServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String javaWeb = request.getParameter("java");
        String[] hobby = request.getParameterValues("hobby");

        request.setAttribute("javaWeb", javaWeb);
        request.setAttribute("hobby", hobby);

        request.getRequestDispatcher("commit_Result.jsp").forward(request,
                response);

    }

}

commit_Result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'commit_Result.jsp' starting page</title>
</head>
<body>
    <%
        String javaWeb = (String) request.getAttribute("javaWeb");
        String[] hobby = (String[]) request.getAttribute("hobby");
    %>
    <%
        /*  HashMap<String , Integer> map = new HashMap<String, Integer>(); 
        map.put("hao", 0);
        map.put("buhao", 0);
        map.put("haihaola", 0);

        if("hao".equals(javaWeb)){
            int i = 0;
            i ++;
            map.put("hao", i);


        }else if("buhao".equals(javaWeb)){
            int i;
            map.put("buhao", i++);

        }else if("haihaola".equals(javaWeb)){
            int i = 0;
            i ++;
            map.put("haihaola", i);

        }
         */
         /* 如果获得到的值不存在并且获得到的值与条件的相同
            则用application设置setattribute为“1”;
         */
        if (application.getAttribute("hao") == null && "hao".equals(javaWeb)) {
            application.setAttribute("hao", "1");

            /* 如果不为空就累加 */
        } else if ("hao".equals(javaWeb)) {

            String java = (String) application.getAttribute("hao");
            int num = Integer.parseInt(java);
            num++;
            String Str = String.valueOf(num);
            application.setAttribute("hao", Str);
        }

        if (application.getAttribute("buhao") == null && "buhao".equals(javaWeb)) {
            application.setAttribute("buhao", "1");

        } else if ("buhao".equals(javaWeb)) {

            String java = (String) application.getAttribute("buhao");
            int num = Integer.parseInt(java);
            num++;
            String Str = String.valueOf(num);
            application.setAttribute("buhao", Str);
        }

        if (application.getAttribute("haihaola") == null && "haihaola".equals(javaWeb)) {
            application.setAttribute("haihaola", "1");

        } else if ("haihaola".equals(javaWeb)) {

            String java = (String) application.getAttribute("haihaola");
            int num = Integer.parseInt(java);
            num++;
            String Str = String.valueOf(num);
            application.setAttribute("haihaola", Str);
        }


        /* 循环数组所有的值 */
        for(int i = 0;i<hobby.length;i++){

            if (application.getAttribute("basketball") == null && "basketball".equals(hobby[i])) {
                application.setAttribute("basketball", "1");

            } else if ("basketball".equals(hobby[i])) {

                String java = (String) application.getAttribute("basketball");
                int num = Integer.parseInt(java);
                num++;
                String Str = String.valueOf(num);
                application.setAttribute("basketball", Str);
            }


        }

        for(int i = 0;i<hobby.length;i++){

            if (application.getAttribute("football") == null && "football".equals(hobby[i])) {
                application.setAttribute("football", "1");

            } else if ("football".equals(hobby[i])) {

                String java = (String) application.getAttribute("football");
                int num = Integer.parseInt(java);
                num++;
                String Str = String.valueOf(num);
                application.setAttribute("football", Str);
            }


        }

        for(int i = 0;i<hobby.length;i++){

            if (application.getAttribute("volleyball") == null && "volleyball".equals(hobby[i])) {
                application.setAttribute("volleyball", "1");

            } else if ("volleyball".equals(hobby[i])) {

                String java = (String) application.getAttribute("volleyball");
                int num = Integer.parseInt(java);
                num++;
                String Str = String.valueOf(num);
                application.setAttribute("volleyball", Str);
            }


        }
    %>
    <a href="result.jsp">查看测试总结果</a>
</body>
</html>

result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'result.jsp' starting page</title>
</head>
<body>
    第一题测试结果为:
    <br> “好”---<%=application.getAttribute("hao")%><br> “不好”---<%=application.getAttribute("buhao")%><br> “还好啦”---<%=application.getAttribute("haihaola")%><br> 第二题测试结果为:
    <br> “篮球”---<%=application.getAttribute("basketball")%><br> “足球”---<%=application.getAttribute("football")%><br> "排球"---<%=application.getAttribute("volleyball")%><br>
<a href="conmit.jsp">再次做题</a>
</body>
</html>

效果图:
commit.jsp
commit.jsp
commit_Result.jsp
这里写图片描述
result.jsp
这里写图片描述

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GHQLYJ/article/details/76573087
文章标签: servlet jsp
个人分类: java-web
上一篇Java穷举法例子
下一篇JSP中用JavaScript简单客户端验证
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭