JSP和Servlet(测试计数器)
来源:互联网 发布:郑和 知乎 编辑:程序博客网 时间:2024/06/05 20:33
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_Result.jsp
result.jsp
阅读全文
0 0
- JSP和Servlet(测试计数器)
- 测试jsp+servlet+javaBean
- Eclipse开发Servlet+jsp模式的简单计数器
- JSP 计数器
- JSP计数器
- JSP计数器
- jsp计数器
- Jsp+Servlet+Mysql测试案例
- jsp测试servlet的生命周期
- 计数器测试
- Java Servlet 和 JSP入门教程(1)
- Java Servlet 和 JSP入门教程(2)
- Java Servlet 和 JSP入门教程(3)
- Java Servlet 和 JSP入门教程(4)
- Java Servlet和JSP教程(1)
- Java Servlet和JSP教程(2)
- Java Servlet和JSP教程(3)
- Java Servlet和JSP教程(4)
- PAT甲级解题报告汇总
- CSS层叠样式表的层叠和特殊性
- Intellij IDEA搭建Spark开发环境并运行
- java实现kubernates API的访问调用
- 典型约瑟夫环问题的实现
- JSP和Servlet(测试计数器)
- Linux采用iptables端口转发的配置
- Coin Test
- 使用状态模式实现计算器
- mongo 安装
- SQL截非标准字符串:CHARINDEX函数调用方法
- 错误:找不到或无法加载主类
- 【R笔记】R机器学习(十四)——BP神经网络
- 光照模型