javaweb新闻发布系统

文章目录

          • 项目组成
    • 数据库(oracle)
    • 登入界面
          • 注册界面
          • 系统主页
          • 新闻发布与管理界面
          • 新闻阅读界面
          • 新闻增加功能(完善了部分功能)
          • 新闻修改功能(完善了部分功能)
          • 新闻删除
          • 实现评论

项目组成

javaweb新闻发布系统_第1张图片

数据库(oracle)

create table t_topic
(–新闻话题表
topic_id number primary key,
topic_name varchar2(20) not null
);

create table t_news
(–新闻表
news_id number primary key,
news_title varchar2(255) not null,
news_topic number not null,
news_author varchar2(255) not null,
news_publisher varchar2(255) not null,
news_content long not null,
news_cover varchar2(255)
);

create table t_comment
(–评论表
comment_id number primary key,
comment_from number not null,
comment_publisher varchar2(20) not null,
comment_author varchar2(20) not null,
comment_content varchar2(255) not null
);

create table t_user

(–用户表
user_id number primary key,
user_name varchar(20) not null,
user_pwd varchar(20) not null
);

登入界面

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="/javaweb_06/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="/javaweb_06/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }

        html,
        body {
            background: #1abe9c;
        }

        form {
            width: 300px;
            background: #ebeff2;
            box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
            border-radius: 5px;
            padding: 20px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .btn-group {
            width: 100%;
        }

        .btn-group button {
            width: 50%;
        }
    </style>
</head>

<body>
    <form action="doLogin.jsp" method="post">
        <h3 class="text-center">欢迎使用币新闻管理</h3>
        <div class="form-group">
            <input name="yx" type="email" class="form-control" placeholder="请输入您的邮箱">
        </div>
        <div class="form-group">
            <input name="mm" type="password" class="form-control" placeholder="请输入您的密码">
        </div>
        <div class="btn-group">
            <button type="submit" class="btn btn-primary">登录</button>
            <button type="button" class="btn btn-danger" onclick='location=href="regiest.html"'>没有账号?</button>
        </div>
    </form>
</body></html>

javaweb新闻发布系统_第2张图片

注册界面

regiest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="/javaweb_06/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="/javaweb_06/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }

        html,
        body {
            background: #1abe9c;
        }

        form {
            width: 300px;
            background: #ebeff2;
            box-shadow: 0px 0px 50px rgba(0, 0, 0, .5);
            border-radius: 5px;
            padding: 20px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }

        .btn-group {
            width: 100%;
            margin-bottom: 15px;
        }

        .btn-group > * {
            width: 50%;

        }

    </style>
</head>

<body>
<form action="doZc.jsp" method="post">
    <h3 class="text-center">welcome注册</h3>
    <div class="form-group">
        <input name="yx" type="email" required class="form-control" placeholder="请输入您的邮箱">
    </div>
    <div class="form-group">
        <input name="mm" type="password" required class="form-control" placeholder="请输入您的密码">
    </div>
    <div class="btn-group" data-toggle="buttons">
        <label class="btn btn-primary active"><input type="radio" value="男" name="xb" checked></label>
        <label class="btn btn-primary"><input type="radio" value="女" name="xb"></label>
    </div>
    <div class="form-group">
        <input name="nl" type="number" required min="18" max="150" class="form-control" placeholder="请输入您的年龄">
    </div>
    <div class="btn-group">
        <button type="submit" class="btn btn-primary">注册</button>
        <button type="button" class="btn btn-danger" onclick='location=href="login.html"'>已有账号?</button>
    </div>
</form>
</body>

</html>

javaweb新闻发布系统_第3张图片

系统主页

show.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
</head>

