用jsp和servlet实现投票页面

 调查问卷:投票系统

        功能有投票和查看投票结果

数据库字段

字段名称

字段说明

数据类型

大小

id

选项编号

int/主键/自增

item

选项名称

varchar

50

times

投票次数

int

实现代码

//util 工具类
public class DBUtil {
    public static Connection getConn(){
        Connection conn=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/servlet01?useUnicode=true&characterEncoding=utf8&userSSL=false&serverTimezone=Asia/Shanghai", "root", "mysql");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    
    
    public  static void closeAll(Connection conn, Statement st, ResultSet rs){
        try {
            if (rs!=null){
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        }catch (Exception e){
           
            
            e.printStackTrace();
        }
    }
}
//entity 实体类
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Vote implements Serializable {
    private int id;
    private String item;
    private int times;
    
    @Override
    public String toString() {
        return "Vote{" +
                "id=" + id +
                ", item='" + item + '\'' +
                ", times=" + times +
                '}';
    }
}
//dao 接口
public interface VoteDao {
    boolean voteAdd(String item,int times);
    
    int voteCheck(String item);
    
    List<Vote> voteCheckAll();
}

 

//DaoImpl 实现类
public class VoteDaoImpl extends DBUtil implements VoteDao {
    Connection conn = getConn();
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    public boolean voteAdd(String item, int times) {
        boolean boo = false;
        String sql = "update vote set times=? where item=?";
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, times);
            pstmt.setString(2, item);
            int x = pstmt.executeUpdate();
            if (x > 0) {
                boo = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
        return boo;
    }
    
    
    public int voteCheck(String item) {
        String sql = "select times from vote where item=?";
        int x=0;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, item);
             rs = pstmt.executeQuery();
             while (rs.next()){
                 x= rs.getInt("times");
             }
             
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
        return x;
        
    }
    
    public List<Vote> voteCheckAll() {
        List<Vote> voteList=new ArrayList<Vote>();
        String sql="select * from vote";
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Vote vote=new Vote();
                vote.setId(rs.getInt("id"));
                vote.setItem(rs.getString("item"));
                vote.setTimes(rs.getInt("times"));
                voteList.add(vote);
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
        
        return voteList;
    }
}
//servlet 
@WebServlet("/voteServlet")
public class VoteServlet extends HttpServlet {
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String item=req.getParameter("items");
        
        System.out.println(item);
        
        VoteDao vd=new VoteDaoImpl();
        int times = vd.voteCheck(item);
        
        boolean boo = vd.voteAdd(item, times + 1);
        System.out.println(item);
        System.out.println(times);
        System.out.println(boo);
        if (boo){
            PrintWriter out=resp.getWriter();
            resp.setContentType("text/html;charset=UTF-8");
            out.println("<script>alert('提交成功');location.href='results.jsp'</script>");
        }
        
        List<Vote> voteList=vd.voteCheckAll();
        
        req.setAttribute("voteList",voteList);
        req.getRequestDispatcher("results.jsp").forward(req, resp);
        
        
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}


 

// VoteMain.jsp
<%--
  Created by IntelliJ IDEA.
  User: 86156
  Date: 2024/7/26
  Time: 20:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/voteServlet" method="post">
    你对哪种技术最感兴趣:<br>
    <input type="radio" name="items" id="java" value="java">Java<br>
    <input type="radio" name="items" id=".net" value="net">.net<br>
    <input type="radio" name="items" id="Android" value="Android">Android<br>
    <input type="radio" name="items" id="PHP" value="PHP">PHP<br>
    <input type="submit" value="提交">
</form>

</body>

 

//results.jsp
<%--
  Created by IntelliJ IDEA.
  User: 86156
  Date: 2024/7/26
  Time: 21:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.util.List" %>
<%@ page import="com.by.entity.Vote" %>
<%@ page contentType="text/html;charset=UTF-8"  pageEncoding="UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>

        #box{
            width: 200px;
            height: 200px;
            background-color: rgb(255, 212, 85);
        }
        td{
            height: 20px;
        }
        td div{
            height: 20px;
            background-color: red;

        }

    </style>
</head>

<body>


<div id="box">
    <% List<Vote> list=(List<Vote>) request.getAttribute("voteList"); %>
    <table>
        <tr>
            <th>
                <span>调查结果</span>
            </th>
        </tr>
        <tr>
            <td><%=list.get(0).getItem()%></td>
            <td ><div style="width: <%=list.get(0).getTimes()%>*2px"> <%=list.get(0).getTimes()%></div></td>
        </tr>
        <tr>
            <td><%=list.get(1).getItem()%></td>
            <td> <div style="width: <%=list.get(1).getTimes()%>px"> <%=list.get(1).getTimes()%></div></td>
        </tr>
        <tr>
            <td><%=list.get(2).getItem()%></td>
            <td><div style="width: <%=list.get(2).getTimes()%>px"> <%=list.get(2).getTimes()%> </div></td>
        </tr>
        <tr>
            <td><%=list.get(3).getItem()%></td>
            <td ><div style="width: <%=list.get(3).getTimes()%>px"> <%=list.get(3).getTimes()%></div> </td>
        </tr>
    </table>

    

</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值