免费预览已结束,剩余4页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JSP实训八(JSP+JavaBean+Servlet实现投票系统)一、实训目的:1、掌握MVC模式。2、掌握投票系统的前台和后台的实现。二、实训内容:(一)实验一:投票系统前台的实现1、 创建项目chapter9-1,编写login.jsp,用于用户投票。访问页面如图1所示:图1、login.jsp页面 2、把数据库驱动程序复制到lib下。3、 编写JavaBean:BallotBean.java,成员属性有:private Integer id;private String content;private Double number;private String title;4、在项目中添加处理汉字的过滤器SetCharacterEncodingFilter。5、创建数据库test表ballot: create table test.ballot( id INT not null auto_increment, content VARCHAR(50), number DOUBLE, title VARCHAR(50), primary key (id) );6、编写result.jsp页面,如图2所示:图2、result.jsp页面7、编写JdbcDao.java,用于数据库连接、访问。private Connection getConnection()Connection con = null;try Class.forName(com.mysql.jdbc.Driver); catch (ClassNotFoundException e1) / TODO Auto-generated catch blocke1.printStackTrace();String url = jdbc:mysql:/localhost/test;try con = DriverManager.getConnection(url,root,root); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return con;/添加投票public boolean updateBallot(Integer id ,Double number)boolean bool = false;Connection con = this.getConnection();trycon.setAutoCommit(false);Statement sm = con.createStatement();String sql = update ballot set number=+number+ where id=+id;sm.execute(sql);mit();sm.close();con.close();bool = true;catch(SQLException sql)sql.printStackTrace();catch(Exception e)e.printStackTrace();return bool;/查询一条信息public BallotBean queryOneBallot(Integer id)BallotBean bean = new BallotBean();Connection con = this.getConnection();tryStatement sm = con.createStatement();String sql = select * from ballot where id=+id;ResultSet result = sm.executeQuery(sql);while(result.next()bean.setId(result.getInt(id);bean.setContent(result.getString(content);bean.setNumber(result.getDouble(number);catch(SQLException sql)sql.printStackTrace();catch(Exception e)e.printStackTrace();return bean;public List queryAllBallot()List ballotList = new ArrayList();Connection con = this.getConnection();tryStatement sm = con.createStatement();String sql = select * from ballot;ResultSet result = sm.executeQuery(sql);while(result.next()BallotBean bean = new BallotBean();bean.setId(result.getInt(id);bean.setContent(result.getString(content);bean.setNumber(result.getDouble(number);bean.setTitle(result.getString(title);ballotList.add(bean);catch(SQLException sql)sql.printStackTrace();catch(Exception e)e.printStackTrace();return ballotList;8、编写servlet:disposal.java,用于处理用户提交信息。public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);/得到前台传来的值String id = request.getParameter(content);List perList = new ArrayList();JdbcDao jdbc = new JdbcDao();BallotBean bean = jdbc.queryOneBallot(new Integer(id);double num = bean.getNumber().doubleValue() + 1;boolean bool = jdbc.updateBallot(new Integer(id), num);if(bool)List ballotList = jdbc.queryBallot();/投票的总数量double sum = 0.0;for(int i=0;iballotList.size();i+)BallotBean baBean = (BallotBean)ballotList.get(i);sum = sum + baBean.getNumber();/计算投票的百分比for(int i=0;iballotList.size();i+)BallotBean baBean = (BallotBean)ballotList.get(i);double pe = (baBean.getNumber().doubleValue() / sum) * 100;long per = (long)(pe * 100 + 0.5);double percent = (double)per / 100;baBean.setNumber(percent);perList.add(baBean);request.setAttribute(perList, perList);request.getRequestDispatcher(/result.jsp).forward(request, response);9、在表ballot中输入3条记录:10、访问login.jsp,结果如图2所示。(二)实验二:投票系统后台的实现(对侯选人的增加和删除)1、编写add.jsp,用于添加候选人,页面如图3所示:图3、add.jsp页面 添加党员侯选人 输入侯选人姓名: 2、 编写query.jsp,用于查询候选人。页面如图4、如图5所示:图4、query.jsp页面 待选标题: 带选标题 侯选人 票数 操作 % for(int i=0;i a href=servlet/Delete?id=删除 3、编写servlet:Delete.java,用于删出候选人。public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);String id = (String)request.getParameter(id);JdbcDao jdbc = new JdbcDao();jdbc.deleteBallot(id);request.getRequestDispatcher(/query.jsp).forward(request, response);4、编写servlet:add.java,用于添加候选人。public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(text/html);String title = (String)request.getParameter(title);String content = (String)request.getParameter(content);JdbcDao jdbc = new JdbcDao();boolean bool = jdbc.addBallot(title, content);if(bool)request.setAttribute(message, 添加成功);request.getRequestDispatcher(/add.jsp).forward(request, response);5、编写servlet:Query.java,用于查询候选人。(见p224代码9-9)6、在数据库访问类JdbcDao.java中,增加删除和添加候选人的方法:public void deleteBallot(String id)Connection con = this.getConnection();tryStatement sm = con.createStatement();String sql = delete from ballot where id=+id;sm.execute(sql);/Querycatch(SQLException sql)sql.printStackTrace();catch(Exception e)e.printStackTrace();/向投票表中添加内容public boolean addBallot(String title ,String content ) boolean bool = false;Connection con = this.getConnection();try con.setAutoCommit(false);Statement sm = con.createStatement();String sql = insert into ballot(content,number,title) value(+content+,0.0,+titl
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026四川大学华西第二医院眉山市妇女儿童医院(眉山市妇幼保健院)公开招聘编外内科主治医师1人考试参考题库及答案解析
- 2026中移铁通有限公司博州分公司招聘笔试参考题库及答案解析
- 2026绥芬河市妇女联合会公开招聘公益性岗位工作人员1人笔试参考题库及答案解析
- 2026年四川省遂宁市事业单位人员招聘笔试试题与答案详解
- 塑料厂注塑成型规范
- 2026年阜新市太平区事业单位人员招聘笔试试题与答案详解
- 2026年齐齐哈尔市昂昂溪区公务员人员招聘考试备考题库与答案详解
- 2026年北海市银海区公务员人员招聘笔试备考题库与答案详解
- 2026重庆市专业应急救援璧山支队队员招聘5人笔试备考题库及答案解析
- 2026西安交通大学人文学院招聘副教授2名考试备考题库及答案解析
- 2026国家电网公司北京电力医院招聘考试模拟试题及答案解析
- 九师联盟2027届高三11月联考历史+答案
- 体外诊断试剂药品培训试题及答案(新版)
- 湖南雅礼中学2027届高三月考试卷三语文试卷
- 2026-2027学年科粤版九年级上册化学期中测试卷解析版
- 《95128出租汽车电召平台服务规范(征求意见稿)》编制说明
- 2026年选矿工程师试题及答案
- 中国区域国别学全套教学课件
- 超声造影剂安全使用专家共识(2026版)
- JJG 1189.1-2026 测量用互感器检定规程 第1部分:标准电流互感器
- 移动式操作平台(盘扣式)专项施工方案(品茗验算通过可套用)
评论
0/150
提交评论