<body>
    <nav class="navbar navbar-default hidden-sm hidden-xs">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" style="font-size: 25px;">当前是新闻首页</a>
            </div>
            <ul class="nav navbar-nav navbar-right" style="margin-right: 20px;">
                <li><a href="${pageContext.request.contextPath}/login.jsp">返回管理员登录界面<span class="glyphicon glyphicon-road"></span></a></li>
            </ul>
        </div>
    </nav>

    <div class="container" style="margin: 10px auto;">
        <div class="btn-group btn-group-justified">
        <%
        	String topicId=request.getParameter("topicId");
          	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
        	//OracleDriver
        	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
        	//编写连接语句
        	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
        	//获得连接
        	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
        	//获得预编译对象(执行对象)
        	PreparedStatement ps=con.prepareStatement("SELECT * FROM T_TOPIC");
        	//获得结果集
        	ResultSet rs=ps.executeQuery();
        	while(rs.next()){
            	if(topicId!=null){
            %>
            		<a href="show.jsp?topicId=<%=rs.getInt(1) %>" type="button" class="btn btn-success <%=rs.getInt(1)==Integer.parseInt(topicId)?"active":"" %>"><%=rs.getString(2)%></a>
            <%
            	}else{
            %>	
            		<a href="show.jsp?topicId=<%=rs.getInt(1) %>" type="button" class="btn btn-success"><%=rs.getString(2)%></a>
            <%
            	}
            }
            %>
        </div>
    </div>

    <div class="container">
        <ul class="list-group">
        <%
        	String sql="SELECT * FROM T_NEWS";
        	if(topicId!=null){
        		sql+=" WHERE NEWS_TOPIC="+topicId;//这里需要在语句前面加上一个空格
        	}
        	ps=con.prepareStatement(sql);
        	rs=ps.executeQuery();
        	while(rs.next()){
        %>
            <li class="list-group-item">
            <h4 class="list-group-item-heading">
                <a href="${pageContext.request.contextPath}/news/read.jsp?newsId=<%=rs.getInt(1) %>&userId=<%=request.getParameter("userId") %>" data-placement="bottom" data-toggle="tooltip" title="<%=rs.getString(2) %>">
                    <%=rs.getString(2) %>
                </a>
            </h4>
            <p class="list-group-item-text text-right">
                <span class="glyphicon glyphicon-user"><code><%=rs.getString(4) %></code></span>
                <span class="glyphicon glyphicon-eye-open"><code><%=rs.getInt(8) %></code></span>
                <span class="glyphicon glyphicon-tag"><code><%=rs.getInt(9) %></code></span>
                <span class="glyphicon glyphicon-time"><code><%=rs.getString(5) %></code></span>
            </p>
        </li>
        <%
        	}
        %>
        </ul>
    </div>
</body>
</html>

新闻发布与管理界面

index.jsp

<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<%
String userId=request.getParameter("userId");
//判断是否为管理员,不是则返回登录界面
if(userId.equals("null")){
	out.print("");
	return;
}
%>
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <%--底下三行是导入的外部样式 --%>
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <%--自定义的样式 --%>
    <style>
        * {
            outline: none !important;
        }

        body,
        html {
            background: #7f8d90;
        }

        nav,
        .breadcrumb {
            border-radius: 0px !important;
            margin-bottom: 0px !important;
        }

        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }

        li h4 {
            width: 300px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>

<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="index.jsp?userId=<%=request.getParameter("userId") %>" style="font-size: 25px;">虚空</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown"> 新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="${pageContext.request.contextPath}/news/add.jsp?userId=<%=request.getParameter("userId") %>">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="${pageContext.request.contextPath}/news/show.jsp">前端新闻主页</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a><%=request.getParameter("userId") %></a></li>
            <li><a href="${pageContext.request.contextPath }/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>

<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">首页</li>
</ol>

<form action="${pageContext.request.contextPath}/news/index.jsp" class="form-inline" style="margin: 0px auto 20px;">
	<input type="hidden" name="userId" value="<%=request.getParameter("userId") %>">
    <div class="form-group" style="display: block;text-align: center;">
        <div class="input-group">
            <div class="input-group-addon">新闻标题</div>
            <input class="form-control" placeholder="请在此输入搜索的关键字" type="text" name="newsName">
            <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit">搜索</button>
                </span>
        </div>
    </div>
</form>

<div class="container">
    <ul class="list-group">
    <%
  	//拿到查询的值
    String newsName=request.getParameter("newsName");
    if(newsName==null){
    	newsName="";
    }
  	//破碎重组,用于post传输的中文数据接收
  	//newsName = new String(newsName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
    
  	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
  	//OracleDriver
  	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
  	//编写连接语句
  	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
  	//获得连接
  	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
  	//获得预编译对象(执行对象)
  	PreparedStatement ps=con.prepareStatement("SELECT * FROM T_NEWS WHERE NEWS_TITLE LIKE ?");
  	ps.setString(1,"%"+newsName+"%");
  	//获得结果集
  	ResultSet rs=ps.executeQuery();
  	//结果集中可能有有很多的数据
  	while(rs.next()){
    %>
        <li class="list-group-item">
            <h4 class="list-group-item-heading">
                <a href="${pageContext.request.contextPath}/news/read.jsp?newsId=<%=rs.getInt(1) %>&userId=<%=request.getParameter("userId") %>" data-placement="bottom" data-toggle="tooltip" title="<%=rs.getString(2) %>">
                    <%=rs.getString(2) %>
                </a>
            </h4>
            <p class="list-group-item-text text-right">
                <span class="glyphicon glyphicon-user"><code><%=rs.getString(4) %></code></span>
                <span class="glyphicon glyphicon-eye-open"><code><%=rs.getInt(8) %></code></span>
                <span class="glyphicon glyphicon-tag"><code><%=rs.getInt(9) %></code></span>
                <span class="glyphicon glyphicon-time"><code><%=rs.getString(5) %></code></span>
            </p>
        </li>
    <%
    	}
  	//关闭资源
  	if(con!=null&&!con.isClosed()){
  		con.close();
  	}
  	if(ps!=null){
  		ps.close();
  	}
  	if(rs!=null){
  		rs.close();
  	}
    %>
    </ul>
</div>
<div class="container text-center">
    <ul class="pagination" style="margin: 20px auto;">
        <li>
            <a href="#"><span>&laquo;</span></a>
        </li>
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
        <li><a href="#">5</a></li>
        <li>
            <a href="#"><span>&raquo;</span></a>
        </li>
    </ul>
</div>
<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip({
            trigger: "hover"
        })
    })
