JSP MVC模式案例之投票系统

举报
tea_year 发表于 2021/12/29 23:18:24 2021/12/29
【摘要】 create table voteitems( id number(8) primary key, item varchar2(50) not null, times number(8) default 0); create sequence seq_voteitems; insert into voteitems(id,item) v...

  
  1. create table voteitems(
  2. id number(8) primary key,
  3. item varchar2(50) not null,
  4. times number(8) default 0
  5. );
  6. create sequence seq_voteitems;
  7. insert into voteitems(id,item) values (seq_voteitems.nextval,'jsp');
  8. insert into voteitems(id,item) values (seq_voteitems.nextval,'asp');
  9. insert into voteitems(id,item) values (seq_voteitems.nextval,'asp.net');
  10. insert into voteitems(id,item) values (seq_voteitems.nextval,'php');
  11. commit;


  
  1. package com.javaweb.dao.util;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class BaseDao {
  8. private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
  9. private static String driver = "oracle.jdbc.driver.OracleDriver";
  10. private static String user="scott";
  11. private static String pwd = "tiger";
  12. public static Connection getConnection()
  13. throws ClassNotFoundException, SQLException{
  14. Class.forName(driver);
  15. Connection con = DriverManager.getConnection(url,user,pwd);
  16. return con;
  17. }
  18. public static void closeAll(ResultSet rs,Statement stmt,Connection con)
  19. throws SQLException{
  20. if(rs!=null)
  21. rs.close();
  22. if(stmt!=null)
  23. stmt.close();
  24. if(con!=null)
  25. con.close();
  26. }
  27. }

   
  1. package com.javaweb.dao;
  2. import java.util.List;
  3. import com.javaweb.entity.VoteItems;
  4. public interface VoteItemsDao {
  5. public List<VoteItems> findAll();
  6. public boolean update(int id);
  7. }



  
  1. package com.javaweb.dao.impl;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import com.javaweb.dao.VoteItemsDao;
  9. import com.javaweb.dao.util.BaseDao;
  10. import com.javaweb.entity.VoteItems;
  11. public class VoteItemsDaoImpl extends BaseDao implements VoteItemsDao {
  12. public static void main(String[] args) {
  13. System.out.println(new VoteItemsDaoImpl().findAll().size());
  14. }
  15. public List<VoteItems> findAll() {
  16. Connection con = null;
  17. PreparedStatement pstmt = null;
  18. ResultSet rs=null;
  19. List<VoteItems> items=null;
  20. String sql="select id,item,times from voteitems";
  21. try {
  22. con=getConnection();
  23. items=new ArrayList<VoteItems>();
  24. pstmt=con.prepareStatement(sql);
  25. rs=pstmt.executeQuery();
  26. while(rs.next()){
  27. VoteItems item=new VoteItems();
  28. item.setId(rs.getInt(1));
  29. item.setItem(rs.getString(2));
  30. item.setTimes(rs.getInt(3));
  31. items.add(item);
  32. }
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }finally{
  38. try {
  39. closeAll(rs, pstmt, con);
  40. } catch (SQLException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. return items;
  46. }
  47. public boolean update(int id) {
  48. String sql = "update voteitems set times = times + 1 where id = "+id;
  49. Connection con = null;
  50. PreparedStatement pstmt = null;
  51. int n = -1;
  52. try {
  53. con = getConnection();
  54. pstmt = con.prepareStatement(sql);
  55. n=pstmt.executeUpdate();
  56. } catch (ClassNotFoundException e) {
  57. // TODO Auto-generated catch block
  58. e.printStackTrace();
  59. } catch (SQLException e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }finally{
  63. try {
  64. closeAll(null, pstmt, con);
  65. } catch (SQLException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. }
  70. return n>0;
  71. }
  72. }

  
  1. package com.javaweb.servlet;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import com.javaweb.dao.VoteItemsDao;
  8. import com.javaweb.dao.impl.VoteItemsDaoImpl;
  9. public class UpdateVoteServlet extends HttpServlet {
  10. /**
  11. * The doGet method of the servlet. <br>
  12. *
  13. * This method is called when a form has its tag value method equals to get.
  14. *
  15. * @param request the request send by the client to the server
  16. * @param response the response send by the server to the client
  17. * @throws ServletException if an error occurred
  18. * @throws IOException if an error occurred
  19. */
  20. public void doGet(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22. doPost(request, response);
  23. }
  24. /**
  25. * The doPost method of the servlet. <br>
  26. *
  27. * This method is called when a form has its tag value method equals to post.
  28. *
  29. * @param request the request send by the client to the server
  30. * @param response the response send by the server to the client
  31. * @throws ServletException if an error occurred
  32. * @throws IOException if an error occurred
  33. */
  34. public void doPost(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. String[] strItems = request.getParameterValues("items");
  37. VoteItemsDao dao = new VoteItemsDaoImpl();
  38. for(String item:strItems){
  39. dao.update(Integer.parseInt(item));
  40. System.out.println(item);
  41. }
  42. //
  43. response.sendRedirect("ShowVoteServlet");
  44. //request.getRequestDispatcher("ShowVoteServlet").forward(request, response);
  45. }
  46. }



  
  1. package com.javaweb.servlet;
  2. import java.io.IOException;
  3. import java.util.List;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import com.javaweb.dao.VoteItemsDao;
  9. import com.javaweb.dao.impl.VoteItemsDaoImpl;
  10. import com.javaweb.entity.VoteItems;
  11. public class ShowVoteServlet extends HttpServlet {
  12. /**
  13. * The doGet method of the servlet. <br>
  14. *
  15. * This method is called when a form has its tag value method equals to get.
  16. *
  17. * @param request the request send by the client to the server
  18. * @param response the response send by the server to the client
  19. * @throws ServletException if an error occurred
  20. * @throws IOException if an error occurred
  21. */
  22. public void doGet(HttpServletRequest request, HttpServletResponse response)
  23. throws ServletException, IOException {
  24. doPost(request, response);
  25. }
  26. /**
  27. * The doPost method of the servlet. <br>
  28. *
  29. * This method is called when a form has its tag value method equals to post.
  30. *
  31. * @param request the request send by the client to the server
  32. * @param response the response send by the server to the client
  33. * @throws ServletException if an error occurred
  34. * @throws IOException if an error occurred
  35. */
  36. public void doPost(HttpServletRequest request, HttpServletResponse response)
  37. throws ServletException, IOException {
  38. VoteItemsDao dao = new VoteItemsDaoImpl();
  39. List<VoteItems> voteitems = dao.findAll();
  40. request.setAttribute("voteitems", voteitems);
  41. request.getRequestDispatcher("vote_result.jsp").
  42. forward(request, response);
  43. }
  44. }


  
  1. <%@ page language="java" contentType="text/html; charset=utf-8"%>
  2. <%@ page import="java.util.*,com.javaweb.entity.*,com.javaweb.dao.*,com.javaweb.dao.impl.*" %>
  3. <%
  4. VoteItemsDao voteDao = new VoteItemsDaoImpl();
  5. List<VoteItems> votes = voteDao.findAll();
  6. %>
  7. <html>
  8. <head>
  9. <title>调查问卷</title>
  10. <style>
  11. .outside{ /* 外层表格 */
  12. background:url(bg1.jpg);
  13. font-size:12px;
  14. padding:0px;
  15. }
  16. .title{ /* 表格标题 */
  17. color:#FFFFFF;
  18. font-weight:bold;
  19. text-align:center;
  20. padding-top:3px;
  21. padding-bottom:0px;
  22. }
  23. .tdoutside{
  24. padding:0px 1px 4px 1px;
  25. }
  26. .inside{ /* 内层表格 */
  27. width:269px;
  28. font-size:12px;
  29. padding:0px;
  30. margin:0px;
  31. }
  32. .tdinside{
  33. padding:7px 0px 7px 10px;
  34. background-color:#FFD455;
  35. }
  36. form{
  37. margin:0px; padding:0px;
  38. }
  39. input{
  40. font-size:12px;
  41. }
  42. a{
  43. color:#000000;
  44. }
  45. </style></head>
  46. <body>
  47. <table class="outside">
  48. <tr><td class="title">点调查</td></tr>
  49. <tr><td class="tdoutside">
  50. <form method="post" action="UpdateVoteServlet">
  51. <table class="inside" cellspacing="0">
  52. <tr>
  53. <td class="tdinside">
  54. 你对哪种技术最感兴趣<br>
  55. <%
  56. for(VoteItems v:votes){
  57. %>
  58. <input type="checkbox" name="items" value="<%=v.getId() %>"><%=v.getItem() %><br>
  59. <%
  60. }
  61. %>
  62. <input type="submit" value="提交">
  63. <a href="ShowResultServlet">查看结果</a>
  64. </td>
  65. </tr>
  66. </table>
  67. </form>
  68. </td></tr>
  69. </table>
  70. </body>
  71. </html>


  
  1. <%@ page language="java" import="java.util.*,com.javaweb.entity.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. List<VoteItems> voteitems =
  6. (List<VoteItems>)request.getAttribute("voteitems");
  7. %>
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12. <title>My JSP 'vote_result.jsp' starting page</title>
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="styles.css">
  20. -->
  21. </head>
  22. <body>
  23. <table>
  24. <%
  25. for(VoteItems v:voteitems){
  26. %>
  27. <tr>
  28. <td><%=v.getItem() %></td>
  29. <td><%=v.getTimes() %></td>
  30. </tr>
  31. <%} %>
  32. </table>
  33. </body>
  34. </html>


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/53138122

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。