新闻系统(登录&增加)

今天给大家带来的数JavaWeb登录&新闻增加,再开始分享前,先普及一个小知识

如果Oracle登录的密码忘记了该怎么办?

1.首先登录sys(密码是root123

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_15,color_FFFFFF,t_70,g_se,x_16

注:连接一定要为SYSDBA

2.在当前用户中找到Users中的SCOTT

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_13,color_FFFFFF,t_70,g_se,x_16

3.点击右键再点击编辑,然后进行更改口令(密码)再点击应用就可以了

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_20,color_FFFFFF,t_70,g_se,x_16

 登录

我的数据的分布(一定要引入jar包)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_13,color_FFFFFF,t_70,g_se,x_16

效果图如下:

这个账号和密码是我在数据库里已经有的

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_20,color_FFFFFF,t_70,g_se,x_16

点击没有账号会进入到注册界面(注册功能暂未完善)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_20,color_FFFFFF,t_70,g_se,x_16

输入数据库里的账号和密码,正确的话就会到首页面

代码如下:

登录界面

<%@ 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" id="myForm">
        <h3 class="text-center">欢迎使用🐖币新闻管理</h3>
        <div class="form-group">
            <input id="username" name="yh" type="text" class="form-control" placeholder="请输入您的用户名">
        </div>
        <div class="form-group">
            <input id="password" 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.jsp"'>没有账号?</button>
        </div>
    </form>
    
</body>
<script >

	$("#myForm").submit(()=>{
		if($("#username").val().length==0){
			return false;//不提交
		}
		if($("#password").val().length==0){
			return false;
		}
		return true
	})

</script>
</html>
    

登录界面中的验证方法

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@ 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");//密码

	//加载驱动
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");
	//定义连接字符串
	String url="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(url,"scott","123");
	//获得执行对象
	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()){
		//转发
		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>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="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="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="yh" type="text" 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.jsp"'>已有账号?</button>
    </div>
</form>
</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 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="/web04/news/add.jsp">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a><%=request.getParameter("yh") %></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>毛泽东</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>毛泽东</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>毛泽东</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>毛泽东</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>毛泽东</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>&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>

增加

效果图如下:

点击首页上面新闻管理中的新闻发布

9dd848b4476444859377d883b8765dec.png

 会出现一个增加的界面

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6I-y5LuUeW8=,size_20,color_FFFFFF,t_70,g_se,x_16

 点击增加会将数据添加到数据库中,首页的添加目前尚未完善

代码如下:

增加界面

<%@ 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;
        }

        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="/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">
        <div class="panel panel-info">
            <div class="panel-heading">新闻标题</div>
            <input class="form-control" name="title" maxlength="50" required placeholder="标题控制在30个字之内哦~~~">
            <div class="panel-heading">新闻类别</div>
            <select name="" class=" form-control">
                <option value="1">军事1</option>
                <option value="2">军事2</option>
                <option value="3">军事3</option>
                <option value="4">军事4</option>
                <option value="5">军事5</option>
                <option value="6">军事6</option>
            </select>
            <div class="panel-heading">新闻作者</div>
            <input class="form-control" name="author" maxlength="10" required placeholder="名字控制在10个字之内哦~~~">
            <div class="panel-heading">发布时间</div>
            <input type="date" class="form-control" name="publisher" required type="date">
            <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>
                <button class="btn btn-danger">取消</button>
            </div>
        </div>
    </form>

</body></html>

增加界面中的验证方法

因为id不能自增所以在数据插入之前需要先把主键查询出来

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@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");
	
	//新闻的添加(连接数据库)
	
	//加载驱动
	//OracleDriver
	Class.forName("oracle.jdbc.driver.OracleDriver");
	//定义连接字符串
	String url="jdbc:oracle:thin:@localhost:1521:orcl";
	//获得连接
	Connection con=DriverManager.getConnection(url,"scott","123");
	
	//主键不能填
	//主键没有自增的选项(触发器+序列)
	//获得执行对象【数据插入之前,先把主键查询出来】
	PreparedStatement ps=con.prepareStatement("select nvl(news_id,0) from t_name");
	
	ResultSet rs=ps.executeQuery();
	
	int id=0;
	if(rs.next()){
		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='/webo4/news/index.jsp'</script>");
	}else{
		out.print("<script>alert('增加失败');location.href='/webo4/news/index.jsp'</script>");
	}
	//资源关闭
	if(con!=null&&con.isClosed()){
		con.close();
	}
	if(ps!=null){
		ps.close();
	}
	if(rs!=null){
		rs.close();
	}
%>

注:如果界面跳转不了,一定要好好检查有没有拿到数据

今天的分享到此结束啦,下次再见,拜拜。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值