</script>
</body>
</html>
新闻阅读界面

read.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }

        body,
        html {
            background: #7f8d90;
        }

        nav,
        .breadcrumb {
            border-radius: 0px !important;
            margin-bottom: 0px !important;
        }

        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }

        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0px !important;
        }

        .breadcrumb .active {
            color: yellow;
        }
        
        #del{
        	margin-right: 840px;
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-default hidden-sm hidden-xs">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp?userId=<%=request.getParameter("userId") %>" style="font-size: 25px;">虚空</a>
            </div>
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown">
                        新闻管理
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="${pageContext.request.contextPath}/news/add.jsp?userId=<%=request.getParameter("userId") %>">新闻发布</a></li>
                        <li class="divider"></li>
                        <li><a href="${pageContext.request.contextPath}/news/show.jsp">前端新闻主页</a></li>
                    </ul>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a><%=request.getParameter("userId") %></a></li>
                <li><a href="${pageContext.request.contextPath }/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
            </ul>
        </div>
    </nav>
    <ol class="breadcrumb">
        <li>您当前的位置是</li>
        <li>新闻发布系统</li>
        <li class="active">新闻阅读</li>
    </ol>
<%
	String newsId=request.getParameter("newsId");
	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
	//编写连接语句
	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
	//获得预编译对象(执行对象)
	PreparedStatement ps=con.prepareStatement("SELECT * FROM T_NEWS WHERE NEWS_ID=?");
	ps.setInt(1,Integer.parseInt(newsId));
	//获得结果集
	ResultSet rs=ps.executeQuery();
	//定义需要的值
	String title="";
    int count=0;
    String author="";
    String publisher="";
    String content="";
    //赋值
    if(rs.next()){
    	title=rs.getString(2);
        publisher=rs.getString(5);
        author=rs.getString(4);
        content=rs.getString(6);
        count=rs.getInt(8)+1;//当前你阅读了一次
    }
    //新闻已经被阅读了,阅读量加1
    ps=con.prepareStatement("UPDATE T_NEWS SET NEWS_COUNT=NEWS_COUNT+1 WHERE NEWS_ID=?");
    ps.setInt(1,Integer.parseInt(newsId));
    ps.executeUpdate();
