从零开始的个人博客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">
共
<h4>1</h4>
篇
</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"></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">
<i class="iconfont"></i>
热门文章
</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">
<i class="iconfont"></i>
分类
</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">
<i class="iconfont"></i>
标签
</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">
<i class="iconfont"></i>
最新推荐
</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");
}
}
完整的代码
添加链接描述
说在最后
安全申明:本人才疏学浅,若有任何谬误,欢迎指正