1 概述
2 数据库表
表4.4 t_tiwen 提问人表
名称 |
类型 |
说明 |
id |
int(11) |
编号 |
Tbianhao |
longtext |
用户名 |
name1 |
longtext |
姓名 |
sex |
longtext |
性别 |
age |
longtext |
年龄 |
loginpw |
longtext |
密码 |
3 关键代码
package com.action;import java.io.IOException;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.dao.DB;
import com.orm.TAdmin;
import com.orm.Thuifu;
import com.orm.Tstu;
import com.orm.Ttea;
import com.orm.Twenti;
import com.service.liuService;public class wenti_servlet extends HttpServlet {public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String type = req.getParameter("type");if (type.endsWith("wenti_tijiao")) {wenti_tijiao(req, res);}if (type.endsWith("wenti_manage")) {wenti_manage(req, res);}if (type.endsWith("wenti_mime")) {wenti_mime(req, res);}if (type.endsWith("huifu_mime")) {huifu_mime(req, res);}if (type.endsWith("wenti_all")) {wenti_all(req, res);}if (type.endsWith("wenti_huifu")) {wenti_huifu(req, res);}if (type.endsWith("wenti_jieshu")) {wenti_jieshu(req, res);}if (type.endsWith("wenti_del")) {wenti_del(req, res);}if (type.endsWith("wenti_delqiantai")) {wenti_delqiantai(req, res);}if (type.endsWith("huifu_delqiantai")) {huifu_delqiantai(req, res);}if (type.endsWith("huifu_show")) {huifu_show(req, res);}}// 学生添加问题public void wenti_tijiao(HttpServletRequest req, HttpServletResponse res) {HttpSession session = req.getSession();Tstu stu = (Tstu) session.getAttribute("stu");String mingcheng = req.getParameter("mingcheng");String content = req.getParameter("content");String tag = req.getParameter("tag");String fujian = req.getParameter("fujian");String fujianYuanshiming = req.getParameter("fujianYuanshiming");String shijian_shangchuan = new SimpleDateFormat("yyyy-MM-dd").format(new Date());String huifuneirong = "";String shijian_huifu = "";int stu_id = stu.getId();String sql = "INSERT INTO `t_wenti` ( `mingcheng`, `content`, `tag`, `fujian`, `fujianYuanshiming`, `shijian_shangchuan`, `stu_id`, `status`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?); ";Object[] params = { mingcheng, content,tag,fujian, fujianYuanshiming, shijian_shangchuan, stu_id,0 };DB mydb = new DB();mydb.doPstm(sql, params);mydb.closed();req.setAttribute("msg", "问题提交完毕");String targetURL = "/common/msg.jsp";dispatch(targetURL, req, res);}// 前台教师回复列表public void huifu_mime(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {HttpSession session = req.getSession();Ttea tea = (Ttea) session.getAttribute("tea");List huifuList = new ArrayList();String sql = "select * from t_huifu where tea_id=?";Object[] params = { tea.getId() };DB mydb = new DB();try {mydb.doPstm(sql, params);ResultSet rs = mydb.getRs();while (rs.next()) {Thuifu huifu = new Thuifu();huifu.setId(rs.getInt("id"));huifu.setContent(rs.getString("content"));huifu.setShijian(rs.getString("shijian"));huifu.setTeaname(rs.getString("teaname"));huifu.setTea_id(rs.getInt("tea_id"));huifu.setTea(liuService.getTea(rs.getInt("tea_id")));// 加载问题内容String mingcheng = "";Twenti wenti = new Twenti();String sqlre = "select * from t_wenti where id=?";Object[] paramsre = { rs.getString("wenti_id") };DB mydbre = new DB();try {mydb.doPstm(sqlre, paramsre);ResultSet rsre = mydb.getRs();while (rsre.next()) {mingcheng = rsre.getString("mingcheng");}rsre.close();} catch (Exception e) {e.printStackTrace();}wenti.setMingcheng(mingcheng);huifu.setWenti(wenti);huifuList.add(huifu);}rs.close();} catch (Exception e) {e.printStackTrace();}mydb.closed();req.setAttribute("huifuList", huifuList);req.getRequestDispatcher("qiantai/wenti/huifu_mime.jsp").forward(req, res);}// 前台学生的问题列表public void wenti_mime(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {HttpSession session = req.getSession();Tstu stu = (Tstu) session.getAttribute("stu");List wentiList = new ArrayList();String sql = "select * from t_wenti where stu_id=?";Object[] params = { stu.getId() };DB mydb = new DB();try {mydb.doPstm(sql, params);ResultSet rs = mydb.getRs();while (rs.next()) {Twenti wenti = new Twenti();wenti.setId(rs.getInt("id"));wenti.setMingcheng(rs.getString("mingcheng"));wenti.setFujian(rs.getString("fujian"));wenti.setFujianYuanshiming(rs.getString("fujianYuanshiming"));wenti.setShijian_shangchuan(rs.getString("shijian_shangchuan"));wenti.setStu_id(rs.getInt("stu_id"));wenti.setStatus(rs.getInt("status"));wenti.setStu(liuService.getStu(rs.getInt("stu_id")));// 加载回复内容String huifuneirong = "无";String sqlre = "select * from t_huifu where wenti_id=?";Object[] paramsre = { rs.getString("id") };DB mydbre = new DB();try {mydb.doPstm(sqlre, paramsre);ResultSet rsre = mydb.getRs();while (rsre.next()) {huifuneirong = "有";}rsre.close();} catch (Exception e) {e.printStackTrace();}wenti.setHuifuneirong(huifuneirong);wentiList.add(wenti);}rs.close();} catch (Exception e) {e.printStackTrace();}mydb.closed();req.setAttribute("wentiList", wentiList);req.getRequestDispatcher("qiantai/wenti/wenti_mime.jsp").forward(req, res);}// 所有的问题列表public void wenti_all(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String page1 = req.getParameter("page");if (page1 == null) {page1 = "1";}// 分页设置int EVERYPAGENUM = 20;// 每页条数int page = Integer.parseInt(page1); // 传递过来的当前页int cou = 1;// 得到信息总数int pagecount = 1; // 总页数String sql1 = "select count(*) as cou from t_wenti where 1=1 ";if (req.getParameter("mingcheng") != null)sql1 += " AND mingcheng like '%" + req.getParameter("mingcheng").trim() + "%'";if (req.getParameter("tag") != null) {String strPtname = new String(req.getParameter("tag").getBytes("ISO-8859-1"), "UTF-8");sql1 += " AND FIND_IN_SET('" + strPtname + "',tag) ";}Object[] params1 = {};DB mydb1 = new DB();try {mydb1.doPstm(sql1, params1);ResultSet rs = mydb1.getRs();while (rs.next()) {cou = rs.getInt("cou");}rs.close();} catch (Exception e) {e.printStackTrace();}mydb1.closed();if (cou % EVERYPAGENUM == 0) {pagecount = cou / EVERYPAGENUM;} else {pagecount = cou / EVERYPAGENUM + 1;}req.setAttribute("EVERYPAGENUM", EVERYPAGENUM);req.setAttribute("page", page);req.setAttribute("cou", cou);req.setAttribute("pagecount", pagecount);List wentiList = new ArrayList();String sql = "select * from t_wenti where 1=1 ";if (req.getParameter("mingcheng") != null)sql += " AND mingcheng like '%" + req.getParameter("mingcheng").trim() + "%'";if (req.getParameter("tag") != null) {String strPtname = new String(req.getParameter("tag").getBytes("ISO-8859-1"), "UTF-8");sql += " AND FIND_IN_SET('" + strPtname + "',tag) ";}sql += " order by id desc";Object[] params = {};DB mydb = new DB();try {mydb.doPstm(sql, params);ResultSet rs = mydb.getRs();for (int i = 0; i < (page - 1) * EVERYPAGENUM; i++) {rs.next();}for (int t = 0; t < EVERYPAGENUM; t++) {if (rs.next()) {Twenti wenti = new Twenti();wenti.setId(rs.getInt("id"));wenti.setMingcheng(rs.getString("mingcheng"));wenti.setFujian(rs.getString("fujian"));wenti.setFujianYuanshiming(rs.getString("fujianYuanshiming"));wenti.setShijian_shangchuan(rs.getString("shijian_shangchuan"));wenti.setStu_id(rs.getInt("stu_id"));// 加载回复内容String huifuneirong = "无";String sqlre = "select * from t_huifu where wenti_id=?";Object[] paramsre = { rs.getString("id") };DB mydbre = new DB();try {mydb.doPstm(sqlre, paramsre);ResultSet rsre = mydb.getRs();while (rsre.next()) {huifuneirong = "有";}rsre.close();} catch (Exception e) {e.printStackTrace();}wenti.setHuifuneirong(huifuneirong);wenti.setStu(liuService.getStu(rs.getInt("stu_id")));wentiList.add(wenti);} else {break; // 减少空循环的时间}}rs.close();} catch (Exception e) {e.printStackTrace();}mydb.closed();// 获取tagString tags = "";String sql12 = "select * from t_wenti ";Object[] params12 = {};DB mydb12 = new DB();try {mydb12.doPstm(sql12, params12);ResultSet rs12 = mydb12.getRs();while (rs12.next()) {tags += rs12.getString("tag") + ",";}rs12.close();} catch (Exception e) {e.printStackTrace();}mydb12.closed();String[] tags_array = tags.split(",");// 数组去重Set<String> set = new HashSet<>();for (int i = 0; i < tags_array.length; i++) {set.add(tags_array[i]);}String[] arrayResult = (String[]) set.toArray(new String[set.size()]);System.out.println(Arrays.toString(arrayResult));req.setAttribute("wentitagList", arrayResult);req.setAttribute("wentiList", wentiList);req.getRequestDispatcher("qiantai/wenti/wenti_all.jsp").forward(req, res);}// 根据问题ID显示说明的回复内容public void huifu_show(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String id = req.getParameter("id");List huifuList = new ArrayList();String sql = "select * from t_huifu where wenti_id='" + id + "'";Object[] params = {};DB mydb = new DB();try {mydb.doPstm(sql, params);ResultSet rs = mydb.getRs();while (rs.next()) {Thuifu huifu = new Thuifu();huifu.setId(rs.getInt("id"));huifu.setContent(rs.getString("content"));huifu.setShijian(rs.getString("shijian"));huifu.setTeaname(rs.getString("teaname"));huifu.setTea_id(rs.getInt("tea_id"));huifu.setTea(liuService.getTea(rs.getInt("tea_id")));huifuList.add(huifu);}rs.close();} catch (Exception e) {e.printStackTrace();}mydb.closed();Twenti wenti = new Twenti();String sql1 = "select * from t_wenti where id=?";Object[] params1 = { id };DB mydb1 = new DB();try {mydb1.doPstm(sql1, params1);ResultSet rs1 = mydb1.getRs();while (rs1.next()) {wenti.setId(rs1.getInt("id"));wenti.setMingcheng(rs1.getString("mingcheng"));wenti.setFujian(rs1.getString("fujian"));wenti.setFujianYuanshiming(rs1.getString("fujianYuanshiming"));wenti.setContent(rs1.getString("content"));wenti.setTag(rs1.getString("tag"));wenti.setStatus(rs1.getInt("status"));wenti.setShijian_shangchuan(rs1.getString("shijian_shangchuan"));wenti.setStu_id(rs1.getInt("stu_id"));wenti.setStu(liuService.getStu(rs1.getInt("stu_id")));}rs1.close();} catch (Exception e) {e.printStackTrace();}mydb1.closed();req.setAttribute("wenti", wenti);req.setAttribute("huifuList", huifuList);req.getRequestDispatcher("qiantai/wenti/wenti_showhuifu.jsp").forward(req, res);}// 教师保存回复public void wenti_huifu(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String huifuneirong = req.getParameter("huifuneirong");String wenti_id = req.getParameter("wenti_id");String shijian = new Date().toLocaleString();// String name=sessionScope.tea.nameHttpSession session = req.getSession();Ttea tea = (Ttea) session.getAttribute("tea");int tea_id = tea.getId();String sql = "INSERT INTO `t_huifu` ( `wenti_id`, `content`, `tea_id`, `shijian`, `teaname`) VALUES ( ?, ?, ?, ?, ?);";Object[] params = { wenti_id, huifuneirong, tea_id, shijian, "" };DB mydb = new DB();mydb.doPstm(sql, params);mydb.closed();req.setAttribute("message", "问题回复完毕");req.setAttribute("path", "wenti?type=huifu_show&id=" + wenti_id);String targetURL = "/common/success.jsp";dispatch(targetURL, req, res);}// 后台的问题列表public void wenti_manage(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String page1 = req.getParameter("page");if (page1 == null) {page1 = "1";}// 分页设置int EVERYPAGENUM = 20;// 每页条数int page = Integer.parseInt(page1); // 传递过来的当前页int cou = 1;// 得到信息总数int pagecount = 1; // 总页数String sql1 = "select count(*) as cou from t_wenti where 1=1 ";if (req.getParameter("mingcheng") != null)sql1 += " AND mingcheng like '%" + req.getParameter("mingcheng").trim() + "%'";if (req.getParameter("tag") != null) {String strPtname = new String(req.getParameter("tag").getBytes("ISO-8859-1"), "UTF-8");sql1 += " AND FIND_IN_SET('" + strPtname + "',tag) ";}Object[] params1 = {};DB mydb1 = new DB();try {mydb1.doPstm(sql1, params1);ResultSet rs = mydb1.getRs();while (rs.next()) {cou = rs.getInt("cou");}rs.close();} catch (Exception e) {e.printStackTrace();}mydb1.closed();if (cou % EVERYPAGENUM == 0) {pagecount = cou / EVERYPAGENUM;} else {pagecount = cou / EVERYPAGENUM + 1;}req.setAttribute("EVERYPAGENUM", EVERYPAGENUM);req.setAttribute("page", page);req.setAttribute("cou", cou);req.setAttribute("pagecount", pagecount);List wentiList = new ArrayList();String sql = "select * from t_wenti where 1=1 ";if (req.getParameter("mingcheng") != null)sql += " AND mingcheng like '%" + req.getParameter("mingcheng").trim() + "%'";if (req.getParameter("tag") != null) {String strPtname = new String(req.getParameter("tag").getBytes("ISO-8859-1"), "UTF-8");sql += " AND FIND_IN_SET('" + strPtname + "',tag) ";}sql += " order by id desc";Object[] params = {};DB mydb = new DB();try {mydb.doPstm(sql, params);ResultSet rs = mydb.getRs();for (int i = 0; i < (page - 1) * EVERYPAGENUM; i++) {rs.next();}for (int t = 0; t < EVERYPAGENUM; t++) {if (rs.next()) {Twenti wenti = new Twenti();wenti.setId(rs.getInt("id"));wenti.setMingcheng(rs.getString("mingcheng"));wenti.setFujian(rs.getString("fujian"));wenti.setFujianYuanshiming(rs.getString("fujianYuanshiming"));wenti.setShijian_shangchuan(rs.getString("shijian_shangchuan"));wenti.setStu_id(rs.getInt("stu_id"));// 加载回复内容String huifuneirong = "无";String sqlre = "select * from t_huifu where wenti_id=?";Object[] paramsre = { rs.getString("id") };DB mydbre = new DB();try {mydb.doPstm(sqlre, paramsre);ResultSet rsre = mydb.getRs();while (rsre.next()) {huifuneirong = "有";}rsre.close();} catch (Exception e) {e.printStackTrace();}wenti.setHuifuneirong(huifuneirong);wenti.setStu(liuService.getStu(rs.getInt("stu_id")));wentiList.add(wenti);} else {break; // 减少空循环的时间}}rs.close();} catch (Exception e) {e.printStackTrace();}mydb.closed();// 获取tagString tags = "";String sql12 = "select * from t_wenti ";Object[] params12 = {};DB mydb12 = new DB();try {mydb12.doPstm(sql12, params12);ResultSet rs12 = mydb12.getRs();while (rs12.next()) {tags += rs12.getString("tag") + ",";}rs12.close();} catch (Exception e) {e.printStackTrace();}mydb12.closed();String[] tags_array = tags.split(",");// 数组去重Set<String> set = new HashSet<>();for (int i = 0; i < tags_array.length; i++) {set.add(tags_array[i]);}String[] arrayResult = (String[]) set.toArray(new String[set.size()]);System.out.println(Arrays.toString(arrayResult));req.setAttribute("wentitagList", arrayResult);req.setAttribute("wentiList", wentiList);req.getRequestDispatcher("admin/wenti/wentiMana.jsp").forward(req, res);}// 后台问题删除public void wenti_del(HttpServletRequest req, HttpServletResponse res) {String id = req.getParameter("id");String sql = "delete from t_wenti where id=?";Object[] params = { id };DB mydb = new DB();mydb.doPstm(sql, params);sql = "delete from t_huifu where wenti_id=?";Object[] params1 = { id };mydb.doPstm(sql, params1);mydb.closed();req.setAttribute("message", "操作成功");req.setAttribute("path", "wenti?type=wenti_manage");String targetURL = "/common/success.jsp";dispatch(targetURL, req, res);}// 前台学生问题删除public void wenti_delqiantai(HttpServletRequest req, HttpServletResponse res) {String id = req.getParameter("id");String sql = "delete from t_wenti where id=?";Object[] params = { id };DB mydb = new DB();mydb.doPstm(sql, params);sql = "delete from t_huifu where wenti_id=?";Object[] params1 = { id };mydb.doPstm(sql, params1);mydb.closed();req.setAttribute("message", "操作成功");req.setAttribute("path", "wenti?type=wenti_mime");String targetURL = "/common/success.jsp";dispatch(targetURL, req, res);}// 前台教师回复删除public void huifu_delqiantai(HttpServletRequest req, HttpServletResponse res) {String id = req.getParameter("id");String sql = "delete from t_huifu where id=?";Object[] params1 = { id };DB mydb = new DB();mydb.doPstm(sql, params1);mydb.closed();req.setAttribute("message", "操作成功");req.setAttribute("path", "wenti?type=huifu_mime");String targetURL = "/common/success.jsp";dispatch(targetURL, req, res);}//问题结束public void wenti_jieshu(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String id = req.getParameter("id");String status = req.getParameter("status");String sql = "update t_wenti set status='" + status + "' where id=" + Integer.parseInt(id);Object[] params = {};DB mydb = new DB();mydb.doPstm(sql, params);mydb.closed();req.setAttribute("message", "操作成功");req.setAttribute("path", "wenti?type=wenti_mime");String targetURL = "/common/success.jsp";dispatch(targetURL, req, res);}public void dispatch(String targetURI, HttpServletRequest request, HttpServletResponse response) {RequestDispatcher dispatch = getServletContext().getRequestDispatcher(targetURI);try {dispatch.forward(request, response);return;} catch (ServletException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void init(ServletConfig config) throws ServletException {super.init(config);}public void destroy() {}
}
4 效果演示
idea或eclipse开发,mysql数据库