%>
    <div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;">
        <h1><%=title%></h1>
        <h3 class="text-right">
            <small>
                <span class="glyphicon glyphicon-user"><span class="label label-default"><%=author%></span></span>
                <span class="glyphicon glyphicon-eye-open"><span class="label label-default"><%=count%></span></span>
                <span class="glyphicon glyphicon-time"><span class="label label-info"><%=publisher%></span></span>
            </small>
        </h3>
        <samp><%=content%></samp>
        <div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
            <div class="btn-group">
                <a href="${pageContext.request.contextPath}/news/doDel.jsp?newsId=<%=newsId%>&userId=<%=request.getParameter("userId") %>" class="btn btn-info">删除</a>
            </div>
            <div class="btn-group">
                <a href="${pageContext.request.contextPath}/news/upd.jsp?newsId=<%=newsId%>&userId=<%=request.getParameter("userId") %>" class="btn btn-info">修改</a>
            </div>
        </div>
    </div>
    <div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;">
    <%
    	ps=con.prepareStatement("SELECT * FROM T_COMMENT WHERE COMMENT_FROM=?");
    	ps.setInt(1,Integer.parseInt(newsId));
    	rs=ps.executeQuery();
    	while(rs.next()){
    %>
        <div class="panel panel-default" style="margin-top: 20px;">
            <div class="panel-heading">
                <span class="glyphicon glyphicon-user"><span class="label label-success"><%=rs.getString(4) %></span></span>
                <p style="margin-top: 10px;text-indent: 2em;">
                    <samp><%=rs.getString(5) %></samp>
                </p>
                <p class="text-right">
                	<a href="${pageContext.request.contextPath}/news/doDelComment.jsp?newsId=<%=newsId%>&userId=<%=request.getParameter("userId") %>&commentId=<%=rs.getInt(1)%>" class="btn btn-info" id="del">删除</a>
                    <span class="glyphicon glyphicon-time"><span class="label label-info"><%=rs.getString(3) %></span></span>
                </p>
            </div>
        </div>
    <%
    	}
    %>
    </div>
	<%
		//关闭资源
		if(con!=null&&!con.isClosed()){
			con.close();
		}
		if(ps!=null){
			ps.close();
		}
		if(rs!=null){
			rs.close();
		}
	%>
    <form action="doAddComment.jsp" class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;">
        <input type="hidden" name="newsId" value="<%=newsId%>">
        <input type="hidden" name="userId" value="<%=request.getParameter("userId") %>">
        <div class="form-group">
            <label for="">Name</label>
            <input name="author" type="text" class="form-control" placeholder="用户名称" required>
        </div>
        <div class="form-group">
            <label for="">Comment</label>
            <input name="content" type="text" class="form-control" placeholder="评论内容" required>
        </div>
        <button type="submit" class="btn btn-default">发布评论</button>
    </form>

    <div style="height: 50px;"></div>
</body>
</html>

新闻增加功能(完善了部分功能)

add.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<%
String userId=request.getParameter("userId");
//判断是否为管理员,不是则返回登录界面
if(userId.equals("null")){
	out.print("");
	return;
}
%>
<head>
    <meta charset="UTF-8">
    <title>增加新闻</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%--底下三行是导入的外部样式 --%>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <%--自定义的样式 --%>
    <style>
        * {
            outline: none !important;
        }

        body,
        html {
            background: #7f8d90;
        }

        nav,
        .breadcrumb {
            border-radius: 0px !important;
            margin-bottom: 0px !important;
        }

        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }

        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0px !important;
        }
        
        .breadcrumb .active{
            color: yellow;
        }
    </style>
</head>

<body>
    <nav class="navbar navbar-default hidden-sm hidden-xs">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp?userId=<%=request.getParameter("userId") %>" style="font-size: 25px;">虚空</a>
            </div>
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <a class="dropdown-toggle" data-toggle="dropdown">
                        新闻管理
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu">
                        <li><a href="">新闻发布</a></li>
                        <li class="divider"></li>
                        <li><a href="${pageContext.request.contextPath}/news/show.jsp">前端新闻主页</a></li>
                    </ul>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a><%=request.getParameter("userId") %></a></li>
                <li><a href="${pageContext.request.contextPath }/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
            </ul>
        </div>
    </nav>

    <ol class="breadcrumb">
        <li>您当前的位置是</li>
        <li>新闻发布系统</li>
        <li class="active">新闻发布</li>
    </ol>
