博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实战2--应用EL表达式显示投票结果
阅读量:6967 次
发布时间:2019-06-27

本文共 4117 字,大约阅读时间需要 13 分钟。

(1)编写index.jsp页面,用于收集投票信息

<%@ page language="java" pageEncoding="GBK"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>              应用EL表达式显示投票结果    
·您最需要哪方面的编程类图书?
基础教程类
实例集锦类
经验技巧类
速查手册类
案例剖析类
 

界面如下:

(2)编写投票功能的Servlet

package com.wuyudong.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.*;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class PollServlet extends HttpServlet {    private static final long serialVersionUID = -7264414153802032772L;    /**     * The doPost method of the servlet. 
* * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("GBK"); // 设置请求的编码方式 String item = request.getParameter("item"); // 获取投票项 ServletContext servletContext = request.getSession() .getServletContext(); // 获取ServletContext对象该对象在application范围内有效 Map map = null; if (servletContext.getAttribute("pollResult") != null) { map = (Map) servletContext.getAttribute("pollResult"); // 获取投票结果 map.put(item, Integer.parseInt(map.get(item).toString()) + 1); // 将当前的投票项加1 } else { // 初始化一个保存投票信息的Map集合,并将选定投票项的投票数设置为1,其他为0 String[] arr = { "基础教程类", "实例集锦类", "经验技巧类", "速查手册类", "案例剖析类" }; map = new HashMap(); for (int i = 0; i < arr.length; i++) { if (item.equals(arr[i])) { // 判断是否为选定的投票项 map.put(arr[i], 1); } else { map.put(arr[i], 0); } } } servletContext.setAttribute("pollResult", map); // 保存投票结果到ServletContext对象中 response.setCharacterEncoding("GBK"); // 设置响应的编码方式,如果不设置弹出的对话框中的文字将乱码 PrintWriter out = response.getWriter(); out.println(""); }}

(3)编写showResult.jsp页面

<%@ page language="java" pageEncoding="GBK"%>      显示示投票结果页面    
·您最需要哪方面的编程类图书?
基础教程类   (${empty applicationScope.pollResult["基础教程类"]? 0 :applicationScope.pollResult["基础教程类"]})
实例集锦类   (${empty applicationScope.pollResult["实例集锦类"] ? 0 :applicationScope.pollResult["实例集锦类"]})
经验技巧类   (${empty applicationScope.pollResult["经验技巧类"] ? 0 :applicationScope.pollResult["经验技巧类"]})
速查手册类   (${empty applicationScope.pollResult["速查手册类"] ? 0 : applicationScope.pollResult["速查手册类"]})
案例剖析类   (${empty applicationScope.pollResult["案例剖析类"] ? 0 :applicationScope.pollResult["案例剖析类"]})
合计:${applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]}人投票!
l>

最后运行界面如下:

转载地址:http://siisl.baihongyu.com/

你可能感兴趣的文章
单表60亿记录等大数据场景的MySQL优化和运维之道
查看>>
Linux zip解压/压缩并指定目录
查看>>
Ubuntu下安装MySQL 5.6.23
查看>>
Codeforces Round #261 (Div. 2)——Pashmak and Buses
查看>>
kafka源码分析之一server启动分析
查看>>
【CodeForces 626C】Block Towers
查看>>
SQL Server代理(11/12):维护计划作业
查看>>
提高批量插入数据的方法
查看>>
ios开发第三方库--cocoapods安装
查看>>
Mac OS X上搭建Apache、PHP、MySQL的Web服务器
查看>>
FZU 1686 神龙的难题 DLX反复覆盖
查看>>
《ArcGIS Engine+C#实例开发教程》第五讲 鹰眼的实现
查看>>
LVS+Keepalived实现高可用负载均衡(转)
查看>>
LINQ使用Lambda表达式选择几列
查看>>
12款很棒的浏览器兼容性测试工具推荐
查看>>
如果读取图像的图像出现坏图,无法读取的怎么办?怎么自动跳过坏的图像,而读取下一张?...
查看>>
RegisterDllAndOcx.bat -批量注册当前文件夹中的dll和ocx
查看>>
Python学习笔记(3):数据集操作-列的统一操作
查看>>
在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView
查看>>
SqlSugar-执行Sql语句查询实例
查看>>