实战2--应用EL表达式显示投票结果

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

<%@ page language="java" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>应用EL表达式显示投票结果</title>
    <link rel="stylesheet" type="text/css" href="CSS/style.css">

  </head>

  <body><form name="form1" method="post" action="PollServlet">
  <table width="403" height="230" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#666666">
    <tr>
      <td height="30" bgcolor="#EFEFEF">·您最需要哪方面的编程类图书?</td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"> <input name="item" type="radio" class="noborder" value="基础教程类" checked>
        基础教程类</td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"> <input name="item" type="radio" class="noborder" value="实例集锦类">
      实例集锦类 </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"> <input name="item" type="radio" class="noborder" value="经验技巧类">
      经验技巧类</td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"> <input name="item" type="radio" class="noborder" value="速查手册类">
        速查手册类</td>
    </tr>
    <tr>
      <td bgcolor="#FFFFFF"> <input name="item" type="radio" class="noborder" value="案例剖析类">
      案例剖析类</td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <input name="Submit" type="submit" class="btn_grey" value="投票">
&nbsp;
<input name="Submit2" type="button" class="btn_grey" value="查看投票结果" onClick="window.location.href='showResult.jsp'"></td>
    </tr>
  </table>
 </form>
  </body>
</html>

界面如下:

(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. <br>
     *
     * 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("<script>alert('投票成功!');window.location.href='showResult.jsp';</script>");

    }

}

(3)编写showResult.jsp页面

<%@ page language="java" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>显示示投票结果页面</title>
    <link rel="stylesheet" type="text/css" href="CSS/style.css">

  </head>

  <body>
  <table width="403" height="230" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#666666">
    <tr>
      <td height="30" colspan="2" bgcolor="#EFEFEF">·您最需要哪方面的编程类图书?</td>
    </tr>
    <tr>
      <td width="79" align="center" bgcolor="#FFFFFF">        基础教程类</td>
      <td width="321" bgcolor="#FFFFFF">&nbsp;<img src="bar.gif" width='${220*(applicationScope.pollResult["基础教程类"]/(applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]))}' height="13">
      (${empty applicationScope.pollResult["基础教程类"]? 0 :applicationScope.pollResult["基础教程类"]})</td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">      实例集锦类 </td>
      <td bgcolor="#FFFFFF">&nbsp;<img src="bar.gif" width='${220*(applicationScope.pollResult["实例集锦类"]/(applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]))}' height="13">
      (${empty applicationScope.pollResult["实例集锦类"] ? 0 :applicationScope.pollResult["实例集锦类"]})</td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">      经验技巧类</td>
      <td bgcolor="#FFFFFF">&nbsp;<img src="bar.gif" width='${220*(applicationScope.pollResult["经验技巧类"]/(applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]))}' height="13">
      (${empty applicationScope.pollResult["经验技巧类"] ? 0 :applicationScope.pollResult["经验技巧类"]})</td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">        速查手册类</td>
      <td bgcolor="#FFFFFF">&nbsp;<img src="bar.gif" width='${220*(applicationScope.pollResult["速查手册类"]/(applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]))}' height="13">
      (${empty applicationScope.pollResult["速查手册类"] ? 0 : applicationScope.pollResult["速查手册类"]})</td>
    </tr>
    <tr>
      <td align="center" bgcolor="#FFFFFF">      案例剖析类</td>
      <td bgcolor="#FFFFFF">&nbsp;<img src="bar.gif" width='${220*(applicationScope.pollResult["案例剖析类"]/(applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]))}' height="13">
      (${empty applicationScope.pollResult["案例剖析类"] ? 0 :applicationScope.pollResult["案例剖析类"]})</td>
    </tr>
    <tr>
      <td colspan="2" align="center" bgcolor="#FFFFFF">
        合计:${applicationScope.pollResult["基础教程类"]+applicationScope.pollResult["实例集锦类"]+applicationScope.pollResult["经验技巧类"]+applicationScope.pollResult["速查手册类"]+applicationScope.pollResult["案例剖析类"]}人投票!
        <input name="Button" type="button" class="btn_grey" value="返回" onClick="window.location.href='index.jsp'"></td>
    </tr>
  </table>
  </body>