<%--表单,action是提交的地址,需要给input和select标签加上name属性(不然doAdd拿不到数据) --%>
    <form action="doAdd.jsp" class="container">
    <input type="hidden" name="userId" value="<%=request.getParameter("userId") %>"></input>
        <div class="panel panel-info">
            <div class="panel-heading">新闻标题</div>
            <input class="form-control" maxlength="50" name="title" required placeholder="标题控制在30个字之内哦~~~">
            <div class="panel-heading">新闻类别</div>
            <select name="topic" class=" form-control">
            <%
          	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
        	//OracleDriver
        	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
        	//编写连接语句
        	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
        	//获得连接
        	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
        	//获得预编译对象(执行对象)
        	PreparedStatement ps=con.prepareStatement("SELECT * FROM T_TOPIC");
        	//获得结果集
        	ResultSet rs=ps.executeQuery();
        	while(rs.next()){
            %>
            	<option value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option>
            <%
            	}
        	//关闭资源
        	if(con!=null&&!con.isClosed()){
        		con.close();
        	}
        	if(ps!=null){
        		ps.close();
        	}
        	if(rs!=null){
        		rs.close();
        	}
            %>
            </select>
            <div class="panel-heading">新闻作者</div>
            <input class="form-control" maxlength="10" name="author" required placeholder="名字控制在10个字之内哦~~~">
            <div class="panel-heading">发布时间</div>
            <input type="date" class="form-control" name="publisher" required>
            <div class="panel-heading">新闻内容</div>
            <textarea class="form-control" rows="10" name="content" required placeholder="‍达咩~~~~这是必填的"></textarea>
            <div class="panel-footer">
                <button class="btn btn-primary">增加</button>
                <a href="${pageContext.request.contextPath }/news/index.jsp?userId=<%=request.getParameter("userId") %>" class="btn btn-danger">取消</a>
            </div>
        </div>
    </form>
</body>
</html>

新闻修改功能(完善了部分功能)

upd.jsp

<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<%
String userId=request.getParameter("userId");
//判断是否为管理员,不是则返回登录界面
if(userId.equals("null")){
	out.print("");
	return;
}
%>
<head>
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }

        body,
        html {
            background: #7f8d90;
        }

        nav,
        .breadcrumb {
            border-radius: 0 !important;
            margin-bottom: 0 !important;
        }

        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }

        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0 !important;
        }

        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>

<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp?userId=<%=request.getParameter("userId") %>"
               style="font-size: 25px;">虚空</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">
                    新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="${pageContext.request.contextPath}/news/add.jsp?userId=<%=request.getParameter("userId") %>">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="${pageContext.request.contextPath}/news/show.jsp">前端新闻主页</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a><%=request.getParameter("userId") %></a></li>
            <li><a href="${pageContext.request.contextPath }/login.jsp">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>

<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">新闻修改</li>
</ol>
<%
    //获得新闻的id
    String newsId = request.getParameter("newsId");
    //加载驱动
    //OracleDriver
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //定义连接字符串
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    //获得连接
    Connection con = DriverManager.getConnection(url, "scott", "tiger");
    //查询所有的新闻数据
    PreparedStatement ps = con.prepareStatement("SELECT * FROM T_NEWS WHERE NEWS_ID=?");
    //占位符的设置
    ps.setInt(1,Integer.parseInt(newsId));
    //得到结果集
    ResultSet rs = ps.executeQuery();
    //定义需要的值
    String title="";
    int topic=0;
    String author="";
    String publisher="";
    String content="";
    if(rs.next()){
        //可以取值
        title=rs.getString(2);
        publisher=rs.getString(5);
        author=rs.getString(4);
        content=rs.getString(6);
        topic=rs.getInt(3);
    }
%>
<form action="doUpd.jsp" class="container" method="post">
    <div class="panel panel-info">
        <input type="hidden" value="<%=newsId%>" name="newsId">
        <input type="hidden" value="<%=request.getParameter("userId")%>" name="userId">
        <div class="panel-heading">新闻标题</div>
        <input value="<%=title%>" class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
        <div class="panel-heading">新闻类别</div>
        <select class=" form-control" name="topic">
            <%
                //查询对应的类目的信息
                ps=con.prepareStatement("SELECT * FROM T_TOPIC");
                rs=ps.executeQuery();
                while(rs.next()){
            %>
            <!-- 使用三元运算符判断是那种新闻 -->
            <option
                <%=topic==rs.getInt(1)?"selected":""%>
                value="<%=rs.getInt(1)%>"
            >
                    <%=rs.getString(2)%>
            </option>
            <%
                }
            %>
        </select>
        <div class="panel-heading">新闻作者</div>
        <input value="<%=author%>" class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
        <div class="panel-heading">发布时间</div>
        <input value="<%=publisher%>" class="form-control" name="publisher" required type="date">
        <div class="panel-heading">新闻内容</div>
        <textarea class="form-control" name="content" placeholder="‍达咩~~~~这是必填的" required rows="10">
            <%=content%>
        </textarea>
        <div class="panel-footer">
            <button class="btn btn-primary">修改</button>
            <a href="${pageContext.request.contextPath }/news/index.jsp?userId=<%=request.getParameter("userId") %>" class="btn btn-danger">取消</a>
        </div>
    </div>
