0、效果展示
1、概述
2、搭建环境
本文以实现一个线上在线考试网站为目标,从环境搭建到编码实现全过程讲述
我们使用javaweb、J2EE来构建在线考试网站,环境使用最新版jdk和tomcat,配合mysql数据库
开发工具使用idea(也可以使用eclipse),数据库管理工具使用Navicat Premium
开发框架使用JavaBean Servlet MVC结构;
没有使用SSH(Struts+Spring+Hibernate)或SSM(Spring+SpringMVC+MyBatis),这两个框架我们在别的项目中再介绍开发过程
在项目中会引入My97DatePicker作为前端日期时间选择工具,使用fckeditor作为富媒体编辑器(也可以使用百度的ueditor)
使用DWR(Direct Web Remoting)用于改善web页面与Java类交互,实现远程服务器端AJAX读取登录数据。
使用JSTL(Java server pages standarded tag library,即JSP标准标签库),此库是由JCP(Java community Proces)所制定的标准规范,它主要提供给Java Web开发人员一个标准通用的标签库,并由Apache的Jakarta小组来维护。开发人员可以利用这些标签取代JSP页面上的Java代码,从而提高程序的可读性,降低程序的维护难度。
3、数据表结构
表4.3 t_chengji成绩表
字段名称 | 自动增长 | 字段类型 | 说明 |
id | - | int | 编号 |
stu_id | - | int | 学生编号 |
kecheng_id | - | int | 课程编号 |
chengji | - | double | 成绩 |
istest | - | varchar | 是否测试 |
shijian | - | varchar | 时间 |
del | - | varchar | 是否删除 |
表4.4 t_liuyan 留言表
名称 | 类型 | 说明 |
id | int(11) | 编号 |
title | text | 标题 |
content | text | 内容 |
shijian | text | 时间 |
name | text | 留言人名称 |
4、后端代码示例
package com.action;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
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.Tstu;
import com.orm.Ttimu;
import com.orm.Ttimu;
import com.orm.Ttimustu;
import com.service.liuService;
public class timu_servlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
String type=req.getParameter("type");
if(type.endsWith("timuAdd1"))
{
timuAdd1(req, res);//单选
}
if(type.endsWith("timuAdd2"))
{
timuAdd2(req, res);//多选
}
if(type.endsWith("timuAdd3"))
{
timuAdd3(req, res);//判断
}
if(type.endsWith("timuCreate1"))
{
timuCreate1(req, res);
}
if(type.endsWith("timuCreate2"))
{
timuCreate2(req, res);
}
if(type.endsWith("timuCreate3"))
{
timuCreate3(req, res);
}
if(type.endsWith("timuDetail"))
{
timuDetail(req, res);
}
if(type.endsWith("timuMana1"))
{
timuMana1(req, res);
}
if(type.endsWith("timuMana2"))
{
timuMana2(req, res);
}
if(type.endsWith("timuMana3"))
{
timuMana3(req, res);
}
if(type.endsWith("timuDel1"))
{
timuDel1(req, res);
}
if(type.endsWith("timuDel2"))
{
timuDel2(req, res);
}
if(type.endsWith("timuDel3"))
{
timuDel3(req, res);
}
//在线考试
if(type.endsWith("timuSuiji"))
{
timuSuiji(req, res);
}
//在线测试
if(type.endsWith("timuSuijicheshi"))
{
timuSuijicheshi(req, res);
}
//得分计算,错题保存 成绩保存
if(type.endsWith("timuSuiji_defen"))
{
timuSuiji_defen(req, res);
}
if(type.endsWith("timu_stu"))
{
timu_stu(req, res);
}
//题目收藏
if(type.endsWith("timu_stuSave"))
{
timu_stuSave(req, res);
}
if(type.endsWith("timu_stuDel"))
{
timu_stuDel(req, res);
}
if(type.endsWith("timustuDetail"))
{
timustuDetail(req, res);
}
}
public void timuAdd1(HttpServletRequest req,HttpServletResponse res)
{
String name=req.getParameter("name");
String xuanxianga=req.getParameter("xuanxianga");
String xuanxiangb=req.getParameter("xuanxiangb");
String zhangjie_id=req.getParameter("zhangjie_id");
String xuanxiangc=req.getParameter("xuanxiangc");
String xuanxiangd=req.getParameter("xuanxiangd");
String daan=req.getParameter("daan");
//int fenshu=Integer.parseInt(req.getParameter("fenshu"));
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,xuanxianga,xuanxiangb,xuanxiangc,xuanxiangd,daan,"0.5",del,"1",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana1");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuAdd2(HttpServletRequest req,HttpServletResponse res)
{
String name=req.getParameter("name");
String xuanxianga=req.getParameter("xuanxianga");
String xuanxiangb=req.getParameter("xuanxiangb");
String zhangjie_id=req.getParameter("zhangjie_id");
String xuanxiangc=req.getParameter("xuanxiangc");
String xuanxiangd=req.getParameter("xuanxiangd");
String[] daan1=req.getParameterValues("daan");
String daan="";
for (int i = 0; i < daan1.length; i++) {
daan += daan1[i] + ",";
}
daan=daan.replaceAll("(.*),","$1"); //清除掉右侧多余的空格
//int fenshu=Integer.parseInt(req.getParameter("fenshu"));
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,xuanxianga,xuanxiangb,xuanxiangc,xuanxiangd,daan,"1",del,"2",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana2");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuAdd3(HttpServletRequest req,HttpServletResponse res)
{
String name=req.getParameter("name");
String daan=req.getParameter("daan");
String zhangjie_id=req.getParameter("zhangjie_id");
//int fenshu=Integer.parseInt(req.getParameter("fenshu"));
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,"","","","",daan,"1",del,"3",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana3");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
//随机生成单选 试题数据保存入数据库
public void timuCreate1(HttpServletRequest req,HttpServletResponse res)
{
for(int i=1;i<1001;i++)
{
int numb1000=new Random().nextInt(1000)+1;
String name="单选题目 "+i;
String xuanxianga="答案A "+numb1000;
String xuanxiangb="答案B "+numb1000;
int zhangjie_id=new Random().nextInt(10)+1;
String xuanxiangc="答案C "+numb1000;
String xuanxiangd="答案D "+numb1000;
int numb1=new Random().nextInt(4)+1;
String daan1="A";
if(numb1==1) daan1="A";
if(numb1==2) daan1="B";
if(numb1==3) daan1="C";
if(numb1==4) daan1="D";
String daan=daan1;
//分数
String fenshu="0.5";
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,xuanxianga,xuanxiangb,xuanxiangc,xuanxiangd,daan,fenshu,del,"1",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
}
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana1");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
//随机生成多选 试题数据保存入数据库
public void timuCreate2(HttpServletRequest req,HttpServletResponse res)
{
for(int i=1;i<1001;i++)
{
int numb1000=new Random().nextInt(1000)+1;
String name="多选题目 "+i;
String xuanxianga="答案A "+numb1000;
String xuanxiangb="答案B "+numb1000;
int zhangjie_id=new Random().nextInt(10)+1;
String xuanxiangc="答案C "+numb1000;
String xuanxiangd="答案D "+numb1000;
int numb1=new Random().nextInt(4)+1;
String daan1="A";
if(numb1==1) daan1="A,B";
if(numb1==2) daan1="B,C";
if(numb1==3) daan1="C,D";
if(numb1==4) daan1="A,B,C,D";
String daan=daan1;
//分数
int fenshu=1;
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,xuanxianga,xuanxiangb,xuanxiangc,xuanxiangd,daan,fenshu,del,"2",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
}
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana2");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
//随机生成判断 试题数据保存入数据库
public void timuCreate3(HttpServletRequest req,HttpServletResponse res)
{
for(int i=1;i<1001;i++)
{
int numb1000=new Random().nextInt(1000)+1;
String name="判断题目 "+i;
String xuanxianga="判断题内容 "+numb1000;
String xuanxiangb=" ";
int zhangjie_id=new Random().nextInt(10)+1;
String xuanxiangc=" ";
String xuanxiangd=" ";
int numb1=new Random().nextInt(2)+1;
String daan1="A";
if(numb1==1) daan1="正确";
if(numb1==2) daan1="错误";
String daan=daan1;
//分数
int fenshu=1;
String del="no";
String sql="insert into t_timu values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] params={null,name,xuanxianga,xuanxiangb,xuanxiangc,xuanxiangd,daan,fenshu,del,"3",zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
}
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana3");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuDel1(HttpServletRequest req,HttpServletResponse res)
{
int id=Integer.parseInt(req.getParameter("id"));
String sql="update t_timu set del='yes' where id=?";
Object[] params={id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana1");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuDel2(HttpServletRequest req,HttpServletResponse res)
{
int id=Integer.parseInt(req.getParameter("id"));
String sql="update t_timu set del='yes' where id=?";
Object[] params={id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana2");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuDel3(HttpServletRequest req,HttpServletResponse res)
{
int id=Integer.parseInt(req.getParameter("id"));
String sql="update t_timu set del='yes' where id=?";
Object[] params={id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timuMana3");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuMana1(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
int stuid=stu.getId();//当前登录的用户的ID
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_timu where del='no' and type='1'" +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql1+=" and name like '%"+req.getParameter("name").trim()+"%'";
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 timuList=new ArrayList();
String sql="select * from t_timu where del='no' and type='1' " +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql+=" and name like '%"+req.getParameter("name").trim()+"%'";
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()) {
Ttimu timu=new Ttimu();
timu.setId(rs.getInt("id"));
timu.setZhangjie_id(rs.getInt("zhangjie_id"));
timu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timu.setName(rs.getString("name"));
timu.setXuanxianga(rs.getString("xuanxianga"));
timu.setXuanxiangb(rs.getString("xuanxiangb"));
timu.setXuanxiangc(rs.getString("xuanxiangc"));
timu.setXuanxiangd(rs.getString("xuanxiangd"));
timu.setDaan(rs.getString("daan"));
timu.setFenshu(rs.getInt("fenshu"));
timuList.add(timu);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("timuList", timuList);
req.getRequestDispatcher("tea/timu/timuMana1.jsp").forward(req, res);
}
public void timuMana2(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
int stuid=stu.getId();//当前登录的用户的ID
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_timu where del='no' and type='2'" +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql1+=" and name like '%"+req.getParameter("name").trim()+"%'";
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 timuList=new ArrayList();
String sql="select * from t_timu where del='no' and type='2' " +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql+=" and name like '%"+req.getParameter("name").trim()+"%'";
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()) {
Ttimu timu=new Ttimu();
timu.setId(rs.getInt("id"));
timu.setZhangjie_id(rs.getInt("zhangjie_id"));
timu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timu.setName(rs.getString("name"));
timu.setXuanxianga(rs.getString("xuanxianga"));
timu.setXuanxiangb(rs.getString("xuanxiangb"));
timu.setXuanxiangc(rs.getString("xuanxiangc"));
timu.setXuanxiangd(rs.getString("xuanxiangd"));
timu.setDaan(rs.getString("daan"));
timu.setFenshu(rs.getInt("fenshu"));
timuList.add(timu);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("timuList", timuList);
req.getRequestDispatcher("tea/timu/timuMana2.jsp").forward(req, res);
}
public void timuMana3(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
int stuid=stu.getId();//当前登录的用户的ID
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_timu where del='no' and type='3'" +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql1+=" and name like '%"+req.getParameter("name").trim()+"%'";
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 timuList=new ArrayList();
String sql="select * from t_timu where del='no' and type='3' " +
" and zhangjie_id in (select id from t_zhangjie where del='no' and kecheng_id in (select id from t_kecheng where del='no' and stu_id="+stuid+")) ";
if(req.getParameter("name")!=null)sql+=" and name like '%"+req.getParameter("name").trim()+"%'";
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()) {
Ttimu timu=new Ttimu();
timu.setId(rs.getInt("id"));
timu.setZhangjie_id(rs.getInt("zhangjie_id"));
timu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timu.setName(rs.getString("name"));
timu.setXuanxianga(rs.getString("xuanxianga"));
timu.setXuanxiangb(rs.getString("xuanxiangb"));
timu.setXuanxiangc(rs.getString("xuanxiangc"));
timu.setXuanxiangd(rs.getString("xuanxiangd"));
timu.setDaan(rs.getString("daan"));
timu.setFenshu(rs.getInt("fenshu"));
timuList.add(timu);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("timuList", timuList);
req.getRequestDispatcher("tea/timu/timuMana3.jsp").forward(req, res);
}
//在线考试
public void timuSuiji(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
int kecheng_id= Integer.parseInt( req.getParameter("kecheng_id"));
List<Integer> timu_idList1=liuService.getAllTimuId(kecheng_id,1); //所有单选题
List<Integer> timu_idList2=liuService.getAllTimuId(kecheng_id,2); //所有多选题
List<Integer> timu_idList3=liuService.getAllTimuId(kecheng_id,3); //所有判断选题
int[] timu_id1=liuService.Random(timu_idList1, 90);//随机抽取不同的90个题目的id 单选题
List timuList1=new ArrayList();
for(int i=0;i<timu_id1.length;i++)
{
timuList1.add(liuService.getTimuById(timu_id1[i]));
}
req.getSession().setAttribute("timuList1", timuList1);
int[] timu_id2=liuService.Random(timu_idList2, 40);//随机抽取不同的4个题目的id 多选题
List timuList2=new ArrayList();
for(int i=0;i<timu_id2.length;i++)
{
timuList2.add(liuService.getTimuById(timu_id2[i]));
}
req.getSession().setAttribute("timuList2", timuList2);
int[] timu_id3=liuService.Random(timu_idList3, 15);//随机抽取不同的15个题目的id 判断题
List timuList3=new ArrayList();
for(int i=0;i<timu_id3.length;i++)
{
timuList3.add(liuService.getTimuById(timu_id3[i]));
}
req.getSession().setAttribute("timuList3", timuList3);
req.getSession().setAttribute("kecheng_id", kecheng_id);
req.getRequestDispatcher("qiantai/timu/timuSuiji.jsp").forward(req, res);
}
//测试
public void timuSuijicheshi(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
int kecheng_id=Integer.parseInt(req.getParameter("kecheng_id"));
int isdaan=Integer.parseInt(req.getParameter("isdaan"));
List<Integer> timu_idList1=liuService.getAllTimuId(kecheng_id,1); //所有单选题
List<Integer> timu_idList2=liuService.getAllTimuId(kecheng_id,2); //所有多选题
List<Integer> timu_idList3=liuService.getAllTimuId(kecheng_id,3); //所有判断选题
int[] timu_id1=liuService.Random(timu_idList1, 90);//随机抽取不同的90个题目的id 单选题
List timuList1=new ArrayList();
for(int i=0;i<timu_id1.length;i++)
{
timuList1.add(liuService.getTimuById(timu_id1[i]));
}
req.getSession().setAttribute("timuList1", timuList1);
int[] timu_id2=liuService.Random(timu_idList2, 40);//随机抽取不同的4个题目的id 多选题
List timuList2=new ArrayList();
for(int i=0;i<timu_id2.length;i++)
{
timuList2.add(liuService.getTimuById(timu_id2[i]));
}
req.getSession().setAttribute("timuList2", timuList2);
int[] timu_id3=liuService.Random(timu_idList3, 15);//随机抽取不同的15个题目的id 判断题
List timuList3=new ArrayList();
for(int i=0;i<timu_id3.length;i++)
{
timuList3.add(liuService.getTimuById(timu_id3[i]));
}
req.getSession().setAttribute("timuList3", timuList3);
req.getSession().setAttribute("kecheng_id", kecheng_id);
req.getSession().setAttribute("isdaan", isdaan);
req.getRequestDispatcher("qiantai/timu/timuSuijicheshi.jsp").forward(req, res);
}
public void timuSuiji_defen(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
int fenshu=0;
int truenumb=0;
int falsenumb=0;
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
int stuid=stu.getId();//当前登录的用户的ID
String shijian=new Date().toLocaleString();
//单选题判断
List timuList1=(List)req.getSession().getAttribute("timuList1");
for(int i=0;i<timuList1.size();i++)
{
Ttimu timu=(Ttimu)timuList1.get(i);
String timuDaan=timu.getDaan();
String timuDaan_user=req.getParameter(String.valueOf(timu.getId()));
if(timuDaan.equalsIgnoreCase(timuDaan_user))
{
fenshu+=timu.getFenshu();//正确得分累加
truenumb++;
}else
{//将错存入 我的题目
falsenumb++;
int timuid=timu.getId();
String sql="select * from t_timu_stu where stu_id="+stuid+" and timu_id="+timuid;
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
if(rs.next())
{//如果有此记录 则更新
String sql1="update t_timu_stu set isfalse='错题',shijian='"+shijian+"' where stu_id='"+stuid+"' and timu_id="+timuid;
Object[] params1={};
DB mydb1=new DB();
mydb1.doPstm(sql1, params1);
mydb1.closed();
}else
{
//无此记录则添加
String sql2="insert into t_timu_stu values(?,?,?,?,?)";
Object[] params2={null,stuid,timuid,"错题",shijian};
DB mydb2=new DB();
mydb2.doPstm(sql2, params2);
mydb2.closed();
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//多选题判断
List timuList2=(List)req.getSession().getAttribute("timuList2");
for(int i=0;i<timuList2.size();i++)
{
Ttimu timu=(Ttimu)timuList2.get(i);
String timuDaan=timu.getDaan();
String timuDaan_user=req.getParameter(String.valueOf(timu.getId()));
if(timuDaan.equalsIgnoreCase(timuDaan_user))
{
fenshu+=timu.getFenshu();//正确得分累加
truenumb++;
}else
{//将错存入 我的题目
falsenumb++;
int timuid=timu.getId();
String sql="select * from t_timu_stu where stu_id="+stuid+" and timu_id="+timuid;
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
if(rs.next())
{//如果有此记录 则更新
String sql1="update t_timu_stu set isfalse='错题',shijian='"+shijian+"' where stu_id='"+stuid+"' and timu_id="+timuid;
Object[] params1={};
DB mydb1=new DB();
mydb1.doPstm(sql1, params1);
mydb1.closed();
}else
{
//无此记录则添加
String sql2="insert into t_timu_stu values(?,?,?,?,?)";
Object[] params2={null,stuid,timuid,"错题",shijian};
DB mydb2=new DB();
mydb2.doPstm(sql2, params2);
mydb2.closed();
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//判断题判断
List timuList3=(List)req.getSession().getAttribute("timuList3");
for(int i=0;i<timuList3.size();i++)
{
Ttimu timu=(Ttimu)timuList3.get(i);
String timuDaan=timu.getDaan();
String timuDaan_user=req.getParameter(String.valueOf(timu.getId()));
if(timuDaan.equalsIgnoreCase(timuDaan_user))
{
fenshu+=timu.getFenshu();//正确得分累加
truenumb++;
}else
{//将错存入 我的题目
falsenumb++;
int timuid=timu.getId();
String sql="select * from t_timu_stu where stu_id="+stuid+" and timu_id="+timuid;
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
if(rs.next())
{//如果有此记录 则更新
String sql1="update t_timu_stu set isfalse='错题',shijian='"+shijian+"' where stu_id='"+stuid+"' and timu_id="+timuid;
Object[] params1={};
DB mydb1=new DB();
mydb1.doPstm(sql1, params1);
mydb1.closed();
}else
{
//无此记录则添加
String sql2="insert into t_timu_stu values(?,?,?,?,?)";
Object[] params2={null,stuid,timuid,"错题",shijian};
DB mydb2=new DB();
mydb2.doPstm(sql2, params2);
mydb2.closed();
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//保存成绩
String sql3="insert into t_chengji values(?,?,?,?,?,?,?)";
Object[] params3={null,stuid,req.getParameter("kecheng_id"),fenshu,req.getParameter("istest"),shijian,"no"};
DB mydb3=new DB();
mydb3.doPstm(sql3, params3);
mydb3.closed();
req.setAttribute("message", "本次测试得分:"+fenshu+" 正确:"+truenumb+" 错误:"+falsenumb+" 错误题目已经保存入'我的题目'");
req.setAttribute("path", "qiantai/default.jsp");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void timuDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
String id=req.getParameter("id");
Ttimu timu=new Ttimu();
String sql="select * from t_timu where id=?";
Object[] params={id};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
rs.next();
timu.setId(rs.getInt("id"));
timu.setZhangjie_id(rs.getInt("zhangjie_id"));
timu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timu.setName(rs.getString("name"));
timu.setXuanxianga(rs.getString("xuanxianga"));
timu.setXuanxiangb(rs.getString("xuanxiangb"));
timu.setXuanxiangc(rs.getString("xuanxiangc"));
timu.setXuanxiangd(rs.getString("xuanxiangd"));
timu.setDaan(rs.getString("daan"));
timu.setFenshu(rs.getInt("fenshu"));
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("timu", timu);
req.getRequestDispatcher("admin/timu/timuDetail.jsp").forward(req, res);
}
//前台用户查看题目详情
public void timustuDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
String id=req.getParameter("id");
int timutype=0; //用于判断跳转页面,单选题和多选一样,判断题另一个页面
Ttimu timu=new Ttimu();
String sql="select * from t_timu where id=?";
Object[] params={id};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
rs.next();
timu.setId(rs.getInt("id"));
timu.setZhangjie_id(rs.getInt("zhangjie_id"));
timu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timu.setName(rs.getString("name"));
timu.setXuanxianga(rs.getString("xuanxianga"));
timu.setXuanxiangb(rs.getString("xuanxiangb"));
timu.setXuanxiangc(rs.getString("xuanxiangc"));
timu.setXuanxiangd(rs.getString("xuanxiangd"));
timu.setDaan(rs.getString("daan"));
timu.setFenshu(rs.getInt("fenshu"));
timutype=rs.getInt("type");
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
String jumpurl="qiantai/timustu/timuDetail.jsp";
if(timutype==3)jumpurl="qiantai/timustu/timuDetail3.jsp";
req.setAttribute("timu", timu);
req.getRequestDispatcher(jumpurl).forward(req, res);
}
//用户收藏题目删除
public void timu_stuDel(HttpServletRequest req,HttpServletResponse res)
{
String sql="DELETE FROM `t_timu_stu` WHERE (`id`='"+Integer.parseInt(req.getParameter("id"))+"')";
Object[] params={};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "timu?type=timu_stu");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
//用户收藏题目保存
public void timu_stuSave(HttpServletRequest req,HttpServletResponse res)
{
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
int stuid=stu.getId();//当前登录的用户的ID
String shijian=new Date().toLocaleString();
String timuid=req.getParameter("timuid");
List timustuList=new ArrayList();
String sql="select * from t_timu_stu where stu_id="+stuid+" and timu_id="+timuid;
Object[] params={};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
if(rs.next())
{//如果有此记录 则更新
String sql1="update t_timu_stu set isfalse='收藏',shijian='"+shijian+"' where stu_id='"+stuid+"' and timu_id="+timuid;
Object[] params1={};
DB mydb1=new DB();
mydb1.doPstm(sql1, params1);
mydb1.closed();
}else
{
//无此记录则添加
String sql2="insert into t_timu_stu values(?,?,?,?,?)";
Object[] params2={null,stuid,timuid,"收藏",shijian};
DB mydb2=new DB();
mydb2.doPstm(sql2, params2);
mydb2.closed();
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
req.setAttribute("message", "操作成功");
req.setAttribute("path", "index");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
//前台用户收藏题目
public void timu_stu(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
HttpSession session=req.getSession();
Tstu stu=(Tstu)session.getAttribute("stu");
/* if(stu==null){
String targetURL = "/common/nologin.jsp";
req.getRequestDispatcher(targetURL).forward(req, res);
}
*/ int stuid=stu.getId();//当前登录的用户的ID
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_timu_stu where stu_id="+stuid+"";
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 timustuList=new ArrayList();
String sql="select t_timu_stu.*," +
"t_stu.name1 as stu_name," +
"t_timu.name timu_name," +
"t_timu.zhangjie_id zhangjie_id from t_timu_stu " +
" left join t_stu on t_stu.id=t_timu_stu.stu_id " +
" left join t_timu on t_timu.id=t_timu_stu.timu_id " +
" left join t_zhangjie on t_zhangjie.id=t_timu.zhangjie_id" +
" left join t_kecheng on t_kecheng.id=t_zhangjie.kecheng_id " +
"where t_timu_stu.stu_id=? " ;
if(req.getParameter("kecheng_id")!=null&&!req.getParameter("kecheng_id").equals("0"))sql+=" and t_kecheng.id = '"+req.getParameter("kecheng_id").trim()+"'";
sql+="order by id desc";
Object[] params={stuid};
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()) {
Ttimustu timustu=new Ttimustu();
timustu.setId(rs.getInt("id"));
timustu.setStu_id(rs.getInt("stu_id"));
timustu.setStu_name(rs.getString("stu_name"));
timustu.setZhangjie_id(rs.getInt("zhangjie_id"));
timustu.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
timustu.setTimu_id(rs.getInt("timu_id"));
timustu.setTimu_name(rs.getString("timu_name"));
timustu.setIsfalse(rs.getString("isfalse"));
timustu.setShijian(rs.getString("shijian"));
timustuList.add(timustu);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("timustuList", timustuList);
req.getRequestDispatcher("qiantai/timustu/timustu.jsp").forward(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()
{
}
}
5、前端代码示例
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page isELIgnored="false" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
<link rel="stylesheet" type="text/css" href="<%=path %>/css/base.css" />
<script language="javascript">
function chengjiDel(id)
{
if(confirm('您确定删除吗?'))
{
window.location.href="<%=path %>/chengji?type=chengjiDel&id="+id;
}
}
function p()
{
window.print();
}
</script>
</head>
<body leftmargin="2" topmargin="2" >
<table width="98%" border="0" cellpadding="2" cellspacing="1" bgcolor="#D1DDAA" align="center" style="margin-top:8px">
<tr bgcolor="#E7E7E7">
<td height="14" colspan="9" background="<%=path %>/images/wbg.gif"> 考试成绩管理 </td>
</tr>
<tr align="center" bgcolor="#FAFAF1" height="22">
<td>用户名</td>
<td>课程名称</td>
<td>成绩</td>
<td>测试/考试</td>
<td >日期</td>
<td>操作</td>
</tr>
<c:forEach items="${requestScope.chengjiList}" var="chengji">
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td bgcolor="#FFFFFF" align="center">
${chengji.stu_name}
</td>
<td bgcolor="#FFFFFF" align="center">
${chengji.kecheng_name}
</td>
<td bgcolor="#FFFFFF" align="center">
${chengji.chengji}
</td>
<td bgcolor="#FFFFFF" align="center">
${chengji.istest}
</td>
<td bgcolor="#FFFFFF" align="center">
${chengji.shijian}
</td>
<td bgcolor="#FFFFFF" align="center">
<a href="#" onclick="chengjiDel(${chengji.id})" class="pn-loperator">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>