java
<%@ page pageEncoding="UTF-8" import="java.util.*,entity.*,dao.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<style>
body{
text-align:center;
}
th,td{
border:2px solid gray;
text-align:center;
padding:3px 10px;
}
table{
border-collapse:collapse;
margin:10px auto;
}
#form1{
padding:20px;
display:none;
position:fixed;
top:50%;
left:50%;
width:300px;
height:200px;
z-index:1000;
margin-left:-200px;
margin-top:-200px;
background: #88f;
}
</style>
</head>
<body>
<%
StudentDao studentDao=new StudentDao();
request.setCharacterEncoding("UTF-8");
String op=request.getParameter("op");
if(op!=null){
if("add".equals(op)){
String sno=request.getParameter("sno");
String sname=request.getParameter("sname");
String sex=request.getParameter("sex");
Student student=new Student(sno,sname,sex);
try{
studentDao.add(student);
}catch(Exception e){
e.printStackTrace();
}
}
else if("update".equals(op)){
String sno=request.getParameter("sno");
String sname=request.getParameter("sname");
String sex=request.getParameter("sex");
Student student=new Student(sno,sname,sex);
try{
studentDao.update(student);
}catch(Exception e){
e.printStackTrace();
}
}
else if("delete".equals(op)){
String sno=request.getParameter("sno");
try{
studentDao.delete(sno);
}catch(Exception e){
e.printStackTrace();
}
}
}
List<Student> studentList=studentDao.query();
pageContext.setAttribute("studentList",studentList);
%>
<h3><a href="javascript:add()">添加记录</a></h3>
<h2>学生信息</h2>
<table>
<tr><th>学号</th><th>姓名</th><th>性别</th><th>修改</th><th>删除</th></tr>
<c:forEach items="${studentList}" var="student" varStatus="st">
<tr>
<td>${student.sno}</td>
<td>${student.sname}</td>
<td>${student.sex}</td>
<td><a href="javascript:update(${student.id},'${student.sno}','${student.sname}','${student.sex}')">修改</a></td>
<td><a href="yemian.jsp?op=delete&id=${student.id}" onclick="return confirm('确实要删除吗?')">删除</a></td>
</tr>
</c:forEach>
</table>
<form method="post" id="form1">
学号<input type="text" name="sno" id="sno"/><br><br>
姓名<input type="text" name="sname" id="sname"/><br><br>
性别<input type="radio" name="sex" value="男" checked/>男<input type="radio" name="sex" value="女" />女<br><br>
<input type="hidden" name="op" value="" id="op"/>
<input type="hidden" name="id" id="id"/>
<input type="submit" value="" id="bt"/>
<input type="reset" value="重置"/>
<input type="button" value="关闭" onclick="document.getElementById('form1').style.display='none';"/>
</form>
<br>
<script>
function add(){
document.getElementById("op").value="add";
document.getElementById("bt").value="添加";
document.getElementById('form1').style.display='block';
}
function update(id,sno,sname,sex){
document.getElementById("op").value="update";
document.getElementById("bt").value="修改";
document.getElementById('form1').style.display='block';
}
</script>
</body>
</html>
java
<%@ page import="dao.CandidateDao,entity.*,java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<script>
function f(){
var cks=document.getElementsByName("ck");
len=cks.length;
sum=0;
for(i=0;i<len;i++){
if(cks[i].checked)
sum++;
}
if(sum==5){
document.getElementById("form1").submit();
}
else{
alert("请选择5个候选人");
}
}
</script>
<body>
<%
CandidateDao candidateDao=new CandidateDao();
String[] ids=request.getParameterValues("ck");
int[] ids1=null;
if(ids!=null){
ids1=new int[ids.length];
for(int i=0;i<ids.length;i++){
ids1[i]=Integer.parseInt(ids[i]);
}
candidateDao.vote(ids1);
}
%>
<%
List<Candidate> cList=candidateDao.query();
pageContext.setAttribute("cList",cList);
%>
<form method="post" id="form1">
<c:forEach items="${cList}" var="c">
<input type="checkbox" name="ck" value="${c.id}">
<img src="image/${c.photo}"><br>
${c.name}<br>
${c.intro}<br>
${c.num}<hr>
</c:forEach>
<input type="button" value="投票" onclick="f()"/>
</form>
</body>
</html>