</form>
<%
//关闭资源
if(con!=null&&!con.isClosed()){
	con.close();
}
if(ps!=null){
	ps.close();
}
if(rs!=null){
	rs.close();
}
%>
</body>
</html>
新闻删除

doDel:处理删除新闻的请求

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 处理删除新闻请求 -->
<%
	String userId=request.getParameter("userId");
	if(userId.equals("null")){
		out.print("");
		return;
	}
	String newsId=request.getParameter("newsId");
	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
	//编写连接语句
	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
	//获得预编译对象(执行对象)
	PreparedStatement ps=con.prepareStatement("DELETE FROM T_NEWS WHERE NEWS_ID=?");
	ps.setInt(1,Integer.parseInt(newsId));
	//获得结果集
	int i = ps.executeUpdate();
	if(i>0){ //删除成功
        out.print("");
    }else{ //删除失败
        out.print("");
    }
	//关闭资源
	if(con!=null&&!con.isClosed()){
		con.close();
	}
	if(ps!=null){
		ps.close();
	}
%>
实现评论

doAddComment.jsp:处理增加评论的请求

<%@page import="java.util.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 处理增加评论的请求 -->
<%
//修改请求的字符编码
	request.setCharacterEncoding("UTF-8");
	//拿到所有的数据
	String newsId=request.getParameter("newsId");
	String publisher=new Date().toLocaleString();//拿到当前时间
	String author=request.getParameter("author");
	String content=request.getParameter("content");
	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
	//编写连接语句
	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
	//获得预编译对象(执行对象)
	PreparedStatement ps=con.prepareStatement("SELECT NVL(MAX(COMMENT_ID),0) FROM T_COMMENT");
	//获得结果集
	ResultSet rs=ps.executeQuery();
	int id=0;
	if(rs.next()){
		id=rs.getInt(1);
	}
	id++;//避免主键冲突
	//编写插入数据库的语句
	String sql="INSERT INTO T_COMMENT(COMMENT_ID,COMMENT_FROM,COMMENT_PUBLISHER,COMMENT_AUTHOR,COMMENT_CONTENT) VALUES(?,?,?,?,?)";
	ps=con.prepareStatement(sql);
	ps.setInt(1, id);
	ps.setInt(2,Integer.parseInt(newsId));
	ps.setString(3, publisher);
	ps.setString(4, author);
	ps.setString(5, content);
	int n=ps.executeUpdate();
	//判断是否增加成功
	if(n>0){
		out.print("");
	}else{
		out.print("");
	}
	//关闭资源
	if(con!=null&&!con.isClosed()){
		con.close();
	}
	if(ps!=null){
		ps.close();
	}
	if(rs!=null){
		rs.close();
	}
%>

doDelComment.jsp:处理删除评论的请求

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- 处理删除评论请求 -->
<%
	String userId=request.getParameter("userId");
	//判断是否为管理员,不是则返回登录界面
	if(userId.equals("null")){
		out.print("");
		return;
	}
	String commentId=request.getParameter("commentId");
	//导入驱动(sqlserver,oracle,mysql),一定要执行 build path,且需要导入对应的jar包
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");//这里使用的是oracle的驱动
	//编写连接语句
	String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(URL, "scott", "tiger");
	//获得预编译对象(执行对象)
	PreparedStatement ps=con.prepareStatement("DELETE FROM T_COMMENT WHERE COMMENT_ID=?");
	ps.setInt(1,Integer.parseInt(commentId));
	//获得结果集
	int i = ps.executeUpdate();
	if(i>0){ //删除成功
	    out.print("");
	}else{ //删除失败
	    out.print("");
	}
	//关闭资源
	if(con!=null&&!con.isClosed()){
		con.close();
	}
	if(ps!=null){
		ps.close();
	}
%>

谢谢浏览 点个关注再走吧!

你可能感兴趣的:(前端,html)