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
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值