目录
前言:我已经和大家分享一些关于JavaWeb的简单知识,我会运用这些写一个新闻发布系统,代码 比较多,不会一次性发完,感兴趣的伙伴们可以点个关注,让我们一起期待这个系统的完善 吧ψ(`∇´)ψ。
一.登陆
朋友们要注意哦,我的项目里导入了其他的美化包所有我的界面时比较好看滴~( ̄▽ ̄)~*,但是根据我的代码写得话也不会报错的,就是会比较丑 。
如果家人们也想导入美化包,记得按图片操作
登陆界面代码:
<%@ 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="/Web04/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="/Web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/Web04/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">欢迎使用xx新闻管理</h3>
<div class="form-group">
<input name="yx" type="text" 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>
<script>
$("#myFom").submit(()=>{
if($("#username").val().length==0){
alert("用户名不能为空")
return flase
}
if($("#password").val().length==0){
alert("密码不能为空")
return flase
}
})
</script>
</body>
</html>
登陆逻辑处理代码:
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String yh=request.getParameter("yh");
String mm=request.getParameter("mm");
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con=DriverManager.getConnection(url,"scott","sa123");
//获得执行对象
PreparedStatement ps=con.prepareStatement("select * from t_user where user_name=? and user_pwd=?");
ps.setString(1, yh);
ps.setString(2, mm);
//获得结果集
ResultSet rs=ps.executeQuery();
//判断结果
if(rs.next()){
// localhost:8080/web04/news/index.jsp
request.getRequestDispatcher("/news/index.jsp").forward(request, response);
}else{
//重定向 客户端
/**
(localhost:8080/web04/)跳转的时候:
a.jsp 跳转到同级路径下的a.jsp (localhost:8080/web04/a.jsp)
../a.jsp 跳转到上一级路径下的a.jsp (localhost:8080/a.jsp)
/a.jsp 根目录的a.jsp (localhost:8080/a.jsp)
**/
response.sendRedirect("login.jsp");
}
//资源关闭
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
%>
二.首页代码
<%@ 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="/Web04/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="/Web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/Web04/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.html" 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="/news/add.html">新闻发布</a></li>
<li class="divider"></li>
<li><a href="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
</ul>
</div>
</nav>
<ol class="breadcrumb">
<li>您当前的位置是</li>
<li>新闻发布系统</li>
<li class="active">首页</li>
</ol>
<form class="form-inline" style="margin: 0px auto 20px;">
<div class="form-group" style="display: block;text-align: center;">
<div class="input-group">
<div class="input-group-addon">新闻标题</div>
<input type="text" class="form-control" placeholder="请在此输入搜索的关键字">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">搜索🔍</button>
</span>
</div>
</div>
</form>
<div class="container">
<ul class="list-group">
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code>xxx</code></span>
<span class="glyphicon glyphicon-eye-open"><code>110</code></span>
<span class="glyphicon glyphicon-tag"><code>110</code></span>
<span class="glyphicon glyphicon-time"><code>2020/1/1 10:23:04</code></span>
</p>
</li>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code>xxx</code></span>
<span class="glyphicon glyphicon-eye-open"><code>110</code></span>
<span class="glyphicon glyphicon-tag"><code>110</code></span>
<span class="glyphicon glyphicon-time"><code>2020/1/1 10:23:04</code></span>
</p>
</li>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code>xxx</code></span>
<span class="glyphicon glyphicon-eye-open"><code>110</code></span>
<span class="glyphicon glyphicon-tag"><code>110</code></span>
<span class="glyphicon glyphicon-time"><code>2020/1/1 10:23:04</code></span>
</p>
</li>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code>xxx</code></span>
<span class="glyphicon glyphicon-eye-open"><code>110</code></span>
<span class="glyphicon glyphicon-tag"><code>110</code></span>
<span class="glyphicon glyphicon-time"><code>2020/1/1 10:23:04</code></span>
</p>
</li>
<li class="list-group-item">
<h4 class="list-group-item-heading">
<a href="" data-toggle="tooltip" data-placement="bottom" title="国家卫健委:昨日新增确诊病例29例,其中本土病例2例">
国家卫健委:昨日新增确诊病例29例,其中本土病例2例
</a>
</h4>
<p class="list-group-item-text text-right">
<span class="glyphicon glyphicon-user"><code>xxx</code></span>
<span class="glyphicon glyphicon-eye-open"><code>110</code></span>
<span class="glyphicon glyphicon-tag"><code>110</code></span>
<span class="glyphicon glyphicon-time"><code>2020/1/1 10:23:04</code></span>
</p>
</li>
</ul>
</div>
<div class="container text-center">
<ul class="pagination" style="margin: 20px auto;">
<li>
<a href="#"><span>«</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>»</span></a>
</li>
</ul>
</div>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip({
trigger: "hover"
})
})
</script>
</body>
</html>
三.新闻添加
增加界面的代码:
<%@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 content="width=device-width, initial-scale=1" name="viewport">
<link href="/Web04/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
<script src="/Web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="/Web04/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="/Web04/news/index.html" 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="#">类别管理</a></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a>245@qq.com</a></li>
<li><a href="#">退出<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="doAdd.jsp" class="container" method="post">
<div class="panel panel-info">
<div class="panel-heading">新闻标题</div>
<input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
<div class="panel-heading">新闻类别</div>
<select class=" form-control" name="topic">
<option value="1">国际性新闻</option>
<option value="2">国内性新闻</option>
<option value="3">地方性新闻</option>
<option value="4">典型新闻</option>
<option value="5">综合新闻</option>
<option value="6">文教新闻</option>
</select>
<div class="panel-heading">新闻作者</div>
<input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
<div class="panel-heading">发布时间</div>
<input class="form-control" name="publisher" required type="date">
<div class="panel-heading">新闻内容</div>
<textarea class="form-control" name="content" placeholder="🙅达咩~~~~这是必填的" required rows="10"></textarea>
<div class="panel-footer">
<button class="btn btn-primary">增加</button>
<button class="btn btn-danger">取消</button>
</div>
</div>
</form>
</body>
</html>
处理增加的代码:
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
//接受新闻的数据
String title=request.getParameter("title");
String author=request.getParameter("author");
String publisher=request.getParameter("publisher");
String topic=request.getParameter("topic");
String content=request.getParameter("content");
//新闻的添加(连接数据库)
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//定义连接字符串
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//获得连接
Connection con=DriverManager.getConnection(url,"scott","sa123");
// 主键不能不填
// 主键没有自增的选项(触发器+序列)
//获得执行对象【数据插入之前,先把主键查询出来】
PreparedStatement ps=con.prepareStatement("select nvl(max(news_id),0) from t_news");
ResultSet rs=ps.executeQuery();
int id=0;
if(rs.next()){
id=rs.getInt(1);//查询出来的最大id
}
id++; //为什么加一 【避免主键的重复】
//插入新闻的操作
ps=con.prepareStatement("insert into t_news(news_id, news_title, news_topic, news_author, news_publisher, news_content) values(?,?,?,?,?,?)");
//赋值
ps.setInt(1, id);
ps.setString(2, title);
ps.setInt(3, Integer.parseInt(topic));
ps.setString(4, author);
ps.setString(5, publisher);
ps.setString(6, content);
//执行结果
int n=ps.executeUpdate();
if(n>0){
out.print("<script>alert('增加成功');location.href='/Web04/news/index.jsp'</script>");
}else{
out.print("<script>alert('增加失败');location.href='/Web04/news/index.jsp'</script>");
}
//资源的关闭
if(con!=null&&!con.isClosed()){
con.close();
}
if(ps!=null){
ps.close();
}
if(rs!=null){
rs.close();
}
%>
数据库代码
还有一个用户表我用的是写上一篇文章 时建的,这里我就不写了。
create table t_news(
news_id number primary key ,--新闻id
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--新闻内容
);
今天的就到这里了,下次见,拜拜(づ ̄3 ̄)づ╭❤~