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