Javaweb-Document-Management-System-Javaweb-
本示例是使用JavaWeb技术实现一个简单的文章管理系统(新闻管理系统)其中主要功能如下:
- 用户和管理员登录
- 用户发布新文章、文章详情查看、文章修改、文章删除与恢复
- 用户查看他人对自己授权的文章及其文章信息
- 用户将自己的文章对他人进行授权
- 管理员对普通用户新发布的文章进行审核和删除
- 管理员查看普通用户发布的所有文章及其详情
- 管理员发布新文章
使用的主要技术有: JavaEE、JDBC、AJAX、JSP、JavaBean
本项目开发环境为:
- Intellij IDEA 2016.3
- Tomcat 8
- JDK 1.8
- MySQL 5.5
数据库配置文件可以下载Mynews.sql文件,直接导入配置就好
简单界面展示
登录界面
用户主要浏览界面
发布文章界面
管理员界面
为了能够让下载的工程文件直接运行这里再放上数据库的结构图:
简单代码介绍
为了节省篇幅这里主要介绍JSP中负责和Servlet或后台DAO有关的代码。
登录界面:
主要的就是form标签里的action属性,表示将表单里的内容提交给后台的checkLogin_user这个Servlet进行处理,其中input标签里的name属性标记其中的值,可以在Servlet中使用request.getparameter()方法得到标签中填入的值。 (还有一点需要注意的是action里的方法需要先在web.xml文件中进行注册,这样tomcat服务器才能正确的找到对应的类进行后续的处理,实际上所有的Servlet类都需要在web.xml中进行注册,所以后面就不再赘述这个问题了)
```
```
处理登录的Servlet:
处理JSP发送过来的数据,调用后台程序进行处理,并返回结果。
```java package servlet;
import dao.NewsRealeseDao;
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 java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class checkLogin_user extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//这里的"user"和"pass"必须要和JSP里的对应的标签的name属性相同
String user=request.getParameter("user");
String pass=request.getParameter("pass");
NewsRealeseDao newsRealeseDao=new NewsRealeseDao();
try {
boolean checked=newsRealeseDao.ischecked(user,pass,"user");
//调用后台的Dao方法利用user表进行身份验证
if(checked)
{
HttpSession session=request.getSession();
session.setAttribute("username",user);//设置用户的姓名
response.sendRedirect("content_user.jsp");
}
else
{
response.sendRedirect("login_user.jsp");
}
}
catch (Exception ex)
{
Logger.getLogger(checkLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description"+"public String getServletInfo() ";
}
} ```
数据库访问类newsRealeseDao:
访问数据库并提供相关的方法。
```java
import com.lut.beans.NewsRealese;
import java.sql.*; import java.util.ArrayList; import java.util.HashMap;
public class NewsRealeseDao {
public static String driver = "com.mysql.jdbc.Driver";//定义驱动
public static String url = "jdbc:mysql://localhost:3306/myNews?useUnicode=true&characterEncoding=utf-8";//定义URL
public static String databseUser = "root";//定义用户名
public static String password = "root";//定义密码
private ArrayList getNews(Statement stat, String sql)//处理具体的新闻查询请求,返回所有结果
{
ArrayList newsRealese = new ArrayList();
try {
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) { //实例化VO
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
newsRealese.add(news);
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
return newsRealese;
}
}
public ArrayList UserQueryAllNews(String username, String table) throws Exception {//用户查看自己所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from " + table + " where issueuser='" + username + "' order by publish_time desc";
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
public ArrayList UserQueryOthersNews(String username) throws Exception {//查看自己可见的其他人的所有的文章
Connection conn = null;
ArrayList rt = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news where newsId IN (select newsId from authority where username=?) order by publish_time desc";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
NewsRealese news = new NewsRealese();
news.setNewsId(rs.getString("newsid"));
news.setContent(rs.getString("content"));
news.setHead(rs.getString("head"));
news.setIssueuser(rs.getString("issueuser"));
news.setPublish_time(rs.getString("publish_time"));
news.setNewstype(rs.getString("newstype"));
rt.add(news);
}
if (rt.size() == 0) {
System.out.println("查询不到任何信息============Dao.UserQueryOthersNews");
return null;
}
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return rt;
}
}
public ArrayList AdministorQueryAllNews(String username) throws Exception {//管理员查看所有的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);
//运行SQL语句
String sql = "select * from news order by publish_time desc";//获取
Statement stat = conn.createStatement();
newsRealese = getNews(stat, sql);
if (newsRealese.size() == 0) {
System.out.println("查询不到任何信息============");
return null;
}
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//查询一个消息
public ArrayList AdministorQueryCheckPending() throws Exception {//查询所有的待审核的文章
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "select * from check_pending order by publish_time desc";//获取newsid,使用?代替字符串,以免会发生错误
Statement st = conn.createStatement();
newsRealese = getNews(st, sql);
stat.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
return newsRealese;
}
}
//用户删除数据
public String deleteOneNews(String newsid, String table) throws Exception {
Connection conn = null;
ArrayList newsRealese = new ArrayList();
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_move = "insert into dustbin select * from news where newsId='" + newsid + "'";
String sql_delete = "DELETE FROM " + table + " WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
int rs_move = ps.executeUpdate(sql_move);
if (rs_move != 0) {
int rs = ps.executeUpdate(sql_delete);
if (rs == 0)
System.out.println("删除失败==================NewsrealeaseDao");
} else {
System.out.println("插入到dustbin出错=============NewsrealeaseDao");
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return newsRealese.toString();
}
}
public String AdmindeleteCheck_pendingNews(String newsid) throws Exception {
Connection conn = null;
int rs = 0;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
String sql_delete = "DELETE FROM check_pending WHERE newsId='" + newsid + "'";//获取newsid,使用?代替字符串,以免会发生错误
Statement ps = conn.createStatement();
rs = ps.executeUpdate(sql_delete);
if (rs == 0) {
System.out.println("删除失败==================NewsrealeaseDao");
return null;
}
else
return "成功删除"+rs;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
}
return null;
}
public int AuthorizeOneNews(ArrayList<String> userlist, String newsid) {
int count = 0;
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
for (int i = 0; i < userlist.size(); i++) {
String sql = "insert into authority VALUES(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, newsid);
ps.setString(2, userlist.get(i));
System.out.println(ps.toString());
count += ps.executeUpdate();
}
System.out.println("成功添加" + count + "行");
stat.close();
conn.close();
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
}
} catch (Exception ex) {
}
return count;
}
}
//插入数据
public int insertOneNews(HashMap<String, String> addnews_list, String table) throws Exception {//插入一个新的新闻
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//运行SQL语句
Statement stat = conn.createStatement();
String sql = "insert into " + table + " VALUES(?,?,?,?,?,?)";//获取newsid,使用?代替字符串,以免会发生错误
PreparedStatement ps = conn.prepareStatement(sql);
// ps.setString(1, table);
ps.setString(1, addnews_list.get("newsid"));
ps.setString(2, addnews_list.get("head"));
ps.setString(3, addnews_list.get("content"));
ps.setString(4, addnews_list.get("publish_time"));
ps.setString(5, addnews_list.get("issueuser"));
ps.setString(6, addnews_list.get("newstype"));
System.out.println(addnews_list.get("newstype") + "===============" + ps.toString());
int i = ps.executeUpdate();
System.out.println("成功添加" + i + "行");
stat.close();
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public HashMap<String, String> queryOneNews(String newsid, String table) throws SQLException {
Connection connection = null;
HashMap<String, String> rt = new HashMap<>();
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, databseUser, password);
String sql = "select * from " + table + " where newsId='" + newsid + "'";
Statement ps = connection.createStatement();
System.out.println("将要执行的=====" + sql);
ResultSet rs = ps.executeQuery(sql);
while (rs.next()) {
rt.put("newsid", rs.getString("newsId"));
rt.put("head", rs.getString("head"));
rt.put("content", rs.getString("content"));
rt.put("time", rs.getString("publish_time"));
rt.put("author", rs.getString("issueuser"));
rt.put("newstype", rs.getString("newstype"));
return rt;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
}
return rt;
}
//更新数据
public int updateOneNews(HashMap<String, String> addnews_list) throws Exception {
Connection conn = null;
try {
//获取连接
Class.forName(driver);
conn = DriverManager.getConnection(url, databseUser, password);//不安全
//获取newsid,使用?代替字符串,以免会发生错误
String sql = "UPDATE news set head=?,content=?,publish_time=?,issueuser=?,newstype=? where newsId=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(6, addnews_list.get("newsid"));
ps.setString(1, addnews_list.get("head"));
ps.setString(2, addnews_list.get("content"));
ps.setString(3, addnews_list.get("publish_time"));
ps.setString(4, addnews_list.get("issueuser"));
ps.setString(5, addnews_list.get("newstype"));
System.out.println(ps.toString());
int i = ps.executeUpdate();
System.out.println("成功更新" + i + "行");
conn.close();
return i;
} catch (Exception e1) {
e1.printStackTrace();
} finally {
try {//关闭连接
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
}
}
return 0;
}
public boolean CheckNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from check_pending where newsId=?";
String sql_delete = "delete from check_pending where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ResumeNews(String newsid)//审核通过一片文章
{
String sql_insert = "insert into news select * from dustbin where newsId=?";
String sql_delete = "delete from dustbin where newsId=?";
Connection con = null;
try {
Class.forName(driver);
con = DriverManager.getConnection(url, databseUser, password);
PreparedStatement ps = con.prepareStatement(sql_insert);
ps.setString(1, newsid);
int result = ps.executeUpdate();
if (result != 0) {
ps = con.prepareStatement(sql_delete);
ps.setString(1, newsid);
int r = ps.executeUpdate();
if (r != 0)
return true;
else
return false;
} else
return false;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
public boolean ischecked(String user, String pass, String table) throws Exception {//登录验证 table表示从哪个表中查询
Connection conn = null;
//获取连接
Class.forName(driver);
String sql = "select password from " + table + " where username='" + user + "'";
System.out.println(sql);
try {
conn = DriverManager.getConnection(url, databseUser, password);//不安全
System.out.println("建立database连接");
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
if (rs.getString("password").equals(pass))
return true;
else
return false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return false;
}
private static void show(ArrayList in) {
for (int i = 0; i < in.size(); i++) {
NewsRealese tem = (NewsRealese) in.get(i);
System.out.println(tem.getHead() + "======" + tem.getContent() + "@@@" + tem.getIssueuser() + "=====" + tem.getPublish_time());
}
}
public static void main(String arg[]) {
HashMap<String, String> addnews_list = new HashMap<>();
addnews_list.put("newsid", "1234567892");
addnews_list.put("head", "第2条测试");
addnews_list.put("content", "这个是第一条测试文章,里面的内容都是随便乱写的~亏大发老师教辅拉世纪东方拉开圣诞节福利卡士大夫拉克丝打开发送大六块腹肌");
addnews_list.put("publish_time", "2017-4-4/12:12:12");
addnews_list.put("issueuser", "木子勇士心");
addnews_list.put("newstype", "1");
NewsRealeseDao nd = new NewsRealeseDao();
ArrayList rs = nd.AdministorQueryCheckPending(); ///查询所有的待审核订单
show(rs);
} catch (Exception e) {
e.printStackTrace();
}
// nd.CheckNews("1234567890"); //审核一个订单
}
} ```
web.xml 配置文件:
```xml
```
下面简单介绍一下如何查看每个人的所有文章
实际上就是直接调用NewsRealeseDao.java中的方法来来直接查询数据库中的记录,然后将结果放到一个ArrayList中直接返回,然后在JSP页面上显示出来。 (查看别人已授权的文章方法类似,只是查询的时候增加一个条件而已) JSP页面:
```jsp
选择 | 标题 | 时间 | 类别 | 操作 |
---|---|---|---|---|
<%=tem.getHead()%> | <%=tem.getPublish_time()%> | <%=tem.getNewstype()%> | 详情 修改 删除 |
参考文献
- 利用JSP技术开发基于WEB的人事工资管理系统(大连铁道学院·杜欣然)
- 基于.NET架构的自助建站系统的研究与实现(南昌大学·涂剑峰)
- 基于Java EE的个人博客管理系统的设计和实现(内蒙古大学·闫伟光)
- 博客管理系统的设计与实现(吉林大学·赵岩)
- 基于SSH框架的博客用户分享平台的设计与实现(河北工业大学·刘磊)
- 基于Java EE的个人博客管理系统的设计和实现(内蒙古大学·闫伟光)
- 浙江警察学院公文管理系统的设计和实现(电子科技大学·郦宁伟)
- 基于SSM框架的智能web管理系统的研发设计(北京化工大学·陈红)
- 基于SSM框架的智能web管理系统的研发设计(北京化工大学·陈红)
- 基于SSH框架的博客用户分享平台的设计与实现(河北工业大学·刘磊)
- 利用JSP技术开发基于WEB的人事工资管理系统(电子科技大学·李强)
- 基于SSH框架模式的博客系统的设计与实现(西北师范大学·王刚成)
- 基于JCR的Web内容管理系统的设计与实现(江苏大学·夏纯中)
- 基于.NET架构的自助建站系统的研究与实现(南昌大学·涂剑峰)
- 基于J2EE的高校课堂教学管理系统设计与分析(电子科技大学·张霖婧)
本文内容包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主题。发布者:源码客栈 ,原文地址:https://m.bishedaima.com/yuanma/35814.html