目录
part2实现社团与社团活动的创建、查找、删除、统计功能
今天的部分完成社团管理系当中的社团以及社团活动查找、删除、概览统计功能,在社团与社团活动之间实现关联关系。
一、创建社团与社团活动两张表
二、今天要实现的界面大致浏览
三、代码部分(重点部分讲解)
1.登录权限设置(未登录则无法使用管理员功能)
使用session来实现管理权限,在管理员登录页面提交表单后,跳转到AdminLogServlet.java.
将ok(或者自己随便什么自定义的符号代表)设置给creaacmsg(自定义设置)如下面的request.getSession().setAttribute("creaacmsg","ok");
AdminLogServlet.java代码
package com.admin;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AdminLogServlet")
public class AdminLogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String adminUsername=request.getParameter("adminUsername");
String adminPassword=request.getParameter("adminPassword");
String vcode1=request.getParameter("veri");
String s_v = (String) request.getSession().getAttribute("v");
if(!vcode1.equals(s_v)) {
request.getSession().setAttribute("adminLogMsg","请输入正确的二维码");
request.getRequestDispatcher("AdminLogin.jsp").forward(request,response);
}
else {
try {
int t=AdminService.adminLogin(adminUsername, adminPassword);
if(t==404) {
request.getSession().setAttribute("adminLogMsg","用户名或密码不正确.");
request.getRequestDispatcher("AdminLogin.jsp").forward(request,response);
}
else {
request.getSession().setAttribute("adminChangerMsg","ok");
request.getSession().setAttribute("adminUsername", adminUsername);
request.getSession().setAttribute("creaclubmsg","ok");
request.getSession().setAttribute("creaacmsg","ok");
request.getSession().setAttribute("deleclubmsg","ok");
request.getRequestDispatcher("AdminLogsucc.jsp").forward(request,response);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.print("no");
}
}
}
}
然后再点击创建社团的页面CreatClubActivity.jsp,获取到session传来的“creaacmsg”,判断是否正确或为空,正确则可以使用权限
CreatClubActivity.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="bootstrap-4.6.1-dist/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body>
<script src="bootstrap-4.6.1-dist/jquery-3.6.0.min.js"></script>
<script src="bootstrap-4.6.1-dist/js/bootstrap.bundle.min.js"></script>
</body>
<div class="container">
<%@include file="nav.jsp" %>
<div class="row" style="margin-top:10px">
<%@include file="AdminLeftcol.jsp" %>
<div class="col-7">
<div class="card">
<div class="card-body">
<%
String creaacmsg =(String) session.getAttribute("creaacmsg");
if (creaacmsg==null) { %>
<div style="color:red;text-align:left"><%="请先登录"%></div>
<% session.removeAttribute("creaacmsg");
} else { %>
<form action="/CommunityManager/CreateActivityServlet" method="post">
<div class="form-group row">
<label for="InputLeader">活动负责人</label>
<div class="col-sm-10">
<input type="text" name="acLeader" class="form-control" id="InputLeaderid">
</div>
</div>
<div class="form-group row">
<label for="InputHClub">举办社团</label>
<div class="col-sm-10">
<input type="text" name="holdClub" class="form-control" id="InputHoldClub">
</div>
</div>
<div class="form-group row">
<label for="InputHoldContents">活动内容</label>
<div class="col-sm-10">
<input type="text" name="holdContents" class="form-control" id="InputholdContents">
</div>
</div>
<div class="form-group row">
<label for="InputholdTime">活动时间</label>
<div class="col-sm-10">
<input type="date" name="holdTime" class="form-control" id="InputholdTime">
</div>
</div>
<div class="form-group row">
<label for="InputholdPlace">举办地点</label>
<div class="col-sm-10">
<input type="text" name="holdPlace" class="form-control" id="InputholdPlace">
</div>
</div>
<div class="form-group row">
<label for="Inputveri">校验码</label>
<div class="col-sm-3">
<img id="vericode" src="Vericode" onclick="this.src=this.src+'?'+Math.random()" width="80" height="30">
</div>
<div class="col-sm-5">
<input type="text" name="veri" class="form-control" id="InputPasswordid2">
</div>
<div class="col-sm-5">
<button id="btn" onclick="document.getElementById('vericode').click()" >刷新验证码</button>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">发布</button>
</div>
</div>
</form>
<%} %>
</div>
</div>
</div>
</div>
</div>
</html>
2.删除功能的实现
权限功能和上面一致,删除功能我这里 是直接加了一个按钮。在点击进入删除界面的时候,我们先在servlet里面获取到所有的社团活动(servlet里可以通过service方法中获取到社团活动值,这样比较简洁),获取到后再跳转到删除页面,按钮通过链接实现功能,根据id号操作删除。
<td><a class="btn btn-outline-danger" href="http://localhost:1090/CommunityManager/ClubDeleteServlet?id=${clublist.id}" role="button">删除</a></td>
点击删除社团活动先进入到ClubDeleteListServlet.java获取所有活动的值,然后跳转到删除页面
package com.club;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.activity.club_activity;
@WebServlet("/ClubDeleteListServlet")
public class ClubDeleteListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<club_activity> clublist=ClubService.getActivityList();
request.setAttribute("clublist", clublist);
request.getRequestDispatcher("ClubDeleteList.jsp").forward(request, response);
}
}
这就是跳转后的界面ClubDeleteList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet"
href="bootstrap-4.6.1-dist/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body>
</body>
<div class="container">
<%@include file="nav.jsp"%>
<div class="row" style="margin-top: 10px">
<%@include file="AdminLeftcol.jsp" %>
<div class="col-10">
<%
String deleclubmsg =(String) session.getAttribute("deleclubmsg");
if (deleclubmsg==null) { %>
<div style="color:red;text-align:left"><%="请先登录"%></div>
<% session.removeAttribute("deleclubmsg");
} else { %>
<table class="table">
<thead>
<tr>
<th scope="col" width="10%">活动编码</th>
<th scope="col" width="20%">活动举办人</th>
<th scope="col" width="20%">活动举办社团</th>
<th scope="col" width="20%">活动内容</th>
<th scope="col" width="20%">活动时间</th>
<th scope="col" width="10%">活动地点</th>
</tr>
</thead>
<tbody>
<c:forEach items="${clublist}" var="clublist">
<tr>
<th scope="row">${clublist.id}</th>
<td>${clublist.activity_leader}</td>
<td>${clublist.holding_club}</td>
<td>${clublist.holding_contents}</td>
<td>${clublist.holding_time}</td>
<td>${clublist.holding_place}</td>
<td><a class="btn btn-outline-danger" href="http://localhost:1090/CommunityManager/ClubDeleteServlet?id=${clublist.id}" role="button">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<%} %>
</div>
</div>
</div></html>
跳转到链接里的servlet里进行删除操作后回到删除界面,实现删除操作,如下为链接中的servlet,调用的方法里实现删除功能。
package com.club;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.activity.club_activity;
@WebServlet("/ClubDeleteServlet")
public class ClubDeleteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idd=request.getParameter("id");
int id=Integer.parseInt(idd);
List<club_activity> clublist=ClubService.getClubDeleteList(id);
request.setAttribute("clublist", clublist);
request.getRequestDispatcher("ClubDeleteList.jsp").forward(request, response);
}
}
3.根据创建的时间查询社团
也是点击ClubQueryServlet.java获取到社团的值后跳转到ClubQueryInfo.jsp,在此获取到指定日期后,再跳转到ClubQueryInfoServlet.java获取到指定日期的社团后再传入到ClubQueryInfo.jsp界面。
ClubQueryServlet.java代码
package com.club;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/ClubQueryServlet")
public class ClubQueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
List<club> list=ClubService.getClubList();
request.setAttribute("list", list);
request.getRequestDispatcher("ClubQueryInfo.jsp").forward(request, response);
}
}
ClubQueryInfo.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet"
href="bootstrap-4.6.1-dist/css/bootstrap.min.css">
<title>Insert title here</title>
</head>
<body>
</body>
<div class="container">
<%@include file="nav.jsp"%>
<div class="row" style="margin-top: 10px">
<%@include file="ClubLeftcol.jsp" %>
<div class="col-12">
<form action="ClubQueryInfoServlet">
<input type="date" name="q_date">
<input type="submit" name="查询">
</form>
<table class="table">
<thead>
<tr>
<th scope="col" width="10%">社团编码</th>
<th scope="col" width="30%">社团名称</th>
<th scope="col" width="30%">创立人</th>
<th scope="col" width="30%">创始时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="clublist">
<tr>
<th scope="row">${clublist.id}</th>
<td>${clublist.name}</td>
<td>${clublist.founder}</td>
<td>${clublist.found_time}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</html>
ClubQueryInfoServlet.java代码
package com.club;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ClubQueryInfo
*/
@WebServlet("/ClubQueryInfoServlet")
public class ClubQueryInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String day=request.getParameter("q_date");
List<club> list=ClubService.getClubByDay(day);
request.setAttribute("list", list);
request.getRequestDispatcher("ClubQueryInfo.jsp").forward(request, response);
}
}
4.概览统计功能的实现
进入查询界面后的活动概览,我是通过一个链接进入的
<a href="http://localhost:1090/CommunityManager/ActivityTotalServlet" class="list-group-item list-group-item-action active" aria-current="true">活动概览</a>
然后链接到ActivityTotalServlet.java界面获取到对应社团所举办的活动个数,这里新建了一个activity_count类,便于呈现社团对应活动个数和写SQL语句,然后跳转到ActivityTotal.jsp,这个界面可以·对应社团的活动详情。链接里也同时是通过传值然后获取对应的界面。
<td><a href="ActivityDetailServlet?holding_club=${clublist.holding_club}">详细情况</a></td>
代码部分其实和上面差不多,就是在servlet操作和jsp展示,数据库操作通过对应类获取值。