从零开始的个人博客00 --Javaweb

从零开始的个人博客00 --Javaweb


在这里插入图片描述


写在前面

这个是从零开始的个人博客早期的第二版,用jsp和tomcat简单实现了一个可以查询的博客,勉强算是动态的?但是没有写文章的功能,也不太记得当时怎么学的了,放一些当时的代码吧。

<%@ page import="java.util.List" %>
<%@ page import="com.zyq.jdbc.Book" %>
<%@ page import="java.util.LinkedHashSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/index.css">
    <link rel="stylesheet" href="../iconfont/iconfont.css">
    <title>Document</title>
</head>
<body>
    <jsp:include page="header.jsp"></jsp:include>
    <div class="main">
        <div class="mask">
            <img src="../image/2.jpg">
            <h2>hikari</h2>
        </div>
    <div class="text">
        <div class="blog">
            <div class="head">
                <div class="head-left">
                    <h3>博客</h3>
                </div>
                <div class="head-right">&nbsp;&nbsp;
                    <h4>1</h4>
                    &nbsp;&nbsp;</div>
            </div>
            <%
                List<Book> books = (List<Book>)session.getAttribute("books");
                for(Book book : books){
            %>
            <div class="mode">
                <a href=<%=book.getPage_url()%>><img src=<%=book.getImg_url()%>></a>
                <div class="txt">
                    <a href="page_1.jsp"><h3><%=book.getName()%></h3></a>
                    <p>
                        <%=book.getBrief()%>
                    </p>
                </div>
                <div class="information">
                    <img src="../image/2.jpg">
                    <p>
                        <i class="iconfont">&#xe8b4;</i>
                        <%=book.getDate()%>
                    </p>
                </div>
                <div class="box1">
                    <a class="type_a" href="none.html"><%=book.getType()%></a>
                </div>
                <div class="box2">
                    <div class="label">
                        <div class="triangle"></div>
                        <a href="none.html"><%=book.getLabel_1()%></a>
                    </div>
                    <div class="label">
                        <div class="triangle"></div>
                        <a href="none.html"><%=book.getLabel_2()%></a>
                    </div>
                </div>
            </div>
            <%
                }
            %>
            <div class="foot">
                <a href="javascript:;">1</a>
                <a href="javascript:;">2</a>
                <a href="javascript:;">3</a>
                <a href="javascript:;">4</a>
                <a href="javascript:;">5</a>
                <a href="javascript:;">6</a>
            </div>
        </div>
        <div class="box">
            <div class="list">
                <div class="head">
                    <a href="none.html">
                        &nbsp;&nbsp;
                        <i class="iconfont">&#xe6e7;</i>
                        &nbsp;&nbsp;热门文章
                    </a>
                </div>
                <%
                    List<Book> all = (List<Book>)session.getAttribute("all");
                    for (Book book:all){
                %>
                    <div class="type-list">
                        <a href=<%=book.getPage_url()%>><%=book.getName()%></a>
                    </div>
                <%
                    }
                %>
            </div>
            <div class="list">
                <div class="head">
                    <a href="none.html">
                        &nbsp;&nbsp;
                        <i class="iconfont">&#xe6c6;</i>
                        &nbsp;&nbsp;分类
                    </a>
                </div>
                <%
                    LinkedHashSet<String> hashSetByType = new LinkedHashSet<String>();
                    for(Book book:all) {
                        hashSetByType.add(book.getType());
                    }
                    for (String s:hashSetByType){
                %>
                    <div class="type-list">
                        <a href="/index?search=<%=s%>"><%=s%></a>
                    </div>
                <%
                    }
                %>
            </div>
    
            <div class="list">
                <div class="head">
                    <a href="none.html">
                        &nbsp;&nbsp;
                        <i class="iconfont">&#xe887;</i>
                        &nbsp;&nbsp;标签
                    </a>
                </div>
                <div class="label-list">
                    <%
                        LinkedHashSet<String> hashSetByLabel = new LinkedHashSet<String>();
                        for(Book book:all){
                            hashSetByLabel.add(book.getLabel_1());
                            hashSetByLabel.add(book.getLabel_2());
                        }
                        for(String s:hashSetByLabel){
                    %>
                        <div class="label">
                            <div class="triangle"></div>
                            <a href="/index?search=<%=s%>"><%=s%></a>
                        </div>
                    <%
                        }
                    %>
                </div>
            </div>
            <div class="list">
                <div class="head">
                    <a href="none.html">
                        &nbsp;&nbsp;
                        <i class="iconfont">&#xe640;</i>
                        &nbsp;&nbsp;最新推荐
                    </a>
                </div>
                <%
                    for(Book book:all){
                %>
                    <div class="type-list">
                        <a href="<%=book.getPage_url()%>"><%=book.getName()%></a>
                    </div>
                <%
                    }
                %>
            </div>
        </div>
    </div>
    <%@ include file="footer.jsp"%>
</body>
</html>

接下来记下HttpServlet

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.sql.SQLException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String search = request.getParameter("search");
        HttpSession session = request.getSession();
        BookDAO bookDAO = new BookDAO();
        Boolean login = (Boolean) session.getAttribute("login");
        if(login != null && login){
            try {
                List<Book> all = bookDAO.getAll();
                session.setAttribute("all",all);
                if(search != null){
                    List<Book> books = bookDAO.searchBook(search);
                    session.setAttribute("books",books);

                }else{
                    List<Book> books = bookDAO.getAll();
                    session.setAttribute("books",books);
                }
                request.getRequestDispatcher("header").forward(request,response);
            }catch (SQLException sqlException){
                sqlException.printStackTrace();
            }
        }else {
            response.sendRedirect("/login");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String search = request.getParameter("search");
        HttpSession session = request.getSession();
        BookDAO bookDAO = new BookDAO();
        if(search != null && !search.equals("")){
            try {
                List<Book> books = bookDAO.searchBook(search);
                session.setAttribute("books",books);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        response.sendRedirect("/header");
    }
}

完整的代码
添加链接描述


说在最后

安全申明:本人才疏学浅,若有任何谬误,欢迎指正

我的博客:ひかりの博客
csdn主页:csdn博客主页

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值