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_gonggao 学习内容表
名称 | 类型 | 说明 |
id | int(11) | 编号 |
title | longtext | 标题 |
content | longtext | 内容 |
shijian | longtext | 时间 |
表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 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 com.dao.DB;
import com.orm.Tdoc;
import com.orm.Tzhangjie;
import com.service.liuService;
public class doc_servlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
{
String type=req.getParameter("type");
if(type.endsWith("docAdd"))
{
docAdd(req, res);
}
if(type.endsWith("docMana"))
{
docMana(req, res);
}
if(type.endsWith("docDel"))
{
docDel(req, res);
}
if(type.endsWith("docDetail"))
{
docDetail(req, res);
}
if(type.endsWith("docAll"))
{
docAll(req, res);
}
if(type.endsWith("docDetailQian"))
{
docDetailQian(req, res);
}
}
public void docAdd(HttpServletRequest req,HttpServletResponse res)
{
String id=String.valueOf(new Date().getTime());
String title=req.getParameter("title");
String zhangjie_id=req.getParameter("zhangjie_id");
String content=req.getParameter("content");
String fujian=req.getParameter("fujian");
String fujianYuanshiming=req.getParameter("fujianYuanshiming");
String shijian=req.getParameter("shijian");
String del="no";
String sql="insert into t_doc values(?,?,?,?,?,?,?,?)";
Object[] params={id,title,content,fujian,fujianYuanshiming,shijian,del,zhangjie_id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "doc?type=docMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void docDel(HttpServletRequest req,HttpServletResponse res)
{
String id=req.getParameter("id");
String sql="delete from t_doc where id=?";
Object[] params={id};
DB mydb=new DB();
mydb.doPstm(sql, params);
mydb.closed();
req.setAttribute("message", "操作成功");
req.setAttribute("path", "doc?type=docMana");
String targetURL = "/common/success.jsp";
dispatch(targetURL, req, res);
}
public void docDetail(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
String id=req.getParameter("id");
Tdoc doc=new Tdoc();
String sql="select * from t_doc where id=?";
Object[] params={id};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
rs.next();
doc.setId(rs.getString("id"));
doc.setTitle(rs.getString("title"));
doc.setContent(rs.getString("content"));
doc.setFujian(rs.getString("fujian"));
doc.setFujianYuanshiming(rs.getString("fujianYuanshiming"));
doc.setShijian(rs.getString("shijian"));
doc.setZhangjie_id(rs.getInt("zhangjie_id"));
doc.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("doc", doc);
req.getRequestDispatcher("admin/doc/docDetail.jsp").forward(req, res);
}
public void docDetailQian(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
String id=req.getParameter("id");
Tdoc doc=new Tdoc();
String sql="select * from t_doc where id=?";
Object[] params={id};
DB mydb=new DB();
try
{
mydb.doPstm(sql, params);
ResultSet rs=mydb.getRs();
rs.next();
doc.setId(rs.getString("id"));
doc.setTitle(rs.getString("title"));
doc.setContent(rs.getString("content"));
doc.setFujian(rs.getString("fujian"));
doc.setFujianYuanshiming(rs.getString("fujianYuanshiming"));
doc.setShijian(rs.getString("shijian"));
doc.setZhangjie_id(rs.getInt("zhangjie_id"));
doc.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("doc", doc);
req.getRequestDispatcher("qiantai/doc/docDetailQian.jsp").forward(req, res);
}
public void docMana(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_doc where del='no'";
if(req.getParameter("name")!=null)sql1+=" and title 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 docList=new ArrayList();
String sql="select * from t_doc where del='no'";
if(req.getParameter("name")!=null)sql+=" and title 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()) {
Tdoc doc=new Tdoc();
doc.setId(rs.getString("id"));
doc.setTitle(rs.getString("title"));
doc.setZhangjie_id(rs.getInt("zhangjie_id"));
doc.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
doc.setContent(rs.getString("content"));
doc.setFujian(rs.getString("fujian"));
doc.setFujianYuanshiming(rs.getString("fujianYuanshiming"));
doc.setShijian(rs.getString("shijian"));
docList.add(doc);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("docList", docList);
req.getRequestDispatcher("admin/doc/docMana.jsp").forward(req, res);
}
//前台按词库分类显示学习资料
public void docAll(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
String kechengid=req.getParameter("kechengid");
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_doc where del='no' and zhangjie_id in(select id from t_zhangjie where kecheng_id="+kechengid+")";
if(req.getParameter("name")!=null)sql1+=" and title 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 docList=new ArrayList();
String sql="select * from t_doc where del='no' and zhangjie_id in(select id from t_zhangjie where kecheng_id="+kechengid+")";
if(req.getParameter("name")!=null)sql+=" and title 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()) {
Tdoc doc=new Tdoc();
doc.setId(rs.getString("id"));
doc.setTitle(rs.getString("title"));
doc.setContent(rs.getString("content"));
doc.setFujian(rs.getString("fujian"));
doc.setFujianYuanshiming(rs.getString("fujianYuanshiming"));
doc.setShijian(rs.getString("shijian"));
doc.setZhangjie_id(rs.getInt("zhangjie_id"));
doc.setZhangjie_name(liuService.getZhangjieName(rs.getInt("zhangjie_id")));
docList.add(doc);
} else {
break; //减少空循环的时间
}
}
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
mydb.closed();
req.setAttribute("kechengid", kechengid);
req.setAttribute("docList", docList);
req.getRequestDispatcher("qiantai/doc/docAll.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" src="<%=path %>/js/public.js" type="text/javascript"></script>
<script language="javascript">
function docAdd()
{
var url="<%=path %>/admin/doc/docAdd.jsp";
window.location.href=url;
}
function docDetail(id)
{
var url="<%=path %>/doc?type=docDetail&id="+id;
var n="";
var w="500px";
var h="400px";
var s="resizable:no;help:no;status:no;scroll:yes";
window.location.href=url;
}
function down1(fujianPath,fujianYuashiMing)
{
var url="<%=path %>/updown/updown.jsp?fujianPath="+fujianPath+"&fujianYuashiMing="+fujianYuashiMing;
url=encodeURI(url);
url=encodeURI(url);
window.open(url,"_self");
}
</script>
</head>
<body leftmargin="2" topmargin="2" >
<table width='98%' border='0' align="center">
<tr>
<td ><form action="<%=path %>/doc?type=docMana" name="formAdd" method="post">
<input type="button" value="添加" style="width: 80px;" onclick="docAdd()" />
<input type="text" name="name" size="20" value="<%if(request.getParameter("name")!=null){out.print(request.getParameter("name"));} %>"/>
<input type="submit" value="搜索" style="width:40px;" />
</form></td>
</tr>
</table>
<table width="98%" border="0" cellpadding="2" cellspacing="1" bgcolor="#D1DDAA" align="center" style="margin-top:8px">
<tr bgcolor="#E7E7E7">
<td height="14" colspan="6" 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.docList}" var="doc">
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='red';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="22">
<td bgcolor="#FFFFFF" align="center">
${doc.title}
</td>
<td bgcolor="#FFFFFF" align="center">
<a href="#" onclick="docDetail(${doc.id})" class="pn-loperator">查看内容</a>
</td>
<td bgcolor="#FFFFFF" align="center">
${doc.zhangjie_name}
</td>
<td bgcolor="#FFFFFF" align="center">
<a href="#" onclick="down1('${doc.fujian}','${doc.fujianYuanshiming}')" style="font-size: 10px;color: red">down</a>
</td>
<td bgcolor="#FFFFFF" align="center">
${doc.shijian}
</td>
<td bgcolor="#FFFFFF" align="center">
<a class="pn-loperator" href="<%=path %>/doc?type=docDel&id=${doc.id}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<table width='98%' border='0'style="margin-top:8px;margin-left: 5px;">
<TR align="right">
<TD ><form action="" method="post" name="formpage">
<input type="hidden" name="pageCount" value="${requestScope.pagecount}" />
<!--//用于给上面javascript传值-->
<input type="hidden" name="page" value="${requestScope.page}" />
<!--//用于给上面javascript传值-->
<input type="hidden" name="jumpurl" value="<%=path%>/doc?type=docMana&" />
<!--//用于给上面javascript传值-->
<a href="#" onClick="PageTop()"><strong>首页</strong></a>
<a href="#" onClick='PagePre()'><strong>上一页</strong></a>
共${requestScope.cou}条记录,
共计${requestScope.pagecount}页,
当前第${requestScope.page}页
<a href="#" onClick="PageNext()"><strong>下一页</strong></a>
<a href="#" onClick="PageLast()"><strong>尾页</strong></a> 第
<input name="busjump" type="text" size="3" value="${requestScope.page}" style=" width:15px"/>
页<a href="#" onClick="bjump()"><strong>跳转</strong></a>
</form>
<script type="text/javascript" src="<%=path%>/js/page.js"></script></TD>
</TR>
</table>
</body>
</html>