添加博客
package test; 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 java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @WebServlet("/ap") public class AddPostServlet extends HttpServlet { private PostDao postDao = new PostDao(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String title = req.getParameter("title"); String author = req.getParameter("author"); String content = req.getParameter("content"); Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = format.format(d); Post p = new Post(); p.setTitle(title); p.setAuthor(author); p.setContent(content); p.setCreateTime(date); try { postDao.insert(p); resp.sendRedirect("pl"); } catch (Exception e) { e.printStackTrace(); } } } 数据库连接
package test; import java.sql.Connection; import java.sql.DriverManager; public class DBUtil { private static Connection conn = null; static { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/blog?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false"; String username = "root"; String password = "root"; conn = DriverManager.getConnection(url, username, password); }catch (Exception e) { e.printStackTrace(); } } public static Connection getConn() { return conn; } }
删除博客
package test; 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 java.io.IOException; @WebServlet("/dp") public class DeletePostServlet extends HttpServlet { private PostDao postDao = new PostDao(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String a = req.getParameter("id"); int id = Integer.parseInt(a); try { postDao.delete(id); resp.sendRedirect("pl"); } catch (Exception e) { e.printStackTrace(); } } }
package test; public class Post { private int id; private String content; private String title; private String createTime; private String author; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
package test; import java.sql.*; import java.util.ArrayList; import java.util.List; public class PostDao { private Connection conn = DBUtil.getConn(); public List<Post> selectAll() throws Exception { String sql = "select * from posts"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); List<Post> list = new ArrayList<>(); while (rs.next()){ Post post = rsToPost(rs); list.add(post); } return list; } public void insert(Post p) throws SQLException { String sql = "insert into posts (title,author,content,create_time) values(?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,p.getTitle()); ps.setString(2,p.getAuthor()); ps.setString(3,p.getContent()); ps.setString(4,p.getCreateTime()); ps.executeUpdate(); } public void delete(int id) throws Exception { String sql = "delete from posts where id = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1,id); ps.executeUpdate(); } //把结果集数据读取出来,赋值给post对象的方法 private Post rsToPost(ResultSet rs) throws Exception { Post p = new Post(); p.setId(rs.getInt("id")); p.setTitle(rs.getString("title")); p.setContent(rs.getString("content")); p.setCreateTime(rs.getString("create_time")); p.setAuthor(rs.getString("author")); return p; } }
博客列表
package test; 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 java.io.IOException; import java.util.List; @WebServlet("/pl") public class PostListServlet extends HttpServlet { private PostDao postDao = new PostDao(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { List<Post> posts = postDao.selectAll(); req.setAttribute("posts", posts); req.getRequestDispatcher("postList.jsp").forward(req, resp); } catch (Exception e) { e.printStackTrace(); } } } t添加博客jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加博客</title> </head> <body> <h1>添加博客</h1> <form action="ap" method="post"> 标题:<input type="text" name="title" ><br> 作者:<input type="text" name="author" ><br> 内容:<input type="text" name="content" ><br> <input type="submit" value="提交"> </form> </body> </html>
博客列表jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>博客列表</title> </head> <body> <h1>博客列表</h1> <a href="addPost.jsp">添加博客</a> <table border="1" width="600px"> <tr> <td>编号</td> <td>标题</td> <td>作者</td> <td>操作</td> </tr> <c:forEach items="${posts}" var="p"> <tr> <td>${p.id}</td> <td>${p.title}</td> <td>${p.author}</td> <td><a href="dp?id=${p.id}">删除</a></td> </tr> </c:forEach> </table> </body> </html>