</html>
l>

最后运行界面如下:

时间: 2024-08-31 19:11:20

实战2--应用EL表达式显示投票结果的相关文章

怎么用el表达式 显示对象中 对象 的属性。也就是嵌套属性

问题描述 怎么用el表达式 显示对象中 对象 的属性.也就是嵌套属性 小弟做的是一个简单的hibernate+jsp的小项目.实体类有两个 电影Film类 和电影类型 FilmType 类 Film 部分代码如下 public class Film { private int fid; private String fname; private String setor; private String diector; private int price; private FilmType ft

实战1--应用EL表达式访问JavaBean的属性

(1)编写index.jsp页面,用来收集用户的注册信息 <%@ page language="java" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServe

实战3--应用EL表达式判断用户登录信息

1.判断用户名是否为空,空则显示提示信息 (1)编写index.jsp页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w

mybatis sql语句,EL表达式

问题描述 mybatis sql语句,EL表达式 产品规格表prosize(id,name,cid) 产品类别表category(id,name) prosize.cid=category.id,在mybatis里如何对产品规格表进行增删改查,写SQL语句 之后在页面如何用EL表达式显示出产品类别表的属性name,主要是对产品规格表进行操作 谢谢啦! 解决方案 你是整个流程都不知道怎么做么,如果是这样你应该找一个小的demo,网上会有很多,对比的看一下,其实对于你问的问题,个人理解是很基础的,

el表达式-关于EL表达式的问题,在线等答案!!

问题描述 关于EL表达式的问题,在线等答案!! org.apache.jasper.JasperException: An exception occurred processing JSP page /shouFunction.jsp at line 164 161: 162: 163: 164: 165: 166: /c:if 167: ${funlist[i].funname } 这是tomcatc报的错误,那位大神告诉是那里写错了???拜谢!! 解决方案 ${funlist[i].fun

【JSP EL】EL表达式里日期按照格式显示

转:http://blog.csdn.net/kaishuaige/article/details/8505174 JSP页面用EL表达式 输出date格式        1.头上引入标签 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/

EL表达式详细解析

<%@ page isELIgnored="true"%> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言. JSTL 标签 库由标签库和 EL 表达式语言两个部分组成. EL 在 JSTL 1.0 规范中被引入,当时用来作为 Java 表达式来工作,而该表达式必须配合 JSTL 的标签库才能得到需要的结果. 说明:在 JSTL 1.1 规范中, JSP2.0 容器已经能够独立的理解任何 EL 表达式. EL 可以独立出现在

JSP EL表达式详细介绍

一.JSP EL语言定义 E L(Expression Language) 目的:为了使JSP写起来更加简单. 表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言,它提供了在 JSP 中简化表达式的方法.它是一种简单的语言,基于可用的命名空间(PageContext 属性).嵌套属性和对集合.操作符(算术型.关系型和逻辑型)的访问符.映射到 Java 类中静态方法的可扩展函数以及一组隐式对象. EL 提供了在 JSP 脚本编制元素范围外使用运行时表达式的功能.脚本编制元素是

Tomcat5发布项目问题(2):默认不解析EL表达式

Tomcat 5.5使EL表达式不被解析.  现象 代码${userSession.user_name}是JSP中的一个代码片段: 如果部署到tomcat5.5中,不会显示出session中的变量user用户名,而只会把 ${userSession.user_name}打印出来,猜测很可能是tomcat5.5的bug,不解析(或屏蔽了)EL表达式.  原因 如果web.xml中声明部分的schema版本为2.5或者以上,而tomcat使用的是5.5.x以下的版本的时候就会出现在页面直接显示而不解