运用JSP写一个小项目

登录:

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>登录</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;
        }

        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>
<%--表单,action是提交的地址,method是数据传输方式 --%>
    <form action="doLogin.jsp" method="post" id="myForm">
        <h3 class="text-center">欢迎使用虚空新闻管理</h3>
        <div class="form-group">
            <input name="username" type="text" id="id_username" class="form-control" placeholder="请输入您的账号">
        </div>
        <div class="form-group">
            <input name="password" type="password" id="id_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" οnclick='location=href="regiest.html"'>没有账号?</button>
        </div>
    </form>
<script>
//给表单添加一个提交事件(可以尝试导入第三方插件:jQuery Validation),进行一个表单验证
    $("#myForm").submit(()=>{
        if($("#id_username").val().length==0){
            alert("用户名不能为空")
            return false
        }
        if($("#id_password").val().length==0){
            alert("密码不能为空")
            return false
        }
        return true
    })
</script>
</body>
</html>

效果如下

doLogin.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"%>
<!-- 处理登录请求 -->
<%
    //网络中的数据传输使用的都是字节,字节转文字会出现乱码现象,所以需要修改一下请求中的字符编码
    request.setCharacterEncoding("UTF-8");//修改请求中的字符编码
    //获取来自于前端的数据
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    //导入驱动(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");
    //获得预编译对象(执行对象)
    String sql="SELECT * FROM TB_USER WHERE USE_NAME=? AND USE_PWD=?";
    PreparedStatement ps=con.prepareStatement(sql);
    ps.setString(1, username);
    ps.setString(2,password);
    //获得结果集
    ResultSet rs=ps.executeQuery();    
    //做登录验证
    if(rs.next()){
        out.print("<script>alert('登录成功');location.href='news/index.jsp?userId="+username+"'</script>");
        //转发(携带请求数据)
        //request.getRequestDispatcher("/news/index.jsp").forward(request, response);
        //重定向(不携带请求数据)
        //response.sendRedirect("/news/index.jsp");
    }else{
        out.print("<script>alert('登录失败');location.href='login.jsp'</script>");
        //response.sendRedirect("login.jsp");
    }
    //关闭资源
    if(con!=null&&!con.isClosed()){
        con.close();
    }
    if(ps!=null){
        ps.close();
    }
    if(rs!=null){
        rs.close();
    }
%>

注册:

regiest:注册界面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<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;
        }

        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="doRegiest.jsp" method="post">
    <h3 class="text-center">welcome虚空注册</h3>
    <div class="form-group">
        <input name="r_username" type="text" required class="form-control" placeholder="请输入您的账号">
    </div>
    <div class="form-group">
        <input name="r_password" 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="r_sex" checked></label>
        <label class="btn btn-primary">女<input type="radio" value="女" name="r_sex"></label>
    </div>
    <div class="form-group">
        <input name="r_age" 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" οnclick='location=href="login.jsp"'>已有账号?</button>
    </div>
</form>
</body>

</html>

效果如下:

新闻增加:

index.jsp:主页(未完成版)

主页中的新闻数据是定死,目前只有界面和新闻发布功能,后续会继续完善功能

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

<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="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a><%=request.getParameter("userId") %></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 class="form-control" placeholder="请在此输入搜索的关键字" type="text">
            <span class="input-group-btn">
                    <button class="btn btn-primary" type="submit">搜索🔍</button>
                </span>
        </div>
    </div>
</form>

<div class="container">
    <ul class="list-group">
        <li class="list-group-item">
            <h4 class="list-group-item-heading">
                <a data-placement="bottom" data-toggle="tooltip" href="" title="新闻">
                    新闻
                </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 data-placement="bottom" data-toggle="tooltip" href="" title="新闻">
                    新闻
                </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 data-placement="bottom" data-toggle="tooltip" href="" title="新闻">
                    新闻
                </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 data-placement="bottom" data-toggle="tooltip" href="" title="新闻">
                    新闻
                </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 data-placement="bottom" data-toggle="tooltip" href="" title="新闻">
                    新闻
                </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>

结果如下:

add.jsp:增加新闻的页面

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

<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="#">类别管理</a></li>
                    </ul>
                </li>
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <li><a><%=request.getParameter("userId") %></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>
<%--表单,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">
                <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" 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>
                <button class="btn btn-danger">取消</button>
            </div>
        </div>
    </form>
</body>
</html>

效果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值